diff --git a/serve/mcp.py b/serve/mcp.py index 610eac61..4586e2c1 100644 --- a/serve/mcp.py +++ b/serve/mcp.py @@ -108,8 +108,9 @@ class StdioTransport: except OSError as e: raise McpError(f"could not start {command!r}: {e}") from None contain(self.proc) # ends with the server, however it ends (Windows) + self._err_reader = threading.Thread(target=self._read_stderr, daemon=True) + self._err_reader.start() threading.Thread(target=self._read, daemon=True).start() - threading.Thread(target=self._read_stderr, daemon=True).start() def alive(self) -> bool: return self.proc is not None and self.proc.poll() is None and not getattr(self, "ended", False) @@ -144,6 +145,9 @@ class StdioTransport: if isinstance(m, dict): self._dispatch(m) self.ended = True + # the server's last log line says why it stopped: let the stderr reader take it first (the pipe closes with + # the process), or the error raced it and said only "the server stopped" + self._err_reader.join(1.0) code = self.proc.poll() err = McpError(f"the server stopped{f' (exit code {code})' if code is not None else ''}{self._tail()}") with self.lock: