From b82bd26b85d18bd5f9e8dc69260ca24f5ab6d849 Mon Sep 17 00:00:00 2001 From: Nate Date: Sat, 24 May 2025 15:26:21 -0700 Subject: [PATCH] show stderr in the error message for failed MCP servers --- core/context/mcp/MCPConnection.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/core/context/mcp/MCPConnection.ts b/core/context/mcp/MCPConnection.ts index 875f20f78..e7a441305 100644 --- a/core/context/mcp/MCPConnection.ts +++ b/core/context/mcp/MCPConnection.ts @@ -38,6 +38,10 @@ class MCPConnection { public resources: MCPResource[] = []; private transport: Transport; private connectionPromise: Promise | null = null; + private stdioOutput: { stdout: string; stderr: string } = { + stdout: "", + stderr: "", + }; constructor(public options: MCPOptions) { this.transport = this.constructTransport(options); @@ -91,6 +95,7 @@ class MCPConnection { this.prompts = []; this.resources = []; this.errors = []; + this.stdioOutput = { stdout: "", stderr: "" }; this.abortController.abort(); this.abortController = new AbortController(); @@ -215,6 +220,20 @@ class MCPConnection { } } + // Include stdio output if available for stdio transport + if ( + this.options.transport.type === "stdio" && + (this.stdioOutput.stdout || this.stdioOutput.stderr) + ) { + errorMessage += "\n\nProcess output:"; + if (this.stdioOutput.stdout) { + errorMessage += `\nSTDOUT:\n${this.stdioOutput.stdout}`; + } + if (this.stdioOutput.stderr) { + errorMessage += `\nSTDERR:\n${this.stdioOutput.stderr}`; + } + } + this.status = "error"; this.errors.push(errorMessage); } finally { @@ -284,11 +303,20 @@ class MCPConnection { options.transport.args || [], ); - return new StdioClientTransport({ + const transport = new StdioClientTransport({ command, args, env, + stderr: "pipe", }); + + // Capture stdio output for better error reporting + + transport.stderr?.on("data", (data: Buffer) => { + this.stdioOutput.stderr += data.toString(); + }); + + return transport; case "websocket": return new WebSocketClientTransport(new URL(options.transport.url)); case "sse":