1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
| vim mcp_logger.py ------------------------ mcp_logger.py -----------------------
import sys import subprocess import threading import argparse import os
LOG_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "mcp_io.log")
parser = argparse.ArgumentParser( description="Wrap a command, passing STDIN/STDOUT verbatim while logging them.", usage="%(prog)s <command> [args...]" )
parser.add_argument('command', nargs=argparse.REMAINDER, help='The command and its arguments to execute.')
open(LOG_FILE, 'w', encoding='utf-8')
if len(sys.argv) == 1: parser.print_help(sys.stderr) sys.exit(1)
args = parser.parse_args()
if not args.command: print("Error: No command provided.", file=sys.stderr) parser.print_help(sys.stderr) sys.exit(1)
target_command = args.command
def forward_and_log_stdin(proxy_stdin, target_stdin, log_file): """Reads from proxy's stdin, logs it, writes to target's stdin.""" try: while True: line_bytes = proxy_stdin.readline() if not line_bytes: break
try: line_str = line_bytes.decode('utf-8') except UnicodeDecodeError: line_str = f"[Non-UTF8 data, {len(line_bytes)} bytes]\n"
log_file.write(f"输入: {line_str}") log_file.flush()
target_stdin.write(line_bytes) target_stdin.flush()
except Exception as e: try: log_file.write(f"!!! STDIN Forwarding Error: {e}\n") log_file.flush() except: pass
finally: try: target_stdin.close() log_file.write("--- STDIN stream closed to target ---\n") log_file.flush() except Exception as e: try: log_file.write(f"!!! Error closing target STDIN: {e}\n") log_file.flush() except: pass
def forward_and_log_stdout(target_stdout, proxy_stdout, log_file): """Reads from target's stdout, logs it, writes to proxy's stdout.""" try: while True: line_bytes = target_stdout.readline() if not line_bytes: break
try: line_str = line_bytes.decode('utf-8') except UnicodeDecodeError: line_str = f"[Non-UTF8 data, {len(line_bytes)} bytes]\n"
log_file.write(f"输出: {line_str}") log_file.flush()
proxy_stdout.write(line_bytes) proxy_stdout.flush()
except Exception as e: try: log_file.write(f"!!! STDOUT Forwarding Error: {e}\n") log_file.flush() except: pass finally: try: log_file.flush() except: pass
process = None log_f = None exit_code = 1
try: log_f = open(LOG_FILE, 'a', encoding='utf-8')
process = subprocess.Popen( target_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0 )
stdin_thread = threading.Thread( target=forward_and_log_stdin, args=(sys.stdin.buffer, process.stdin, log_f), daemon=True )
stdout_thread = threading.Thread( target=forward_and_log_stdout, args=(process.stdout, sys.stdout.buffer, log_f), daemon=True )
stderr_thread = threading.Thread( target=forward_and_log_stdout, args=(process.stderr, sys.stderr.buffer, log_f), daemon=True ) def forward_and_log_stderr(target_stderr, proxy_stderr, log_file): """Reads from target's stderr, logs it, writes to proxy's stderr.""" try: while True: line_bytes = target_stderr.readline() if not line_bytes: break try: line_str = line_bytes.decode('utf-8') except UnicodeDecodeError: line_str = f"[Non-UTF8 data, {len(line_bytes)} bytes]\n" log_file.write(f"STDERR: {line_str}") log_file.flush() proxy_stderr.write(line_bytes) proxy_stderr.flush() except Exception as e: try: log_file.write(f"!!! STDERR Forwarding Error: {e}\n") log_file.flush() except: pass finally: try: log_file.flush() except: pass
stderr_thread = threading.Thread( target=forward_and_log_stderr, args=(process.stderr, sys.stderr.buffer, log_f), daemon=True )
stdin_thread.start() stdout_thread.start() stderr_thread.start()
process.wait() exit_code = process.returncode
stdin_thread.join(timeout=1.0) stdout_thread.join(timeout=1.0) stderr_thread.join(timeout=1.0)
except Exception as e: print(f"MCP Logger Error: {e}", file=sys.stderr) if log_f and not log_f.closed: try: log_f.write(f"!!! MCP Logger Main Error: {e}\n") log_f.flush() except: pass exit_code = 1
finally: if process and process.poll() is None: try: process.terminate() process.wait(timeout=1.0) except: pass if process.poll() is None: try: process.kill() except: pass
if log_f and not log_f.closed: try: log_f.close() except: pass
sys.exit(exit_code) ------------------------ mcp_logger.py -----------------------
|