Skip to content

feat: support the Events extension (SEP-3415) - #1489

Open
cwahn wants to merge 7 commits into
modelcontextprotocol:mainfrom
cwahn:events/pr1-long-lived-requests
Open

cwahn wants to merge 7 commits into
modelcontextprotocol:mainfrom
cwahn:events/pr1-long-lived-requests

Conversation

@cwahn

@cwahn cwahn commented Nov 24, 2026 •

Copy link
Copy Markdown

Closes #1488.

Motivation and Context

This adds the Events extension (SEP-3415, Beta) for MCP 2026-07-28 and later. A server opts in by returning an EventsServer from ServerHandler::events(), and a client uses EventsClient. While events() returns None (the default), dispatch and advertised capabilities are unchanged, so servers that handle events/* in on_custom_request keep working.

It's split across three non-default features, which only use crates rmcp already depends on:

  • events: server/discover advertisement, events/list, eventsListChanged on subscriptions/listen, events/poll, and push over events/stream, with Mcp-Name and Mcp-Param-* headers on Streamable HTTP. An event is backed either by an app-provided check with its own cursors or by an in-memory replay buffer fed through EmitHandle::emit. EventsClient::subscribe returns one stream for push or poll.
  • events-webhook-receiver: verify_webhook for Standard Webhooks signatures, and subscribe_webhook/unsubscribe_webhook on the client.
  • events-webhook: webhook delivery from the server. It requires an authenticated principal, checks consent with a signed challenge before delivering, delivers only over HTTPS and, by default, only to public addresses, and keeps grants in memory with a finite lifetime.

Left out, since the spec makes them optional: Ed25519 (v1a) signatures, the well-known receiver document, durable or non-expiring webhook grants, and suspension.

Two core fixes apply without any of these features, and they're in their own commits:

  • With an EventStore configured, a 2026-07-28 subscriptions/listen was persisted for replay and kept running after the client disconnected. It's now tied to its response stream and cancelled on disconnect. Other requests are still persisted for replay as in feat: add distributed Streamable HTTP event store #1024.
  • On any protocol version, the server now drops a notification whose subscriptionId doesn't match a live request on that connection, including one a proxy forwards with upstream _meta unchanged. A cancellation that hits a full outbound queue is sent once there's room instead of being dropped.

I can move these to a separate PR if you'd rather ship them as fixes first.

How Has This Been Tested?

Added unit tests and stdio and Streamable HTTP integration tests, with webhook delivery tested over loopback TLS. The existing conformance suites still pass. There are no Events scenarios to run yet (modelcontextprotocol/conformance#521 and #541 are open).

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

I used AI assistance to implement and test this.

With an EventStore configured, every modern listen started a persistence
task that never ended on disconnect. The handler was not cancelled and
notifications continued to append to the store indefinitely.

Bind modern subscriptions/listen to the POST response stream: do not
persist or prime it, omit SSE IDs, and cancel the handler on disconnect.
Drop correlated server notifications before transport handoff when their
request is no longer active.

Refs: modelcontextprotocol#1488
@github-actions github-actions Bot added T-test Testing related changes T-core Core library changes T-service Service layer changes T-transport Transport layer changes labels Nov 24, 2026
@github-actions
github-actions Bot requested a review from baxen November 24, 2026 08:46
@github-actions

Copy link
Copy Markdown
Contributor

@baxen is on triage rotation this week and is the reviewer for this one.

Labels come from the PR labeler. Please review the change.

Automated triage. Labels are a first pass and the assigned maintainer has the final say.

Dropping an opening listen while the outbound queue was full left its
notification route registered without a request handle. The shared send
path could also strand a progress-timeout watcher. Reserve queue capacity
before registering either route, then enqueue without yielding.

Dropping an active listen could silently lose its cancellation notification
when the same queue was full, leaving the stdio handler running. Keep the
synchronous fast path and retry a full enqueue on the service runtime.
A closed queue needs no retry.

Refs: modelcontextprotocol#1488

@jokemanfire jokemanfire 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.

Verified at a61f464 (not on trust): I ran the two targets your Verification section names, with your feature list minus local:

cargo test -p rmcp --features '<your list>' --test test_streamable_http_event_store --test test_subscriptions
  test_streamable_http_event_store: 4 passed
  test_subscriptions:              14 passed

Both negative controls reproduce the failures you describe:

  • Deleting the fence (service.rs:1644-1648) makes stdio_fences_inactive_subscriptions fail in both cases: the stale frame for subscription 1 arrives where the test expects the uncorrelated notifications/tools/list_changed, so test_subscriptions.rs:489 sees a non-null _meta.subscriptionId.
  • Restoring if let Some(event_store) at tower.rs:1654 (dropping !long_lived &&) makes modern_listen_bypasses_event_store_and_cancels_on_disconnect fail on both cases with the priming frame the test forbids: data: / id: event-0 / retry: 1000.

I also checked that the sessionful path cannot serve a listen: 2026-07-28 requests are always stateless (tower.rs:228-231) and legacy versions get -32601 for subscriptions/listen, so gating only stateless_sse_response is complete rather than a partial fix.

Two things I would like your read on:

1. The fence drops silently for IDs that are merely not local (service.rs:1644-1648). The predicate is "not in local_ct_pool", not "known-cancelled", and it applies to every protocol version, as the PR says. A downstream bridge that relays an upstream subscription and forwards the notification with the upstream _meta.subscriptionId intact now loses live frames with no diagnostic — from its side the subscription looks quiet rather than broken. Rewriting the id is the bridge's job, so dropping is defensible, but could the drop emit a tracing::debug! (or count it) so a mis-rewriting bridge is diagnosable? Right now responder.send(Ok(())) tells the sender the frame was delivered. Related: the fence keys on request ID only, so the window you describe at 1640-1642 (id reuse after a completed listen) resolves as "a new request with the same id un-fences the stale frame", which is only safe because the response round trip has happened — worth a sentence in the comment if that is the reasoning.

2. spawn_service_queue_task has no depth bound (service.rs:1428, used from try_cancel_request at 1064-1070). Each cancel that meets a full queue spawns a detached task holding a tx clone and one message; nothing caps how many such tasks wait, and nothing logs a give-up. The old code dropped the frame instead, which was also wrong but bounded. Is a single ordered drain task (or a bounded wait plus an explicit drop log) preferable to N detached sends? A second, smaller edge: the handle is captured in Peer::new (service.rs:894), so a Peer that outlives its runtime and is dropped afterwards now calls Handle::spawn on a shut-down runtime where it previously did nothing.

No approve from me on this pass — point 1 is a diagnosability question rather than a defect, so it should not block, but I would like to see your answer first.

Shared seams: server handler discovery and custom dispatch, the core listen filter and client acceptance, custom-message Debug, and Streamable HTTP routing headers. Existing server capabilities and custom dispatch do not change unless events() returns Some; the client opts in explicitly.

Refs: modelcontextprotocol#1488
Shared seams: the Events registry, descriptor hooks and one-shot poll dispatch from PR 2a. No extra core or transport seams change. Nothing in existing server dispatch changes unless events() returns Some.

Refs: modelcontextprotocol#1488
Add the non-default `events` feature to the crate feature table.

Refs: modelcontextprotocol#1488
@github-actions github-actions Bot added T-documentation Documentation improvements T-config Configuration file changes T-examples Example code changes T-handler Handler implementation changes labels Nov 24, 2026
Keep the synchronous send fast path and drain deferred cancellations in order from the service loop instead of spawning a task per cancellation. Log closed-service drops and identify fenced subscription frames by request ID without changing how their producers are acknowledged.

Refs: modelcontextprotocol#1488
@cwahn

cwahn commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks for checking both of these. Pushed ec2dd25.

  1. The fence now logs the subscription id at debug when it drops a frame, and the comment says why id reuse is fine: a new request can only reuse the id after the earlier response has come back.

  2. Cancellations still use try_send when there's room. When the queue is full they go to a side channel that the serve loop drains, in order, into the normal outbound queue, so there's one drain per service instead of a task per cancellation. Its depth is bounded by in-flight requests since each one is cancelled at most once. Peer no longer holds a runtime handle, so dropping one after shutdown just logs that the cancellation couldn't be sent.

@cwahn cwahn changed the title fix(rmcp): honor long-lived request cancellation feat: support the Events extension (SEP-3415) Nov 24, 2026
Serve events/stream with ordered delivery, replay, and heartbeat
checkpoints, and add one managed subscription stream for push and poll.
Add webhook delivery behind events-webhook and Standard Webhooks
verification behind events-webhook-receiver.

Also treat an absent resultType as complete, use the tools rules for
Events routing headers, and drop Descriptor::transform and the raw
payload validator.

Refs: modelcontextprotocol#1488
@cwahn
cwahn force-pushed the events/pr1-long-lived-requests branch from 604f281 to d6aeeb6 Compare November 24, 2026 17:36
@cwahn
cwahn marked this pull request as ready for review November 24, 2026 17:36
@cwahn
cwahn requested a review from a team as a code owner November 24, 2026 17:36
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-24T17:45:34.463128Z
We couldn’t complete this review. Comment @codex review to try again.
d6aeeb6 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

This branch has not been deployed

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

Labels

T-config Configuration file changes T-core Core library changes T-documentation Documentation improvements T-examples Example code changes T-handler Handler implementation changes T-service Service layer changes T-test Testing related changes T-transport Transport layer changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement SEP-3415: Events extension

2 participants