refactor: remove request_ctx ContextVar, thread Context explicitly#2203
Open
refactor: remove request_ctx ContextVar, thread Context explicitly#2203
Conversation
The request_ctx ContextVar in mcp.server.lowlevel.server was redundant with the ServerRequestContext already passed as the first argument to every _handle_* method. This removes the ContextVar entirely and threads Context explicitly. Changes: - MCPServer.get_context() removed — use ctx: Context parameter injection in tool/resource/prompt functions instead - MCPServer.call_tool/read_resource/get_prompt now accept an optional context: Context | None = None parameter; _handle_* methods construct the Context at the lowlevel boundary and pass it through - Context class moved from server.py to its own context.py module (still re-exported from mcp.server.mcpserver) - Tool.run/Prompt.render/ResourceTemplate.create_resource now raise a clear error if the registered function requires a Context but none was provided, instead of silently injecting None Github-Issue: #2112 Github-Issue: #1684
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Removes the
request_ctxContextVar and threadsContextexplicitly through the MCPServer request-handling chain.Motivation and Context
The
request_ctxContextVar inmcp.server.lowlevel.serverwas redundant: the lowlevel server already passesServerRequestContextas the first argument to every_handle_*method. The ContextVar was a second mechanism carrying the same value, used only byMCPServer.get_context().This PR removes the ContextVar entirely, making the data flow explicit.
_handle_*methods construct the high-levelContextat the lowlevel→MCPServer boundary and pass it through.Part of #2112 (context refactor) / #1684 (contextvars cleanup).
How Has This Been Tested?
Tool.run(),Prompt.render(),ResourceTemplate.create_resource()uv run --frozen pyright src/ tests/— 0 errorsuv run --frozen ruff check— cleanBreaking Changes
Yes — documented in
docs/migration.md:MCPServer.get_context()removed — usectx: Contextparameter injection in tool/resource/prompt functions instead (the existing recommended pattern)request_ctxContextVar removed frommcp.server.lowlevel.server— code importing it directly will need to use parameter injectionMCPServer.call_tool(),read_resource(),get_prompt()now accept an optionalcontext: Context | None = Noneparameter — backward-compatible for callers that don't pass itctx: Contextparameter but is called withcontext=None, a clear error is raised (ToolErrorfor tools,ValueErrorfor prompts/resource templates). PreviouslyNonewas silently injected.Types of changes
Checklist
Additional context
Contextclass moved frommcp.server.mcpserver.servertomcp.server.mcpserver.context. The public import path (from mcp.server.mcpserver import Context) is unchanged via__init__.pyre-export. The old direct-module path (from mcp.server.mcpserver.server import Context) also still works becauseserver.pyimportsContextat module level.Exception hierarchy cleanup (making prompts/resources raise
MCPServerErrorsubclasses instead ofValueError) was considered but deferred to #1742 (error taxonomy) to avoid scope creep — see that issue for the full analysis of the existing inconsistencies.AI Disclaimer