Skip to content

fix(mcp): notify onclose when standalone SSE stream aborts - #2075

Merged
yusukebe merged 7 commits into
honojs:mainfrom
edenbuilds:fix/mcp-sse-abort-onclose-1762
Aug 18, 2026
Merged

fix(mcp): notify onclose when standalone SSE stream aborts#2075
yusukebe merged 7 commits into
honojs:mainfrom
edenbuilds:fix/mcp-sse-abort-onclose-1762

Conversation

@edenbuilds

Copy link
Copy Markdown
Contributor

Summary

  • Call onclose when the standalone GET SSE stream (_GET_stream) aborts, so apps can detect client disconnects without relying on DELETE.
  • Deduplicate notifications so close() after an abort does not fire onclose twice.

Fixes #1762

Test plan

  • Unit test: cancel SSE body → onclose once; subsequent close() does not double-fire
  • Existing onsessionclosed DELETE tests still pass

Made with Cursor

MCP clients often drop the GET SSE stream without DELETE. Call onclose on
standalone stream abort so apps can detect disconnects, without double-firing
when close() also runs.

Fixes honojs#1762
@changeset-bot

changeset-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 67ebe5c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/mcp Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codecov

codecov Bot commented Aug 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.25%. Comparing base (45c94d6) to head (67ebe5c).
⚠️ Report is 14 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2075      +/-   ##
==========================================
+ Coverage   92.23%   92.25%   +0.02%     
==========================================
  Files         116      116              
  Lines        4122     4134      +12     
  Branches     1077     1082       +5     
==========================================
+ Hits         3802     3814      +12     
  Misses        285      285              
  Partials       35       35              
Flag Coverage Δ
mcp 90.72% <100.00%> (+0.16%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

omkar1work and others added 2 commits August 8, 2026 20:05
Await stopTestServer and prefer ?? for session ids so @hono/mcp lint passes in CI.
@yusukebe

Copy link
Copy Markdown
Member

Hello @MathurAditya724 ! Can you review this?

Comment thread packages/mcp/src/streamable-http.ts Outdated
this.onclose?.()
// Standalone SSE abort may already have notified; still notify when there
// was no GET stream (e.g. DELETE after JSON-only traffic).
this.#notifyClose()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this — exposing a dropped standalone SSE connection would be useful.

I’m not sure onclose is the right hook here, though. In the MCP Streamable HTTP model, a disconnected SSE stream can be resumed, and the session itself remains valid until the client sends DELETE or the server terminates it. The official SDK keeps that distinction: an aborted GET stream only cleans up the stream mapping, while onclose is reserved for transport.close().

With this change, a temporary disconnect fires onclose, but a later reconnect is still allowed. The #closedNotified guard then suppresses the callback when the session is actually closed.

Could we add a separate callback for the standalone SSE stream disconnect instead, and keep onclose for terminal transport shutdown? It would also be great to add an abort → reconnect → DELETE test, especially for the resumable/event-store path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in fd6b5fd. The transport now exposes onsessiondisconnected(sessionId) for standalone SSE aborts while keeping onclose reserved for terminal close() / DELETE shutdown. Added coverage for abort → callback → resumable reconnect; with the source fix stashed, the test fails (0 callback calls), and with it restored the focused test passes. Package typecheck and all @hono/mcp tests pass (196/196).

Expose onsessiondisconnected for standalone SSE aborts while keeping onclose
reserved for terminal transport shutdown. Add abort and resumability coverage.
Remove new non-null assertions from the resumable SSE callback path and cover
errors raised by the session disconnect hook.
@edenbuilds

Copy link
Copy Markdown
Contributor Author

CI follow-up pushed as 67ebe5c8 (edenbuilds).

  • Removed the new non-null assertions and updated the package's suppression count.
  • Added coverage for errors raised by onsessiondisconnected.
  • Verified the original regression was red before the behavior fix; current focused tests pass 67/67, full @hono/mcp tests pass 197/197, typecheck passes, lint has no errors, and Prettier passes.

The earlier red autofix/build/Codecov checks were attributable to the PR; this commit addresses their reported failures.

@yusukebe yusukebe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe

Copy link
Copy Markdown
Member

@edenbuilds @MathurAditya724 Thanks!

@yusukebe
yusukebe merged commit e73716f into honojs:main Aug 18, 2026
98 checks passed
@github-actions github-actions Bot mentioned this pull request Aug 18, 2026
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.

StreamableHTTPTransport: SSE stream abort should notify via onclose (or new callback)

4 participants