mcp: cleanup transport after keepalive ping fails
An onClose function is passed to the ServerSession and ClientSession to help cleanup resources from the caller. The onClose function will be executed as part of the ServerSession and ClientSession closure. Fixes #258
This commit is contained in:
parent
9754a2aa86
commit
fb69971650
1 changed files with 118 additions and 11 deletions
|
|
@ -40,6 +40,8 @@ type StreamableHTTPHandler struct {
|
|||
getServer func(*http.Request) *Server
|
||||
opts StreamableHTTPOptions
|
||||
|
||||
onTransportDeletion func(sessionID string) // for testing only
|
||||
|
||||
mu sync.Mutex
|
||||
// TODO: we should store the ServerSession along with the transport, because
|
||||
// we need to cancel keepalive requests when closing the transport.
|
||||
|
|
@ -283,6 +285,19 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
|
|||
connectOpts = &ServerSessionOptions{
|
||||
State: state,
|
||||
}
|
||||
} else {
|
||||
// Cleanup is only required in stateful mode, as transportation is
|
||||
// not stored in the map otherwise.
|
||||
connectOpts = &ServerSessionOptions{
|
||||
onClose: func() {
|
||||
h.mu.Lock()
|
||||
delete(h.transports, transport.SessionID)
|
||||
h.mu.Unlock()
|
||||
if h.onTransportDeletion != nil {
|
||||
h.onTransportDeletion(transport.SessionID)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Pass req.Context() here, to allow middleware to add context values.
|
||||
|
|
|
|||
Loading…
Reference in a new issue