Skip to content

feat(mcp): consume server-initiated notifications via standalone SSE (#193) - #196

Closed
JAORMX wants to merge 1 commit into
mainfrom
feat/mcp-server-initiated-events-193
Closed

JAORMX wants to merge 1 commit into
mainfrom
feat/mcp-server-initiated-events-193

Conversation

@JAORMX

@JAORMX JAORMX commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Closes #193. Phase 1 only (Phase 2 — live catalog refresh — deferred to its own ADR+PR).

Enables the standalone SSE GET stream on the MCP client (removes DisableStandaloneSSE: true) and wires the three listChanged notification handlers on mcpsdk.NewClient in Server.dial — the single construction site reused by Connect and #177's reconnect, so a reconnect re-attaches them automatically.

Design: handlers set a dirty flag + log a WARN (deduped to the false→true transition); accessors lazily re-list under a bounded context.Background(). No catalog mutation (Phase 1). Composes with #177's reconnect. Lifecycle: one persistent handleSSE goroutine per server, drained by session.Close() (ADR 0027 List 1 row 28).

Review: ran the dev-pipeline panel review (spec + standards + secure-code-reviewer + software-architect + QA). Fixed: notification-storm log dedup (Medium), test-helper footgun (Low), ADR latency doc gap (Low), added prompts test (QA). Spec axis clean.

Verification: task lint && task test green; go test -race -count=3 ./internal/adapter/mcp/... green; mecademo smoke intact; llms.txt regenerated.

Out of scope (Phase 2): live catalog refresh (tool.Catalog has no Remove/Replace), ResourceUpdatedHandler, LoggingMessageHandler — own ADR+PR.

…193)

Enable the standalone SSE GET stream (remove DisableStandaloneSSE) and wire
the three list-changed notification handlers (ToolListChangedHandler,
PromptListChangedHandler, ResourceListChangedHandler) in Server.dial — the
single construction site reused by Connect and reconnect, so a reconnect
re-attaches them automatically.

Each handler only sets a dirty flag + logs a WARN; it does NOT re-list
eagerly (it runs on the SDK's SSE goroutine, where a network call would
stall notification processing and could deadlock against s.mu). The
accessors (Tools/Resources/Prompts) lazily re-list on the next read under a
bounded context.Background(), swapping in the fresh snapshot. The re-list
runs WITHOUT s.mu held across the network (only the check + swap hold it)
to avoid deadlocking against reconnect, which re-acquires s.mu.

No catalog mutation: the registered remoteTool specs are untouched; a tool
the server dropped surfaces a tool-call error on use. Live catalog refresh
(Phase 2, needs a tool.Catalog Replace/Unregister seam) is deferred to its
own ADR.

Lifecycle: the SDK spawns one handleSSE goroutine per connected server,
drained by session.Close() → conn.Close() cancelling connCtx. Added a
goleak gate (leakmain_test.go) with a targeted ignore list for the SDK +
stdlib goroutines that unwind asynchronously after close. The SSE stream
composes with #177's reconnect: a transient SSE drop is healed by the SDK's
own reconnect loop; when that exhausts, the connection fails → next POST
surfaces ErrSessionMissing → #177 reconnects the session.

Test teardown ordering fix: with SSE on, httptest.Server.Close() blocks if
the SDK's SSE reader is still attached. The test fixtures (newTestServer,
newToolsOnlyServer, and the internal/app MCP test helpers) now self-register
t.Cleanup(httpSrv.Close) so the listener closes AFTER *Server.Close/
mgr.Close (LIFO), and callers no longer defer stop() (which closed the
listener before the session). This is the bulk of the test-file churn.

Tests: TestToolListChangedNotificationRefreshesSnapshot +
TestResourceListChangedNotificationRefreshesSnapshot (the Phase-1 acceptance
gate — drive a notifications/tools/list_changed from an in-process server,
assert the handler fires + the lazy re-list picks up the new tool). The
goleak gate catches a leaked SSE goroutine.

Docs: ADR 0057 (frozen), ADR 0027 List 1 row 28 (SSE goroutine inventory),
architecture/extensibility.md, PRODUCTION-READINESS.md, llms.txt regenerated.

Phase 2 (live catalog refresh) is referenced in ADR 0057 as the deferred
follow-up.

Closes #193
@JAORMX JAORMX added the e2e-live Run the live e2e suite on this PR label Jun 26, 2026
@JAORMX

JAORMX commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of a clean follow-up PR — #195 already merged the pre-review version to main; this PR carries the post-review fixes that need to land separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

e2e-live Run the live e2e suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant