Skip to content

Commit 4257528

Browse files
authored
mcp: set default logging level to info (#411)
During initialization, the default logging level is set to info allowing servers to emit logs immediately without waiting for a client to set a log level. The updated example shows how to send a notification in the context of the request. Fixes #387
1 parent a1eb484 commit 4257528

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

examples/server/distributed/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func parent() {
144144
}
145145

146146
func child(port string) {
147-
// Create a server with a single tool that increments a counter.
147+
// Create a server with a single tool that increments a counter and sends a notification.
148148
server := mcp.NewServer(&mcp.Implementation{Name: "counter"}, nil)
149149

150150
var count atomic.Int64
@@ -153,6 +153,9 @@ func child(port string) {
153153
if *verbose {
154154
log.Printf("request %d (session %s)", n, req.Session.ID())
155155
}
156+
// Send a notification in the context of the request
157+
// Hint: in stateless mode, at least log level 'info' is required to send notifications
158+
req.Session.Log(ctx, &mcp.LoggingMessageParams{Data: fmt.Sprintf("request %d (session %s)", n, req.Session.ID()), Level: "info"})
156159
return nil, struct{ Count int64 }{n}, nil
157160
}
158161
mcp.AddTool(server, &mcp.Tool{Name: "inc"}, inc)

mcp/streamable.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
282282
if !hasInitialized {
283283
state.InitializedParams = new(InitializedParams)
284284
}
285+
state.LogLevel = "info"
285286
connectOpts = &ServerSessionOptions{
286287
State: state,
287288
}

0 commit comments

Comments
 (0)