Skip to content

Commit 61be548

Browse files
authored
feat: deprecated logger in client & add Logger in ClientOption (#738)
Fix #708 1. deprecated logger in client 2. add Logger in ClientOption 3. add unit test for Logger
1 parent e66c23f commit 61be548

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

mcp/client.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ import (
1818
"time"
1919

2020
"github.com/google/jsonschema-go/jsonschema"
21+
2122
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
2223
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
2324
)
2425

2526
// A Client is an MCP client, which may be connected to an MCP server
2627
// using the [Client.Connect] method.
2728
type Client struct {
28-
impl *Implementation
29-
opts ClientOptions
29+
impl *Implementation
30+
opts ClientOptions
31+
// Deprecated
3032
logger *slog.Logger // TODO: file proposal to export this
3133
mu sync.Mutex
3234
roots *featureSet[*Root]
@@ -42,25 +44,34 @@ type Client struct {
4244
// The first argument must not be nil.
4345
//
4446
// If non-nil, the provided options configure the Client.
45-
func NewClient(impl *Implementation, opts *ClientOptions) *Client {
47+
func NewClient(impl *Implementation, options *ClientOptions) *Client {
4648
if impl == nil {
4749
panic("nil Implementation")
4850
}
49-
c := &Client{
51+
var opts ClientOptions
52+
if options != nil {
53+
opts = *options
54+
}
55+
options = nil // prevent reuse
56+
57+
if opts.Logger == nil { // ensure we have a logger
58+
opts.Logger = ensureLogger(nil)
59+
}
60+
61+
return &Client{
5062
impl: impl,
63+
opts: opts,
5164
logger: ensureLogger(nil), // ensure we have a logger
5265
roots: newFeatureSet(func(r *Root) string { return r.URI }),
5366
sendingMethodHandler_: defaultSendingMethodHandler,
5467
receivingMethodHandler_: defaultReceivingMethodHandler[*ClientSession],
5568
}
56-
if opts != nil {
57-
c.opts = *opts
58-
}
59-
return c
6069
}
6170

6271
// ClientOptions configures the behavior of the client.
6372
type ClientOptions struct {
73+
// Logger may be set to a non-nil value to enable logging of client activity.
74+
Logger *slog.Logger
6475
// CreateMessageHandler handles incoming requests for sampling/createMessage.
6576
//
6677
// Setting CreateMessageHandler to a non-nil value automatically causes the
@@ -407,7 +418,7 @@ func changeAndNotify[P Params](c *Client, notification string, params P, change
407418
}
408419
}
409420
c.mu.Unlock()
410-
notifySessions(sessions, notification, params, c.logger)
421+
notifySessions(sessions, notification, params, c.opts.Logger)
411422
}
412423

413424
// shouldSendListChangedNotification checks if the client's capabilities allow

mcp/client_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package mcp
77
import (
88
"context"
99
"fmt"
10+
"log/slog"
1011
"testing"
1112

1213
"github.com/google/go-cmp/cmp"
@@ -165,6 +166,23 @@ func TestClientPaginateBasic(t *testing.T) {
165166
}
166167
}
167168

169+
func TestClientLogger(t *testing.T) {
170+
// Case 1: No logger provided
171+
c1 := NewClient(&Implementation{Name: "test", Version: "1.0"}, nil)
172+
if c1.opts.Logger == nil {
173+
t.Error("expected default logger, got nil")
174+
}
175+
176+
// Case 2: Logger provided
177+
logger := slog.Default()
178+
c2 := NewClient(&Implementation{Name: "test", Version: "1.0"}, &ClientOptions{
179+
Logger: logger,
180+
})
181+
if c2.opts.Logger != logger {
182+
t.Error("expected provided logger, got different one")
183+
}
184+
}
185+
168186
func TestClientPaginateVariousPageSizes(t *testing.T) {
169187
ctx := context.Background()
170188
for i := 1; i < len(allItems)+1; i++ {

mcp/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type Server struct {
5858
type ServerOptions struct {
5959
// Optional instructions for connected clients.
6060
Instructions string
61-
// If non-nil, log server activity.
61+
// Logger may be set to a non-nil value to enable logging of server activity.
6262
Logger *slog.Logger
6363
// If non-nil, called when "notifications/initialized" is received.
6464
InitializedHandler func(context.Context, *InitializedRequest)

0 commit comments

Comments
 (0)