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
This commit is contained in:
parent
a1eb4849b7
commit
4257528dde
2 changed files with 5 additions and 1 deletions
|
|
@ -144,7 +144,7 @@ func parent() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func child(port string) {
|
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)
|
server := mcp.NewServer(&mcp.Implementation{Name: "counter"}, nil)
|
||||||
|
|
||||||
var count atomic.Int64
|
var count atomic.Int64
|
||||||
|
|
@ -153,6 +153,9 @@ func child(port string) {
|
||||||
if *verbose {
|
if *verbose {
|
||||||
log.Printf("request %d (session %s)", n, req.Session.ID())
|
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
|
return nil, struct{ Count int64 }{n}, nil
|
||||||
}
|
}
|
||||||
mcp.AddTool(server, &mcp.Tool{Name: "inc"}, inc)
|
mcp.AddTool(server, &mcp.Tool{Name: "inc"}, inc)
|
||||||
|
|
|
||||||
|
|
@ -282,6 +282,7 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
|
||||||
if !hasInitialized {
|
if !hasInitialized {
|
||||||
state.InitializedParams = new(InitializedParams)
|
state.InitializedParams = new(InitializedParams)
|
||||||
}
|
}
|
||||||
|
state.LogLevel = "info"
|
||||||
connectOpts = &ServerSessionOptions{
|
connectOpts = &ServerSessionOptions{
|
||||||
State: state,
|
State: state,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue