Skip to content

MCP client: consume server-initiated notifications (enable standalone SSE + listChanged handlers) #193

Closed

Description

@JAORMX

Context

Spun out of #177 (MCP client reconnect on connection drop) — see this PR comment thread.

The MCP adapter (internal/adapter/mcp/mcp.go) sets DisableStandaloneSSE: true on the StreamableClientTransport (line ~332). This is pre-existing v1 behavior, with an honest comment:

// This adapter only issues request/response tool calls; it does not
// consume server-initiated notifications (e.g. tool-list-changed).
// Disabling the standalone SSE GET stream avoids holding a persistent
// connection open, which lets sessions (and test servers) close cleanly.
DisableStandaloneSSE: true,

The result: the harness never opens the persistent SSE GET stream that the MCP Streamable HTTP transport uses for server-initiated messages. A server that fires notifications/tools/list_changed (or the prompts/resources equivalents) finds no live SSE listener and the notification is dropped. The client also doesn't advertise the listChanged capability to servers, so a well-behaved server may not even bother sending the notification.

What the SDK supports (verified against v1.6.1)

The Go SDK fully supports server-initiated notifications on the client side:

  • ClientOptions (mcp/client.go:151-156) exposes ToolListChangedHandler, PromptListChangedHandler, ResourceListChangedHandler, ResourceUpdatedHandler, LoggingMessageHandler, ProgressNotificationHandler.
  • These are already wired into the SDK's clientMethodInfos table (mcp/client.go:909-916) — they route through the receiving method handler when the matching notifications/* method arrives.
  • The SDK's StreamableClientTransport.DisableStandaloneSSE doc (mcp/streamable.go:1512-1530) states: when false (the default), the client opens a persistent SSE GET after initialize; the connection automatically reconnects if interrupted, with a bounded retry loop (handleSSE, maxRetries, retriesWithoutProgress — mcp/streamable.go:1979-2070). When the SSE reconnect exhausts, it calls c.fail(...) which fails the whole connection → the next POST surfaces ErrSessionMissing — which MCP client: reconnect/restart session on connection drop instead of hard-failing #177's reconnect now catches. So the two features compose cleanly.

So enabling notifications is a two-part change:

  1. Set DisableStandaloneSSE: false (or remove the line — false is the SDK default).
  2. Wire the *ListChangedHandler callbacks on mcpsdk.NewClient(...) so the notifications do something useful.

Why it was off

The comment's stated reason — "lets sessions (and test servers) close cleanly" — is the real constraint. The standalone SSE GET holds a persistent connection open for the life of the session (a background handleSSE goroutine per connected server). That means:

  • Lifecycle: Server.Close() and the Manager.Close() teardown now must drain/abort the SSE goroutine too. The SDK cancels it on session.Close() (via the connCtx), so it's bounded, but it's a goroutine that outlives any single tool call — it belongs in the docs/adr/0027-cloud-native.md List 1 resource inventory per the AGENTS.md "outlives-a-call resource" gotcha.
  • Reconnect interaction: MCP client: reconnect/restart session on connection drop instead of hard-failing #177's reconnect path (internal/adapter/mcp/reconnect.go) re-establishes the session via dial, which re-opens the SSE GET, so the notification handlers re-attach automatically (they live on ClientOptions, rebuilt in dial). The SDK's SSE-stream reconnect and MCP client: reconnect/restart session on connection drop instead of hard-failing #177's session reconnect are complementary: a transient SSE drop is healed by the SDK's own reconnect loop; only when that exhausts does the whole session fail with ErrSessionMissing, which MCP client: reconnect/restart session on connection drop instead of hard-failing #177 then catches at the tool-call layer.
  • Tool-list refresh (the hard part): enabling ToolListChangedHandler is the natural complement to ADR 0056's "no tool-list re-fetch on reconnect" decision. A server that re-advertises a different tool set after restart could push a list-changed, and the handler would re-list. But re-registering into the live catalog mid-run crosses a harder boundary: the catalog is shared/process-wide (catalogAssets in internal/app/catalog.go), tools are registered ONCE at buildCatalog/assembleCatalog, and tool.Catalog.Register (engine/tool/catalog.go:28) is append-only — there is no Replace/Unregister/Remove. So a first cut could wire the notification to just invalidate the snapshot + log a WARN (so a stale tool surfaces a clear error on call instead of silently failing), and leave live catalog re-registration as a follow-up.

