From f4a4af8d828207defb9c9c464a9731bcc331aee4 Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Tue, 13 Jan 2026 16:27:52 +0000 Subject: [PATCH] Fix: Write directly to stdout.buffer to bypass TextIOWrapper issues The TextIOWrapper may have buffering issues when stderr is redirected. Writing directly to the binary buffer with encoded bytes ensures immediate delivery of responses to Claude Code. --- scripts/mcp/proxysql_mcp_stdio_bridge.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/scripts/mcp/proxysql_mcp_stdio_bridge.py b/scripts/mcp/proxysql_mcp_stdio_bridge.py index d1cecd227..849c44937 100755 --- a/scripts/mcp/proxysql_mcp_stdio_bridge.py +++ b/scripts/mcp/proxysql_mcp_stdio_bridge.py @@ -271,17 +271,18 @@ class StdioMCPServer: async def _writeline(self, data: Any): """Write JSON data to stdout.""" - loop = asyncio.get_event_loop() output = json.dumps(data, ensure_ascii=False) + "\n" + output_bytes = output.encode('utf-8') - debug_log(f"[_writeline] Writing {len(output)} bytes to stdout") + debug_log(f"[_writeline] Writing {len(output_bytes)} bytes to stdout") debug_log(f"[_writeline] sys.stdout: {sys.stdout}") - debug_log(f"[_writeline] sys.stdout.fileno(): {sys.stdout.fileno() if hasattr(sys.stdout, 'fileno') else 'N/A'}") - - await loop.run_in_executor(None, sys.stdout.write, output) + debug_log(f"[_writeline] sys.stdout.buffer: {sys.stdout.buffer}") - debug_log(f"[_writeline] Data written, now flushing...") - await loop.run_in_executor(None, sys.stdout.flush) + # Write directly to the binary buffer to avoid any TextIOWrapper issues + # This bypasses Python's text encoding layer and writes raw bytes + loop = asyncio.get_event_loop() + await loop.run_in_executor(None, sys.stdout.buffer.write, output_bytes) + await loop.run_in_executor(None, sys.stdout.buffer.flush) debug_log(f"[_writeline] Flush complete")