Repository navigation
Description
mcpcompat/server releases a Streamable HTTP session only when the client sends DELETE. If a client goes away without DELETE, its entry in MCPServer.sessions and its per-session go-sdk server, with a copy of every registered tool, resource and prompt, stay until the process exits. The shim never sets StreamableHTTPOptions.SessionTimeout (transports.go:314), so go-sdk never closes the session either. When the SessionIdManager reports a local session terminated, the shim drops its entry but leaves the go-sdk session open. server.go:197 documents the first gap as KNOWN GAP (issue #156, finding 5), but #156 is closed and I couldn't find a follow-up.
Reproduction
Save as mcpcompat/server/repro_abandoned_sessions_test.go and run go test ./mcpcompat/server/ -run TestAbandonedSessions -v:
repro_abandoned_sessions_test.go
package server
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"runtime"
"strings"
"testing"
mcp "github.com/stacklok/toolhive-core/mcpcompat/mcp"
)
func TestAbandonedSessions(t *testing.T) {
s := NewMCPServer("repro", "1.0.0")
for i := range 5000 {
uri := fmt.Sprintf("docs://page/%d", i)
s.AddResource(mcp.Resource{URI: uri, Name: uri},
func(context.Context, mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { return nil, nil })
}
ts := httptest.NewServer(NewStreamableHTTPServer(s))
defer ts.Close()
send := func(method, sid, body string) (string, int) {
req, _ := http.NewRequest(method, ts.URL, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json, text/event-stream")
if sid != "" {
req.Header.Set("Mcp-Session-Id", sid)
req.Header.Set("MCP-Protocol-Version", "2025-06-18")
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
return resp.Header.Get("Mcp-Session-Id"), resp.StatusCode
}
report := func(label string) {
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
n := 0
s.sessions.Range(func(_, _ any) bool { n++; return true })
t.Logf("%-32s registered=%d heap=%.1f MiB", label, n, float64(m.HeapAlloc)/(1<<20))
}
report("start")
var sids []string
for range 50 {
sid, _ := send(http.MethodPost, "", `{"jsonrpc":"2.0","id":1,"method":"initialize",`+
`"params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"c","version":"1"}}}`)
send(http.MethodPost, sid, `{"jsonrpc":"2.0","method":"notifications/initialized"}`)
sids = append(sids, sid)
}
report("50 sessions, clients gone")
_, status := send(http.MethodPost, sids[0], `{"jsonrpc":"2.0","id":2,"method":"ping"}`)
t.Logf("first session still served: HTTP %d", status)
for _, sid := range sids {
send(http.MethodDelete, sid, "")
}
report("after DELETE of each")
}At main (5b2a999, tagged v0.0.50) with go-sdk v1.8.0:
start registered=0 heap=1.5 MiB
50 sessions, clients gone registered=50 heap=60.5 MiB
first session still served: HTTP 200
after DELETE of each registered=0 heap=1.8 MiB
With 5,000 registered resources each abandoned session holds about 1.2 MiB, and only DELETE gives it back.
Impact
Real clients don't always send DELETE. On two ToolHive vMCPs, clients opened 121 sessions in about 38 hours and sent no DELETE at all. The vMCP holds per-session state of its own on top of this (stacklok/toolhive#5860); this issue covers the shim's part.
Suggested fix
- Add an opt-in
WithSessionIdleTimeout(time.Duration)that sets go-sdk'sSessionTimeoutand applies the same idle rule to rehydrated sessions, which the go-sdk handler never sees. An embedder would pass its session TTL. - Start a watcher per session on
ServerSession.Waitthat drops the shim's entry whenever go-sdk closes the session, so the timeout and every other close path end in the same cleanup. - When
Validatereports a local session terminated, close its go-sdk session too, as the rehydrated path already does.
I'll open a PR with this.
Activity