From ac653c2d02002604a3970e91f73c3929ef719dab Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Wed, 23 Apr 2025 21:30:45 +0800 Subject: [PATCH] feat: print MCP Server logs --- go.mod | 3 ++- go.sum | 6 ++++-- internal/mcp/hub.go | 17 ++++++++++++++++- internal/mcp/hub_test.go | 6 +++--- internal/version/VERSION | 2 +- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 97c0e44a..5f7fcac6 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/joho/godotenv v1.5.1 github.com/json-iterator/go v1.1.12 github.com/maja42/goval v1.2.1 - github.com/mark3labs/mcp-go v0.17.0 + github.com/mark3labs/mcp-go v0.22.0 github.com/mitchellh/mapstructure v1.5.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.33.0 @@ -95,6 +95,7 @@ require ( github.com/sirupsen/logrus v1.9.3 // indirect github.com/slongfield/pyfmt v0.0.0-20220222012616-ea85ff4c361f // indirect github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 // indirect + github.com/spf13/cast v1.7.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/tadglines/go-pkgs v0.0.0-20210623144937-b983b20f54f9 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect diff --git a/go.sum b/go.sum index fdd0eefb..10c191b7 100644 --- a/go.sum +++ b/go.sum @@ -203,8 +203,8 @@ github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4 github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU= github.com/maja42/goval v1.2.1 h1:fyEgzddqPgCZsKcFLk4C6SdCHyEaAHYvtZG4mGzQOHU= github.com/maja42/goval v1.2.1/go.mod h1:42LU+BQXL/veE9jnTTUOSj38GRmOTSThYSXRVodI5J4= -github.com/mark3labs/mcp-go v0.17.0 h1:5Ps6T7qXr7De/2QTqs9h6BKeZ/qdeUeGrgM5lPzi930= -github.com/mark3labs/mcp-go v0.17.0/go.mod h1:KmJndYv7GIgcPVwEKJjNcbhVQ+hJGJhrCCB/9xITzpE= +github.com/mark3labs/mcp-go v0.22.0 h1:cCEBWi4Yy9Kio+OW1hWIyi4WLsSr+RBBK6FI5tj+b7I= +github.com/mark3labs/mcp-go v0.22.0/go.mod h1:rXqOudj/djTORU/ThxYx8fqEVj/5pvTuuebQ2RC7uk4= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -285,6 +285,8 @@ github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sS github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60= github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 h1:TG/diQgUe0pntT/2D9tmUCz4VNwm9MfrtPr0SU2qSX8= github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8/go.mod h1:P5HUIBuIWKbyjl083/loAegFkfbFNx5i2qEP4CNbm7E= +github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y= +github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= diff --git a/internal/mcp/hub.go b/internal/mcp/hub.go index 3283b82a..7f2000bc 100644 --- a/internal/mcp/hub.go +++ b/internal/mcp/hub.go @@ -1,8 +1,10 @@ package mcp import ( + "bufio" "context" "fmt" + "os" "strings" "sync" "time" @@ -92,7 +94,7 @@ func (h *MCPHub) connectToServer(ctx context.Context, serverName string, config delete(h.connections, serverName) } - var mcpClient client.MCPClient + var mcpClient *client.Client var err error // create client @@ -113,6 +115,16 @@ func (h *MCPHub) connectToServer(ctx context.Context, serverName string, config return fmt.Errorf("failed to create client: %w", err) } + // print MCP Server logs + stderr := client.GetStderr(mcpClient) + go func() { + scanner := bufio.NewScanner(stderr) + for scanner.Scan() { + fmt.Fprintf(os.Stderr, "MCP Server %s: %s\n", + serverName, scanner.Text()) + } + }() + // prepare client init request initRequest := mcp.InitializeRequest{} initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION @@ -196,6 +208,9 @@ func (h *MCPHub) GetTool(ctx context.Context, serverName, toolName string) (*mcp func (h *MCPHub) InvokeTool(ctx context.Context, serverName, toolName string, arguments map[string]interface{}, ) (*mcp.CallToolResult, error) { + log.Info().Str("tool", toolName).Interface("args", arguments). + Str("server", serverName).Msg("invoke tool") + conn, err := h.GetClient(serverName) if err != nil { return nil, errors.Wrapf(err, diff --git a/internal/mcp/hub_test.go b/internal/mcp/hub_test.go index edb796a6..80c13a6f 100644 --- a/internal/mcp/hub_test.go +++ b/internal/mcp/hub_test.go @@ -34,9 +34,9 @@ func TestCallTool(t *testing.T) { err = hub.InitServers(ctx) require.NoError(t, err) - result, err := hub.InvokeTool(ctx, "weather", "get_alerts", map[string]interface{}{ - "state": "CA", - }) + result, err := hub.InvokeTool(ctx, "weather", "get_alerts", + map[string]interface{}{"state": "CA"}, + ) require.NoError(t, err) t.Logf("Result: %v", result) } diff --git a/internal/version/VERSION b/internal/version/VERSION index 0bc4f4c1..ef851252 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0-beta-2504212250 +v5.0.0-beta-2504232132