Proposed scope

Phase 1 — enable the SSE stream + notification plumbing (no catalog mutation)

  • Set DisableStandaloneSSE: false in Server.dial (internal/adapter/mcp/mcp.go).
  • Wire ClientOptions{ToolListChangedHandler, PromptListChangedHandler, ResourceListChangedHandler} on mcpsdk.NewClient(...) — each handler logs a port.Diagnostics WARN naming the server + the changed list, and invalidates the cached Server.tools/resources/prompts snapshot so the NEXT read of Tools()/Resources() re-lists. No catalog mutation yet (the registered remoteTool specs stay as-is; a tool that the server dropped will surface as a tool-call error on use, which the model can react to).
  • Verify the connectStandaloneSSE goroutine is drained on Server.Close() and on MCP client: reconnect/restart session on connection drop instead of hard-failing #177's reconnect path (confirm the SDK's connCtx cancel joins it; add a test that Close doesn't leak the SSE goroutine — goleak-style or a goroutine count assertion).
  • Add the standalone-SSE goroutine to docs/adr/0027-cloud-native.md List 1 (resource inventory): owner = *Server, scope = per connected server, cleanup = session.Close() cancels connCtx → handleSSE returns, re-attach = none (the SDK reconnects the stream itself; MCP client: reconnect/restart session on connection drop instead of hard-failing #177 reconnects the session).

Phase 2 — live catalog refresh (separate, harder)

  • Add a tool.Catalog mutation seam (Replace/Unregister or a Reload that swaps the tool set atomically), OR a per-*Server indirection layer so remoteTool resolves its spec through the *Server's current snapshot instead of a frozen catalog entry.
  • On ToolListChangedHandler, re-list and reconcile the catalog: add new tools, remove dropped tools, update changed specs. Decide the concurrency story (the catalog is read by the dispatch path on every turn; a swap must be atomic).
  • This is gated on Phase 1 and on a design decision about catalog mutation — defer to its own ADR.

Phase 3 — ResourceUpdatedHandler + LoggingMessageHandler (optional, lower priority)

  • ResourceUpdatedHandler could invalidate a cached resource read; LoggingMessageHandler could route server log messages to port.Diagnostics. Neither is load-bearing; file if/when there's operator demand.

Acceptance criteria (Phase 1)

  • A server that fires notifications/tools/list_changed mid-session no longer has its notification dropped — the harness logs it and the next Tools() re-lists.
  • DisableStandaloneSSE is false; the standalone SSE GET is established at connect and drained at close (no goroutine leak; tested).
  • The cloud-native resource inventory (ADR 0027 List 1) has a row for the per-server SSE goroutine.
  • MCP client: reconnect/restart session on connection drop instead of hard-failing #177's reconnect path still works (a server restart still triggers a session reconnect; the SSE stream re-establishes on the fresh session).
  • task lint && task test green; a new test drives a notifications/tools/list_changed from an in-process MCP server and asserts the handler fires + the snapshot refreshes.

Out of scope

  • Live catalog mutation / tool replacement (Phase 2 — separate ADR).
  • Server-side resilience.
  • Changes to the MCP protocol or the streaming-HTTP transport decision (no stdio MCP, ever — ADR constraint).

Priority

Low-Medium — this is an operational improvement (stale tool lists after a server-side change stop being silent), not a correctness bug. The #177 reconnect path already recovers from a dead session; this is about proactive freshness, not reactive recovery. The two are complementary.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions