π The fast, Pythonic way to build MCP servers and clients.
Fix stale request context in StatefulProxyClient handlers (#3172)
Jeremiah Lowin5f941be5a31d
diff --git a/docs/python-sdk/fastmcp-server-providers-proxy.mdx b/docs/python-sdk/fastmcp-server-providers-proxy.mdxindex 803adb973..50c105bcf 100644--- a/docs/python-sdk/fastmcp-server-providers-proxy.mdx+++ b/docs/python-sdk/fastmcp-server-providers-proxy.mdx@@ -15,7 +15,7 @@ classes that forward execution to remote servers.## Functions-### `default_proxy_roots_handler` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L713" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `default_proxy_roots_handler` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L720" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythondefault_proxy_roots_handler(context: RequestContext[ClientSession, LifespanContextT]) -> RootsList@@ -25,7 +25,7 @@ default_proxy_roots_handler(context: RequestContext[ClientSession, LifespanConteForward list roots request from remote server to proxy's connected clients.-### `default_proxy_sampling_handler` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L721" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `default_proxy_sampling_handler` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L728" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythondefault_proxy_sampling_handler(messages: list[mcp.types.SamplingMessage], params: mcp.types.CreateMessageRequestParams, context: RequestContext[ClientSession, LifespanContextT]) -> mcp.types.CreateMessageResult@@ -35,7 +35,7 @@ default_proxy_sampling_handler(messages: list[mcp.types.SamplingMessage], paramsForward sampling request from remote server to proxy's connected clients.-### `default_proxy_elicitation_handler` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L744" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `default_proxy_elicitation_handler` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L751" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythondefault_proxy_elicitation_handler(message: str, response_type: type, params: mcp.types.ElicitRequestParams, context: RequestContext[ClientSession, LifespanContextT]) -> ElicitResult@@ -45,7 +45,7 @@ default_proxy_elicitation_handler(message: str, response_type: type, params: mcpForward elicitation request from remote server to proxy's connected clients.-### `default_proxy_log_handler` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L766" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `default_proxy_log_handler` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L773" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythondefault_proxy_log_handler(message: LogMessage) -> None@@ -55,7 +55,7 @@ default_proxy_log_handler(message: LogMessage) -> NoneForward log notification from remote server to proxy's connected clients.-### `default_proxy_progress_handler` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L774" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `default_proxy_progress_handler` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L781" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythondefault_proxy_progress_handler(progress: float, total: float | None, message: str | None) -> None@@ -67,7 +67,7 @@ Forward progress notification from remote server to proxy's connected clients.## Classes-### `ProxyTool` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L66" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `ProxyTool` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L67" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>A Tool that represents and executes a tool on a remote server.@@ -75,7 +75,7 @@ A Tool that represents and executes a tool on a remote server.**Methods:**-#### `model_copy` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L83" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `model_copy` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L84" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonmodel_copy(self, **kwargs: Any) -> ProxyTool@@ -84,7 +84,7 @@ model_copy(self, **kwargs: Any) -> ProxyToolOverride to preserve _backend_name when name changes.-#### `from_mcp_tool` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L93" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `from_mcp_tool` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L94" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonfrom_mcp_tool(cls, client_factory: ClientFactoryT, mcp_tool: mcp.types.Tool) -> ProxyTool@@ -93,7 +93,7 @@ from_mcp_tool(cls, client_factory: ClientFactoryT, mcp_tool: mcp.types.Tool) ->Factory method to create a ProxyTool from a raw MCP tool schema.-#### `run` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L110" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `run` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L111" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonrun(self, arguments: dict[str, Any], context: Context | None = None) -> ToolResult@@ -102,13 +102,13 @@ run(self, arguments: dict[str, Any], context: Context | None = None) -> ToolResuExecutes the tool by making a call through the client.-#### `get_span_attributes` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L156" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `get_span_attributes` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L163" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonget_span_attributes(self) -> dict[str, Any]```-### `ProxyResource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L163" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `ProxyResource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L170" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>A Resource that represents and reads a resource from a remote server.@@ -116,7 +116,7 @@ A Resource that represents and reads a resource from a remote server.**Methods:**-#### `model_copy` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L188" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `model_copy` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L195" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonmodel_copy(self, **kwargs: Any) -> ProxyResource@@ -125,7 +125,7 @@ model_copy(self, **kwargs: Any) -> ProxyResourceOverride to preserve _backend_uri when uri changes.-#### `from_mcp_resource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L198" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `from_mcp_resource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L205" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonfrom_mcp_resource(cls, client_factory: ClientFactoryT, mcp_resource: mcp.types.Resource) -> ProxyResource@@ -134,7 +134,7 @@ from_mcp_resource(cls, client_factory: ClientFactoryT, mcp_resource: mcp.types.RFactory method to create a ProxyResource from a raw MCP resource schema.-#### `read` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L218" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `read` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L225" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonread(self) -> ResourceResult@@ -143,13 +143,13 @@ read(self) -> ResourceResultRead the resource content from the remote server.-#### `get_span_attributes` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L263" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `get_span_attributes` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L270" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonget_span_attributes(self) -> dict[str, Any]```-### `ProxyTemplate` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L270" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `ProxyTemplate` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L277" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>A ResourceTemplate that represents and creates resources from a remote server template.@@ -157,7 +157,7 @@ A ResourceTemplate that represents and creates resources from a remote server te**Methods:**-#### `model_copy` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L287" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `model_copy` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L294" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonmodel_copy(self, **kwargs: Any) -> ProxyTemplate@@ -166,7 +166,7 @@ model_copy(self, **kwargs: Any) -> ProxyTemplateOverride to preserve _backend_uri_template when uri_template changes.-#### `from_mcp_template` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L297" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `from_mcp_template` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L304" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonfrom_mcp_template(cls, client_factory: ClientFactoryT, mcp_template: mcp.types.ResourceTemplate) -> ProxyTemplate@@ -175,7 +175,7 @@ from_mcp_template(cls, client_factory: ClientFactoryT, mcp_template: mcp.types.RFactory method to create a ProxyTemplate from a raw MCP template schema.-#### `create_resource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L316" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `create_resource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L323" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythoncreate_resource(self, uri: str, params: dict[str, Any], context: Context | None = None) -> ProxyResource@@ -184,13 +184,13 @@ create_resource(self, uri: str, params: dict[str, Any], context: Context | NoneCreate a resource from the template by calling the remote server.-#### `get_span_attributes` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L378" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `get_span_attributes` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L385" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonget_span_attributes(self) -> dict[str, Any]```-### `ProxyPrompt` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L385" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `ProxyPrompt` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L392" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>A Prompt that represents and renders a prompt from a remote server.@@ -198,7 +198,7 @@ A Prompt that represents and renders a prompt from a remote server.**Methods:**-#### `model_copy` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L402" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `model_copy` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L409" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonmodel_copy(self, **kwargs: Any) -> ProxyPrompt@@ -207,7 +207,7 @@ model_copy(self, **kwargs: Any) -> ProxyPromptOverride to preserve _backend_name when name changes.-#### `from_mcp_prompt` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L412" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `from_mcp_prompt` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L419" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonfrom_mcp_prompt(cls, client_factory: ClientFactoryT, mcp_prompt: mcp.types.Prompt) -> ProxyPrompt@@ -216,7 +216,7 @@ from_mcp_prompt(cls, client_factory: ClientFactoryT, mcp_prompt: mcp.types.PrompFactory method to create a ProxyPrompt from a raw MCP prompt schema.-#### `render` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L436" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `render` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L443" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonrender(self, arguments: dict[str, Any]) -> PromptResult@@ -225,13 +225,13 @@ render(self, arguments: dict[str, Any]) -> PromptResultRender the prompt by making a call through the client.-#### `get_span_attributes` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L458" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `get_span_attributes` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L465" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonget_span_attributes(self) -> dict[str, Any]```-### `ProxyProvider` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L470" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `ProxyProvider` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L477" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Provider that proxies to a remote MCP server via a client factory.@@ -245,7 +245,7 @@ because tasks cannot be executed through a proxy.**Methods:**-#### `get_tasks` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L595" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `get_tasks` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L602" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonget_tasks(self) -> Sequence[FastMCPComponent]@@ -258,7 +258,7 @@ server lifespan initialization, which would open the client before anycontext is set. All Proxy* components have task_config.mode="forbidden".-### `FastMCPProxy` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L666" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `FastMCPProxy` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L673" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>A FastMCP server that acts as a proxy to a remote MCP-compliant server.@@ -267,7 +267,7 @@ This is a convenience wrapper that creates a FastMCP server with aProxyProvider. For more control, use FastMCP with add_provider(ProxyProvider(...)).-### `ProxyClient` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L784" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `ProxyClient` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L829" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>A proxy client that forwards advanced interactions between a remote MCP server and the proxy's connected clients.@@ -275,7 +275,7 @@ A proxy client that forwards advanced interactions between a remote MCP server aSupports forwarding roots, sampling, elicitation, logging, and progress.-### `StatefulProxyClient` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L821" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `StatefulProxyClient` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L862" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>A proxy client that provides a stateful client factory for the proxy server.@@ -286,10 +286,17 @@ And it will be disconnected when the session is exited.This is useful to proxy a stateful mcp server such as the Playwright MCP server.Note that it is essential to ensure that the proxy server itself is also stateful.+Because session reuse means the receive-loop task inherits a stale+``request_ctx`` ContextVar snapshot, the default proxy handlers are+replaced with versions that restore the ContextVar before forwarding.+``ProxyTool.run`` stashes the current ``RequestContext`` in+``_proxy_rc_ref`` before each backend call, and the handlers consult+it to detect (and correct) staleness.+**Methods:**-#### `clear` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L841" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `clear` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L912" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonclear(self)@@ -298,7 +305,7 @@ clear(self)Clear all cached clients and force disconnect them.-#### `new_stateful` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L847" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `new_stateful` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/proxy.py#L918" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonnew_stateful(self) -> Client[ClientTransportT]diff --git a/src/fastmcp/server/providers/proxy.py b/src/fastmcp/server/providers/proxy.pyindex d06337396..0b7ce0096 100644--- a/src/fastmcp/server/providers/proxy.py+++ b/src/fastmcp/server/providers/proxy.py@@ -16,6 +16,7 @@ from urllib.parse import quoteimport mcp.typesfrom mcp import ServerSessionfrom mcp.client.session import ClientSession+from mcp.server.lowlevel.server import request_ctxfrom mcp.shared.context import LifespanContextT, RequestContextfrom mcp.shared.exceptions import McpErrorfrom mcp.types import (@@ -121,6 +122,12 @@ class ProxyTool(Tool):client = await self._get_client()async with client:ctx = context or get_context()+ # StatefulProxyClient reuses sessions across requests, so+ # its receive-loop task has stale ContextVars from the first+ # request. Stash the current RequestContext in the shared+ # ref so handlers can restore it before forwarding.+ if isinstance(client, StatefulProxyClient):+ cast(list[Any], client._proxy_rc_ref)[0] = ctx.request_context# Build meta dict from request contextmeta: dict[str, Any] | None = Noneif hasattr(ctx, "request_context"):@@ -781,16 +788,50 @@ async def default_proxy_progress_handler(await ctx.report_progress(progress, total, message)+def _restore_request_context(+ rc_ref: list[Any],+) -> None:+ """Set the ``request_ctx`` ContextVar from a stashed RequestContext.++ Called at the start of proxy handler invocations in+ ``StatefulProxyClient`` to fix stale ContextVars in the receive-loop+ task. Only overrides when the ContextVar is genuinely stale (same+ session, different request_id) to avoid corrupting the concurrent+ case where multiple sessions share the same ref via ``copy.copy``.+ """+ rc = rc_ref[0]+ if rc is None:+ return+ try:+ current_rc = request_ctx.get()+ except LookupError:+ request_ctx.set(rc)+ return+ if current_rc.session is rc.session and current_rc.request_id != rc.request_id:+ request_ctx.set(rc)+++def _make_restoring_handler(handler: Callable, rc_ref: list[Any]) -> Callable:+ """Wrap a proxy handler to restore request_ctx before delegating.++ The wrapper is a plain ``async def`` so it passes+ ``inspect.isfunction()`` checks in handler registration paths+ (e.g., ``create_roots_callback``).+ """++ async def wrapper(*args: Any, **kwargs: Any) -> Any:+ _restore_request_context(rc_ref)+ return await handler(*args, **kwargs)++ return wrapper++class ProxyClient(Client[ClientTransportT]):"""A proxy client that forwards advanced interactions between a remote MCP server and the proxy's connected clients.Supports forwarding roots, sampling, elicitation, logging, and progress."""- # Stored context for handlers when contextvar isn't available- # (e.g., when receive loop was started before any request context)- _proxy_context: Context | None = None-def __init__(self,transport: ClientTransportT@@ -826,9 +867,39 @@ class StatefulProxyClient(ProxyClient[ClientTransportT]):This is useful to proxy a stateful mcp server such as the Playwright MCP server.Note that it is essential to ensure that the proxy server itself is also stateful.++ Because session reuse means the receive-loop task inherits a stale+ ``request_ctx`` ContextVar snapshot, the default proxy handlers are+ replaced with versions that restore the ContextVar before forwarding.+ ``ProxyTool.run`` stashes the current ``RequestContext`` in+ ``_proxy_rc_ref`` before each backend call, and the handlers consult+ it to detect (and correct) staleness."""+ # Mutable list shared across copies (Client.new() uses copy.copy,+ # which preserves references to mutable containers). ProxyTool.run+ # writes [0] before each backend call; handlers read it to detect+ # stale ContextVars and restore the correct request_ctx.+ #+ # We store the concrete RequestContext (not fastmcp's Context) because+ # Context properties are themselves ContextVar-dependent and resolve+ # in the caller's async context β which is stale in the receive loop.+ _proxy_rc_ref: list[Any]+def __init__(self, *args: Any, **kwargs: Any):+ # Install context-restoring handler wrappers BEFORE super().__init__+ # registers them with the Client's session kwargs.+ self._proxy_rc_ref = [None]+ for key, default_fn in (+ ("roots", default_proxy_roots_handler),+ ("sampling_handler", default_proxy_sampling_handler),+ ("elicitation_handler", default_proxy_elicitation_handler),+ ("log_handler", default_proxy_log_handler),+ ("progress_handler", default_proxy_progress_handler),+ ):+ if key not in kwargs:+ kwargs[key] = _make_restoring_handler(default_fn, self._proxy_rc_ref)+super().__init__(*args, **kwargs)self._caches: dict[ServerSession, Client[ClientTransportT]] = {}diff --git a/tests/server/providers/proxy/test_stateful_proxy_client.py b/tests/server/providers/proxy/test_stateful_proxy_client.pyindex 8ae0ce713..79a280e72 100644--- a/tests/server/providers/proxy/test_stateful_proxy_client.py+++ b/tests/server/providers/proxy/test_stateful_proxy_client.py@@ -1,15 +1,18 @@import asyncio+from dataclasses import dataclassimport pytestfrom anyio import create_task_groupfrom mcp.types import LoggingLevelfrom fastmcp import Client, Context, FastMCP+from fastmcp.client.elicitation import ElicitResultfrom fastmcp.client.logging import LogMessagefrom fastmcp.client.transports import FastMCPTransportfrom fastmcp.exceptions import ToolError+from fastmcp.server.elicitation import AcceptedElicitationfrom fastmcp.server.providers.proxy import FastMCPProxy, StatefulProxyClient-from fastmcp.utilities.tests import find_available_port+from fastmcp.utilities.tests import find_available_port, run_server_async@pytest.fixture@@ -145,3 +148,54 @@ class TestStatefulProxyClient:result_b = await client.call_tool("b_tool_b", {})assert result_a.data == "a"assert result_b.data == "b"++ @pytest.mark.timeout(10)+ async def test_stateful_proxy_elicitation_over_http(self):+ """Elicitation through a stateful proxy over HTTP must not hang.++ When StatefulProxyClient reuses a session, the receive-loop task+ inherits a stale request_ctx ContextVar from the first request.+ The streamable-HTTP transport uses related_request_id to route+ server-initiated messages (like elicitation) back to the correct+ HTTP response stream. A stale request_id routes to a closed+ stream, causing the elicitation to hang forever.++ This test runs the proxy over HTTP (not in-process) so the+ transport's related_request_id routing is exercised.+ """++ @dataclass+ class Person:+ name: str++ backend = FastMCP("backend")++ @backend.tool+ async def ask_name(ctx: Context) -> str:+ result = await ctx.elicit("What is your name?", response_type=Person)+ if isinstance(result, AcceptedElicitation):+ assert isinstance(result.data, Person)+ return f"Hello, {result.data.name}!"+ return "declined"++ stateful_client = StatefulProxyClient(backend)+ proxy = FastMCPProxy(+ client_factory=stateful_client.new_stateful,+ name="proxy",+ )++ async def elicitation_handler(message, response_type, params, ctx):+ return ElicitResult(action="accept", content=response_type(name="Alice"))++ # Run the proxy over HTTP so the transport uses+ # related_request_id routing for server-initiated messages.+ async with run_server_async(proxy) as proxy_url:+ async with Client(+ proxy_url, elicitation_handler=elicitation_handler+ ) as client:+ result1 = await client.call_tool("ask_name", {})+ assert result1.data == "Hello, Alice!"+ # Second call reuses the stateful session β this is the+ # one that would hang without the fix.+ result2 = await client.call_tool("ask_name", {})+ assert result2.data == "Hello, Alice!"