Skip to content

Expose per-message processing completion to transports - #3024

Open
servrox wants to merge 9 commits into
modelcontextprotocol:mainfrom
servrox:codex/inbound-request-lifecycle
Open

servrox wants to merge 9 commits into
modelcontextprotocol:mainfrom
servrox:codex/inbound-request-lifecycle

Conversation

@servrox

@servrox servrox commented Oct 14, 2026 •

Copy link
Copy Markdown

Motivation

A transport that limits concurrent requests needs to know when it can reuse a slot. A cancelled request can still have a running handler, and a response can reach send() before the SDK has finished the write and removed the request controller. See #3020.

Proposed API

A transport can attach an optional onMessageProcessed(): void callback to a message’s local MessageExtraInfo. The SDK calls it once after the work it owns for that message settles. The callback stays off the wire. Applications remain responsible for work they start outside a handler’s returned promise.

  • Protocol and client. Completion includes response validation, request cleanup, and notification cache eviction. For an input_required response, the SDK releases the current message’s credit after the local input handler hands off the retry, so a single-credit transport can admit the next response.
  • Stdio. Observed writes wait for their own chunk callback. Ordinary writes keep their existing acceptance and backpressure behavior. A successful listen holds its credit until teardown and outstanding writes settle. A failed acknowledgement releases the subscription slot.

The stdio entry exports supportsInboundMessageCompletion for adapters that require this guarantee. The design question for review is whether this per-message boundary belongs in the transport API.

Verification

Regression tests cover cancellation with a running handler, a single-credit retry, failed listen acknowledgements, and concurrent stdio writes. On ea81ac24, 3,751 tests passed across core-internal, client, and server. One optional soak test was skipped. The SDK build, typecheck, lint, documentation build, and all 14 GitHub checks passed.

@changeset-bot

changeset-bot Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ea81ac2

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

This PR includes changesets to release 6 packages
Name Type
@modelcontextprotocol/server Minor
@modelcontextprotocol/client Minor
@modelcontextprotocol/codemod Minor
@modelcontextprotocol/core Minor
@modelcontextprotocol/server-legacy Minor
@modelcontextprotocol/core-internal 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

@pkg-pr-new

pkg-pr-new Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@3024

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@3024

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@3024

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@3024

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@3024

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@3024

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@3024

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@3024

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@3024

commit: ea81ac2

@claude claude Bot added the v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes label Oct 15, 2026

@karaaslanz karaaslanz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Exact-source review of draft HEAD fdd1ffb9baef3d93c996224d164d5b2ebaf4db7f. Found a reproducible post-close write race introduced by async outbound interception.

Location: packages/server/src/server/serveStdio.ts, StdioConnectionChannel.send (approx. lines 184?197). It checks this._closed before await this._outboundIntercept?.(message), but not again afterwards. modernOutboundIntercept is now async, introducing an await boundary even for ordinary responses where interception returns undefined. If the channel is closed/discarded during the await, the continuation still forwards the message to the shared wire after close() finished.

Exact-source reproduction: fetched serveStdio.ts from the PR head (blob 4f6d5fc7928b93269c149d25691428022e82c153), transformed the actual StdioConnectionChannel class with Node 24 TypeScript-strip, instantiated it using a deferred async interceptor and a counting fake wire. Sequence:

  1. Start channel.send({jsonrpc:'2.0', id:7, result:{ok:true}}); interceptor awaits.
  2. await channel.close().
  3. Resolve the interceptor with undefined; then await the pending send.
  4. Observed WIRE_WRITES_AFTER_CLOSE = 1.

No repo files changed. This is a deterministic exact-source isolated test, not a full stdio integration suite.

Narrow fix and regression gate: check this._closed again after awaiting interception, before this._wire.send. Add a deferred-interceptor test that closes the channel between the two steps and verifies zero writes to the shared wire. This prevents discarded connection instances from emitting late messages. I did not review the broader admission-completion design end to end.

servrox commented Oct 22, 2026 •

Copy link
Copy Markdown
Author

The post-close stdio write race was fixed in f71cbbb7.

StdioConnectionChannel.send() now rechecks _closed after awaiting outbound interception and before forwarding to the shared transport. Regression tests cover both legacy and modern connections: close the channel while a send is suspended, resume the send, and verify that no additional wire write occurs. Both tests fail without the guard and pass with it.

The PR description contains the current validation results, completion contract, and review status.

Retain processing promises through OpenTelemetry scopes so admission completion still follows owned work and cleanup. Cover ended spans, cancellation, close, asynchronous validation and immediate request-ID reuse. Clarify the completion contract in the documentation and changeset.
@servrox
servrox marked this pull request as ready for review November 3, 2026 12:21
@servrox

servrox commented Nov 3, 2026 •

Copy link
Copy Markdown
Author

The final fixes are in eb54a1da, with fresh green CI. Subscription acknowledgement ordering, unrelated cancellation during stalled writes, and asynchronous client cache completion are covered by regression tests. Both review threads are resolved.

The proposal adds a local completion signal for reusable transport admission. Completion follows SDK-owned processing, response writes, cache invalidation and cleanup; cancellation or connection closure alone does not release unfinished work. The metadata is never serialized. The PR description records the final scope, exact validation results and local environment limitations.

This is ready for maintainer review and ownership. Maintainer edits are enabled. Maintainers are welcome to adjust the API or implementation, split or cherry-pick the changes, and merge through the repository's normal review process. No further sign-off from me is needed.

@servrox
servrox requested a review from a team as a code owner November 3, 2026 12:21
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T20:32:55.484628Z c71cd8c 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c2020035c8

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@servrox
servrox requested a review from karaaslanz November 4, 2026 22:50
@servrox
servrox marked this pull request as draft November 26, 2026 20:27
@servrox
servrox marked this pull request as ready for review November 26, 2026 20:27

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c71cd8cbf7

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@servrox

servrox commented Dec 1, 2026

Copy link
Copy Markdown
Author

Could a maintainer remove my individual review request for karaaslanz? I’d like the TypeScript SDK code-owner team to keep its review request.

I’d also appreciate feedback on the proposed completion contract in #3020. Each inbound message has its own local completion callback. For an input_required response, the SDK finishes the local input handler and hands off the retry before releasing the credit. That lets a transport with a single credit admit the next response. The caller still awaits the final result. Does this boundary fit the SDK’s public API?

@servrox servrox changed the title Expose actual inbound-processing completion for reusable transport admission Expose per-message processing completion to transports Dec 1, 2026

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

v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants