mcp: fix panics with nil logger; ensure test handlers do not panic (#557)
PR 501 added logging to the streamable server, but didn't pass a non-nil logger to the streamable transport (an oversight). However, tests still passed due to net/http's panic recovery. Update test servers to enforce that handlers do not panic, and fix the panic by: 1. Passing along the configured logger to the session transport. 2. Using ensureLogger in Connect, since we technically allow StreamableServerTransports to be constructed by the user. Fixes #556
This commit is contained in:
parent
4439658dc4
commit
547b5c1426
1 changed files with 36 additions and 12 deletions
|
|
@ -236,6 +236,7 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
|
|||
SessionID: sessionID,
|
||||
Stateless: h.opts.Stateless,
|
||||
jsonResponse: h.opts.JSONResponse,
|
||||
logger: h.opts.Logger,
|
||||
}
|
||||
|
||||
// To support stateless mode, we initialize the session with a default
|
||||
|
|
@ -377,6 +378,10 @@ type StreamableServerTransport struct {
|
|||
// StreamableHTTPOptions.JSONResponse is exported.
|
||||
jsonResponse bool
|
||||
|
||||
// optional logger provided through the [StreamableHTTPOptions.Logger].
|
||||
//
|
||||
// TODO(rfindley): logger should be exported, since we want to allow people
|
||||
// to write their own streamable HTTP handler.
|
||||
logger *slog.Logger
|
||||
|
||||
// connection is non-nil if and only if the transport has been connected.
|
||||
|
|
@ -393,7 +398,7 @@ func (t *StreamableServerTransport) Connect(ctx context.Context) (Connection, er
|
|||
stateless: t.Stateless,
|
||||
eventStore: t.EventStore,
|
||||
jsonResponse: t.jsonResponse,
|
||||
logger: t.logger,
|
||||
logger: ensureLogger(t.logger), // see #556: must be non-nil
|
||||
incoming: make(chan jsonrpc.Message, 10),
|
||||
done: make(chan struct{}),
|
||||
streams: make(map[string]*stream),
|
||||
|
|
|
|||
Loading…
Reference in a new issue