From 2506828d3ec9a574fa350328f4f5e50f423c1af5 Mon Sep 17 00:00:00 2001 From: FanOne <294350394@qq.com> Date: Sat, 27 Dec 2025 17:47:46 +0800 Subject: [PATCH 1/2] feat:remove logger & add Logger in ClientOption --- mcp/client.go | 15 ++++++++++++--- mcp/client_test.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/mcp/client.go b/mcp/client.go index 2dc1a86c..21ab7395 100644 --- a/mcp/client.go +++ b/mcp/client.go @@ -18,6 +18,7 @@ import ( "time" "github.com/google/jsonschema-go/jsonschema" + "github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2" "github.com/modelcontextprotocol/go-sdk/jsonrpc" ) @@ -25,8 +26,9 @@ import ( // A Client is an MCP client, which may be connected to an MCP server // using the [Client.Connect] method. type Client struct { - impl *Implementation - opts ClientOptions + impl *Implementation + opts ClientOptions + // Deprecated logger *slog.Logger // TODO: file proposal to export this mu sync.Mutex roots *featureSet[*Root] @@ -56,11 +58,18 @@ func NewClient(impl *Implementation, opts *ClientOptions) *Client { if opts != nil { c.opts = *opts } + + if opts == nil || opts.Logger == nil { + c.opts.Logger = ensureLogger(nil) + } + return c } // ClientOptions configures the behavior of the client. type ClientOptions struct { + // If non-nil, log client activity. + Logger *slog.Logger // CreateMessageHandler handles incoming requests for sampling/createMessage. // // Setting CreateMessageHandler to a non-nil value automatically causes the @@ -407,7 +416,7 @@ func changeAndNotify[P Params](c *Client, notification string, params P, change } } c.mu.Unlock() - notifySessions(sessions, notification, params, c.logger) + notifySessions(sessions, notification, params, c.opts.Logger) } // shouldSendListChangedNotification checks if the client's capabilities allow diff --git a/mcp/client_test.go b/mcp/client_test.go index ad8c0f12..2f7abc0c 100644 --- a/mcp/client_test.go +++ b/mcp/client_test.go @@ -7,6 +7,7 @@ package mcp import ( "context" "fmt" + "log/slog" "testing" "github.com/google/go-cmp/cmp" @@ -165,6 +166,23 @@ func TestClientPaginateBasic(t *testing.T) { } } +func TestClientLogger(t *testing.T) { + // Case 1: No logger provided + c1 := NewClient(&Implementation{Name: "test", Version: "1.0"}, nil) + if c1.opts.Logger == nil { + t.Error("expected default logger, got nil") + } + + // Case 2: Logger provided + logger := slog.Default() + c2 := NewClient(&Implementation{Name: "test", Version: "1.0"}, &ClientOptions{ + Logger: logger, + }) + if c2.opts.Logger != logger { + t.Error("expected provided logger, got different one") + } +} + func TestClientPaginateVariousPageSizes(t *testing.T) { ctx := context.Background() for i := 1; i < len(allItems)+1; i++ { From 44b51eb135071b2248162dd1e0e040702ed055e3 Mon Sep 17 00:00:00 2001 From: FanOne <294350394@qq.com> Date: Sat, 27 Dec 2025 17:57:07 +0800 Subject: [PATCH 2/2] feat: add Logger option --- mcp/client.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/mcp/client.go b/mcp/client.go index 21ab7395..211ca72c 100644 --- a/mcp/client.go +++ b/mcp/client.go @@ -44,26 +44,28 @@ type Client struct { // The first argument must not be nil. // // If non-nil, the provided options configure the Client. -func NewClient(impl *Implementation, opts *ClientOptions) *Client { +func NewClient(impl *Implementation, options *ClientOptions) *Client { if impl == nil { panic("nil Implementation") } - c := &Client{ + var opts ClientOptions + if options != nil { + opts = *options + } + options = nil // prevent reuse + + if opts.Logger == nil { // ensure we have a logger + opts.Logger = ensureLogger(nil) + } + + return &Client{ impl: impl, + opts: opts, logger: ensureLogger(nil), // ensure we have a logger roots: newFeatureSet(func(r *Root) string { return r.URI }), sendingMethodHandler_: defaultSendingMethodHandler, receivingMethodHandler_: defaultReceivingMethodHandler[*ClientSession], } - if opts != nil { - c.opts = *opts - } - - if opts == nil || opts.Logger == nil { - c.opts.Logger = ensureLogger(nil) - } - - return c } // ClientOptions configures the behavior of the client.