From 86ec75ba2ddd96b00cf46af93b6a2686b9d23a71 Mon Sep 17 00:00:00 2001 From: Luca Beurer-Kellner Date: Tue, 6 May 2025 16:07:50 +0200 Subject: [PATCH] use select() before readline (#45) --- gateway/mcp/mcp.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/gateway/mcp/mcp.py b/gateway/mcp/mcp.py index 9ef00b5..2132155 100644 --- a/gateway/mcp/mcp.py +++ b/gateway/mcp/mcp.py @@ -4,6 +4,7 @@ import sys import subprocess import json import os +import select import threading from invariant_sdk.async_client import AsyncClient @@ -311,11 +312,16 @@ def stream_and_forward_stderr( MCP_LOG_FILE.buffer.write(line) MCP_LOG_FILE.buffer.flush() - def run_stdio_input_loop(ctx: McpContext, mcp_process: subprocess.Popen) -> None: """Handle standard input, intercept call and forward requests to mcp_process stdin.""" + try: - for line in iter(sys.stdin.buffer.readline, b""): + while True: + ready, _, _ = select.select([sys.stdin], [], [], 0.1) + if not ready: + continue + + line = sys.stdin.buffer.readline() if not line: break @@ -371,7 +377,6 @@ def run_stdio_input_loop(ctx: McpContext, mcp_process: subprocess.Popen) -> None except KeyboardInterrupt: mcp_process.terminate() - def split_args(args: list[str] = None) -> tuple[list[str], list[str]]: """ Splits CLI arguments into two parts: