From 7f34e8a12cff7015327375dc56be030014a77f35 Mon Sep 17 00:00:00 2001 From: Luxferre Date: Sun, 22 Mar 2026 13:32:41 +0200 Subject: [PATCH] fixed stdio mcp tool usage --- mcp.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mcp.go b/mcp.go index 4eb9986..fc8671e 100644 --- a/mcp.go +++ b/mcp.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io" "os" "os/exec" "strings" @@ -165,13 +166,11 @@ var defaultMCPFactory MCPClientFactory = func(toolset string) (MCPClientInterfac env = os.Environ() } - // Use WithCommandFunc and redirect Stderr directly to os.Stderr - // This provides the most reliable way to see subprocess errors + // Use WithCommandFunc to configure the command environment c, err = client.NewStdioMCPClientWithOptions(command, env, args, transport.WithCommandFunc(func(ctx context.Context, command string, env []string, args []string) (*exec.Cmd, error) { cmd := exec.CommandContext(ctx, command, args...) cmd.Env = env - cmd.Stderr = os.Stderr return cmd, nil }), ) @@ -209,6 +208,13 @@ var defaultMCPFactory MCPClientFactory = func(toolset string) (MCPClientInterfac return nil, err } + // Redirect stderr from the stdio transport to os.Stderr + if cfg.Transport == "stdio" { + if stderr, ok := client.GetStderr(c); ok { + go io.Copy(os.Stderr, stderr) + } + } + // Initialize the client session initReq := mcp.InitializeRequest{} initReq.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION