MIDI source selector switch
Your device already needs MIDI receiver and transmitter circuits when in mode 2. So the simplest way to pass through MIDI signals is to use these existing circuits to implement a MIDI Thru port (as shown in the MIDI specification); the delay of the optocoupler is not noticeable in practice.

To select between the original signal and the processed data from your microprocessor, you can use any SPDT switch, either a digital multiplexer (e.g., the (SN)74LVC1G97), or an analog multiplexer (e.g., the SN74LVC2G53):