Sitelet https://github.com/modelcontextprotocol/python-sdk/pull/3390
Skip to content

fix(server): expose request cancellation on MCPServer context - #3390

Closed
mohitrai810 wants to merge 1 commit into
modelcontextprotocol:mainfrom
mohitrai810:fix-expose-context-cancellation
Closed

fix(server): expose request cancellation on MCPServer context#3390
mohitrai810 wants to merge 1 commit into
modelcontextprotocol:mainfrom
mohitrai810:fix-expose-context-cancellation

Conversation

@mohitrai810

@mohitrai810 mohitrai810 commented Aug 25, 2026

Copy link
Copy Markdown

Fixes #3389.

Problem

The high-level MCPServer Context does not expose the cancellation event available in the underlying dispatch context. Tool handlers therefore cannot observe cancellation or perform cooperative cleanup through the public API.

Changes

  • Add cancel_requested to ServerRequestContext.
  • Forward the dispatcher's existing cancellation event when constructing the request context.
  • Expose cancel_requested on the high-level Context.
  • Preserve the existing error when context is accessed outside a request.
  • Add regression tests for cancellation access and access outside a request.

Testing

  • All 168 tests passed:
    python -m pytest tests/server/mcpserver/test_server.py -v --inline-snapshot=disable
  • Ruff linting passed.
  • Ruff formatting checks passed.

AI assistance disclosure: I used AI to help investigate and implement this change. I reviewed the implementation and verified all tests myself.

@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Aug 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the contribution. This repository only keeps pull requests open when they're linked to an issue that a maintainer has assigned to the author — CONTRIBUTING.md explains why and how we work. This PR has been closed for now because you aren't currently assigned to #3389.

If a maintainer would like this change as a PR from you, they'll assign you to #3389 and this PR will reopen automatically — there's nothing more you need to do. (If you opened the issue, this PR already shows up on its timeline.)

There's no need to open a new PR — this one will be reopened. While it's closed, please push any updates as new commits rather than force-pushing, since GitHub can't reopen a PR whose branch has been rewritten.

Maintainers: reopening this PR, removing the missing-issue-link label, or adding bypass-issue-check bypasses the check.

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

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v2] Expose request cancellation on MCPServer tool Context

1 participant