From 4257528dde13d228f1fcfe3a9d60d9df4a0d4887 Mon Sep 17 00:00:00 2001 From: ln-12 <36760115+ln-12@users.noreply.github.com> Date: Thu, 4 Sep 2025 17:10:39 +0200 Subject: [PATCH] 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 --- examples/server/distributed/main.go | 5 ++++- mcp/streamable.go | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/server/distributed/main.go b/examples/server/distributed/main.go index 0433f1f..b144040 100644 --- a/examples/server/distributed/main.go +++ b/examples/server/distributed/main.go @@ -144,7 +144,7 @@ func parent() { } func child(port string) { - // Create a server with a single tool that increments a counter. + // Create a server with a single tool that increments a counter and sends a notification. server := mcp.NewServer(&mcp.Implementation{Name: "counter"}, nil) var count atomic.Int64 @@ -153,6 +153,9 @@ func child(port string) { if *verbose { log.Printf("request %d (session %s)", n, req.Session.ID()) } + // Send a notification in the context of the request + // Hint: in stateless mode, at least log level 'info' is required to send notifications + req.Session.Log(ctx, &mcp.LoggingMessageParams{Data: fmt.Sprintf("request %d (session %s)", n, req.Session.ID()), Level: "info"}) return nil, struct{ Count int64 }{n}, nil } mcp.AddTool(server, &mcp.Tool{Name: "inc"}, inc) diff --git a/mcp/streamable.go b/mcp/streamable.go index 0469613..0f6bfdc 100644 --- a/mcp/streamable.go +++ b/mcp/streamable.go @@ -282,6 +282,7 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque if !hasInitialized { state.InitializedParams = new(InitializedParams) } + state.LogLevel = "info" connectOpts = &ServerSessionOptions{ State: state, }