fixed stdio mcp tool usage

This commit is contained in:
Luxferre
2026-03-22 13:32:41 +02:00
parent d7e14b2d7c
commit 7f34e8a12c
+9 -3
View File
@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
@@ -165,13 +166,11 @@ var defaultMCPFactory MCPClientFactory = func(toolset string) (MCPClientInterfac
env = os.Environ() env = os.Environ()
} }
// Use WithCommandFunc and redirect Stderr directly to os.Stderr // Use WithCommandFunc to configure the command environment
// This provides the most reliable way to see subprocess errors
c, err = client.NewStdioMCPClientWithOptions(command, env, args, c, err = client.NewStdioMCPClientWithOptions(command, env, args,
transport.WithCommandFunc(func(ctx context.Context, command string, env []string, args []string) (*exec.Cmd, error) { transport.WithCommandFunc(func(ctx context.Context, command string, env []string, args []string) (*exec.Cmd, error) {
cmd := exec.CommandContext(ctx, command, args...) cmd := exec.CommandContext(ctx, command, args...)
cmd.Env = env cmd.Env = env
cmd.Stderr = os.Stderr
return cmd, nil return cmd, nil
}), }),
) )
@@ -209,6 +208,13 @@ var defaultMCPFactory MCPClientFactory = func(toolset string) (MCPClientInterfac
return nil, err 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 // Initialize the client session
initReq := mcp.InitializeRequest{} initReq := mcp.InitializeRequest{}
initReq.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION initReq.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION