Skip to content

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

Merged
JAORMX merged 1 commit into
mainfrom
feat/mcp-server-initiated-events-193
Jun 25, 2026
Merged

JAORMX merged 1 commit into
mainfrom
feat/mcp-server-initiated-events-193

Conversation

@JAORMX

@JAORMX JAORMX commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Closes #193. Phase 1 only — no live catalog mutation (Phase 2 deferred to its own ADR).

Enables the standalone SSE GET stream on the MCP client and wires the three list-changed notification handlers (ToolListChangedHandler/PromptListChangedHandler/ResourceListChangedHandler), so server-initiated notifications/{tools,prompts,resources}/list_changed are no longer dropped.

See ADR 0057 for the full design. Key points:

Tests: TestToolListChangedNotificationRefreshesSnapshot + TestResourceListChangedNotificationRefreshesSnapshot (the acceptance gate), goleak gate. task lint && task test green.

…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 merged commit 675bf81 into main Jun 25, 2026
13 checks passed
JAORMX added a commit that referenced this pull request Jun 26, 2026
…193)

Follow-up to #195 (the pre-review version that merged). Applies the four
findings from the panel review that didn't make it into the merged commit:

- [Medium] Notification-storm log dedup (secure-code-reviewer):
  handleListChanged logged a WARN on every notification; a malicious server
  could flood the diagnostics sink. Now logs only on the false→true
  transition (first notification since the last re-list). Pinned by
  TestNotificationStormLogsOncePerRefreshCycle.

- [Low] Test-helper footgun (software-architect): newTestServer returned a
  stop-func that, if defer'd, re-introduced the SSE-teardown wedge. Now
  returns only the URL (self-registers t.Cleanup). Applied to both
  internal/adapter/mcp and internal/app test helpers.

- [Low] ADR 0057 latency doc gap (software-architect): the bounded
  synchronous re-list latency on the List* wire surfaces wasn't documented.
  Added to ADR 0057 Consequences.

- QA gap: no prompts/list_changed test. Added
  TestPromptListChangedNotificationRefreshesSnapshot.
JAORMX added a commit that referenced this pull request Jun 26, 2026
…193)

Follow-up to #195 (the pre-review version that merged). Applies the four
findings from the panel review that didn't make it into the merged commit:

- [Medium] Notification-storm log dedup (secure-code-reviewer):
  handleListChanged logged a WARN on every notification; a malicious server
  could flood the diagnostics sink. Now logs only on the false→true
  transition (first notification since the last re-list). Pinned by
  TestNotificationStormLogsOncePerRefreshCycle.

- [Low] Test-helper footgun (software-architect): newTestServer returned a
  stop-func that, if defer'd, re-introduced the SSE-teardown wedge. Now
  returns only the URL (self-registers t.Cleanup). Applied to both
  internal/adapter/mcp and internal/app test helpers.

- [Low] ADR 0057 latency doc gap (software-architect): the bounded
  synchronous re-list latency on the List* wire surfaces wasn't documented.
  Added to ADR 0057 Consequences.

- QA gap: no prompts/list_changed test. Added
  TestPromptListChangedNotificationRefreshesSnapshot.
JAORMX added a commit that referenced this pull request Jun 26, 2026
…193)

Follow-up to #195 (the pre-review version that merged). Applies the four
findings from the panel review that didn't make it into the merged commit:

- [Medium] Notification-storm log dedup (secure-code-reviewer):
  handleListChanged logged a WARN on every notification; a malicious server
  could flood the diagnostics sink. Now logs only on the false→true
  transition (first notification since the last re-list). Pinned by
  TestNotificationStormLogsOncePerRefreshCycle.

- [Low] Test-helper footgun (software-architect): newTestServer returned a
  stop-func that, if defer'd, re-introduced the SSE-teardown wedge. Now
  returns only the URL (self-registers t.Cleanup). Applied to both
  internal/adapter/mcp and internal/app test helpers.

- [Low] ADR 0057 latency doc gap (software-architect): the bounded
  synchronous re-list latency on the List* wire surfaces wasn't documented.
  Added to ADR 0057 Consequences.

- QA gap: no prompts/list_changed test. Added
  TestPromptListChangedNotificationRefreshesSnapshot.
JAORMX added a commit that referenced this pull request Jun 26, 2026
…197)

Follow-up to #195. Resolves all panel-review findings: notification-storm log dedup, single + concurrent lost-update (clear-before-fetch + per-list generation counters), unbounded-ingestion cap (maxListEntries), honest ADR/doc comments, de-flaked storm test, added prompts/list_changed test. Also bumps toolhive v0.28.3 -> v0.31.0 (pulls a fixed docker/docker), clearing the govulncheck allowlist entirely.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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