fix(server): expose request cancellation on MCPServer context by mohitrai810 · Pull Request #3390 · modelcontextprotocol/python-sdk · GitHub
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

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

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