Skip to content

fix(handler): keep RequestContext meta on extract - #1583

Open
orangeCatDeveloper wants to merge 1 commit into
modelcontextprotocol:mainfrom
orangeCatDeveloper:fix/request-meta-extractor-clone
Open

orangeCatDeveloper wants to merge 1 commit into
modelcontextprotocol:mainfrom
orangeCatDeveloper:fix/request-meta-extractor-clone

Conversation

@orangeCatDeveloper

Copy link
Copy Markdown

Fixes #1580

A tool or prompt handler that takes RequestMetaObject before RequestContext sees empty ctx.meta: no progress token, and no client info or capabilities on sessionless requests.

Motivation and Context

The RequestMetaObject extractor mem::swaps the meta out of the RequestContext, so every extractor that runs after it reads a default meta. The fix clones it, as the other RequestContext-derived extractors (RequestContext, Peer, Extensions, RequestId, CancellationToken) already do. It is shared by the tool and prompt routers, so both are fixed.

How Has This Been Tested?

  • Automated unit / integration tests
  • Tested manually
  • Tested in a real application

A tool probe(meta: RequestMetaObject, ctx: RequestContext<RoleServer>) called from an rmcp client over a duplex transport, printing both progress tokens:

main:   meta=Some(ProgressToken(Number(0))) ctx=None

branch: meta=Some(ProgressToken(Number(0))) ctx=Some(ProgressToken(Number(0)))

test_meta_extractor_leaves_request_context_meta_intact drives the same parameter order through ToolRouter; it fails on main.

Breaking Changes

None. as_request_context_mut is no longer called but stays, since it is a public trait method.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Test update (non-breaking change which adds or updates tests)
  • Refactoring (non-breaking change which makes things better)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • CI/build script update (update CI workflow or build scripts)

Checklist

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

The RequestMetaObject extractor moved the meta out of the
RequestContext, so a RequestContext parameter listed after it saw empty
meta: no progress token, and no client info or capabilities on
sessionless requests.
@orangeCatDeveloper
orangeCatDeveloper requested a review from a team as a code owner December 6, 2026 07:35
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 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-12-06T07:37:01.187523Z 7cca723 PR opened
ℹ️ 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.

@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 7cca723c (base 3725772), not from the description.

Red → green, using this PR's own test
I split the change so only the test moves: at base 3725772 with common.rs untouched (mem::swap still in place) and tool.rs taken from this branch, the new test fails with

left:  "Some(ProgressToken(Number(7))) None"
right: "Some(ProgressToken(Number(7))) Some(ProgressToken(Number(7)))"

At head the same test passes (cargo test -p rmcp --all-features test_meta_extractor_leaves_request_context_meta_intact → 1 passed, 967 filtered).

So the diagnosis in #1580 is correct: the extractor mutates the context instead of reading it.

Gates at head

  • cargo clippy -p rmcp --all-features --all-targets -- -D warnings → clean (54s).
  • cargo fmt --check -p rmcp → clean on stable. Note the repo's rustfmt config uses imports_granularity/group_imports, which stable cannot enforce, so nightly fmt is still unverified on my side.
  • No checks are reported on the branch, so CI has not run on this head either.

Consistency
The new body matches the other five FromContextPart impls in crates/rmcp/src/handler/server/common.rs (RequestContext, CancellationToken, Extensions, Extension, Peer, RequestId), and grep finds no remaining destructive read of RequestContext::meta (meta.take() / mem::take) in the tree. Keeping as_request_context_mut is right; removing it would be a breaking trait change.

One request before I approve
The test pins only the progress token, while the issue also claims that sessionless requests lose client info and capabilities. Those live in the same RequestMetaObject, so the clone covers them, but nothing in the suite would notice if a later refactor special-cased the token. Please either build the test meta with RequestMetaObject::with_client_context(protocol_version, client_info, client_capabilities) and assert client_info/client_capabilities survive after extraction, or trim that half of the issue text. A one-line assertion on ctx.meta.client_info() is enough.

Not requesting changes on anything else; the fix itself is correct.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RequestMetaObject extractor empties RequestContext::meta for later parameters

2 participants