From 5d24d33536d1041b4d9f107a5f5813bd43177530 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 2 Jun 2026 00:18:14 +0200 Subject: Harden MCP transport error handling Normalize timeout output to text, convert tool exceptions into tool errors, and keep stdio running after response serialization failures. --- src/sailfish_devel_mcp/server.py | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'src/sailfish_devel_mcp/server.py') diff --git a/src/sailfish_devel_mcp/server.py b/src/sailfish_devel_mcp/server.py index 4fb2f91..7982b21 100644 --- a/src/sailfish_devel_mcp/server.py +++ b/src/sailfish_devel_mcp/server.py @@ -119,6 +119,15 @@ class McpServer: return self.registry[name].handler(args) except ValueError as exc: return tool_error(str(exc)) + except Exception as exc: + return tool_error( + f"{name} failed: {exc}", + { + "error": str(exc), + "exception": type(exc).__name__, + "tool": name, + }, + ) def _resources_list(self) -> dict[str, Any]: return { @@ -158,6 +167,7 @@ def run_stdio(server: McpServer, stdin: TextIO = sys.stdin, stdout: TextIO = sys for line in stdin: if not line.strip(): continue + message: Any = None try: message = json.loads(line) except json.JSONDecodeError as exc: @@ -169,8 +179,26 @@ def run_stdio(server: McpServer, stdin: TextIO = sys.stdin, stdout: TextIO = sys else: response = server.handle(message) if response is not None: - stdout.write(json.dumps(response, separators=(",", ":")) + "\n") - stdout.flush() + try: + response_text = json.dumps(response, separators=(",", ":")) + except TypeError as exc: + request_id = message.get("id") if isinstance(message, dict) else None + response_text = json.dumps( + { + "jsonrpc": "2.0", + "id": request_id, + "error": { + "code": -32603, + "message": f"response serialization failed: {exc}", + }, + }, + separators=(",", ":"), + ) + try: + stdout.write(response_text + "\n") + stdout.flush() + except BrokenPipeError: + return def main(argv: list[str] | None = None) -> None: @@ -193,4 +221,3 @@ def main(argv: list[str] | None = None) -> None: if __name__ == "__main__": main() - -- cgit v1.2.3