π The fast, Pythonic way to build MCP servers and clients.
Fix non-serializable state lost between middleware and tools (#3234)
Jeremiah Lowin3f2090f10a72
diff --git a/docs/python-sdk/fastmcp-server-context.mdx b/docs/python-sdk/fastmcp-server-context.mdxindex 56fa9f91d..74b8fb40b 100644--- a/docs/python-sdk/fastmcp-server-context.mdx+++ b/docs/python-sdk/fastmcp-server-context.mdx@@ -132,7 +132,7 @@ fastmcp(self) -> FastMCPGet the FastMCP instance.-#### `request_context` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L290" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `request_context` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L296" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonrequest_context(self) -> RequestContext[ServerSession, Any, Request] | None@@ -161,7 +161,7 @@ async def on_request(self, context, call_next):```-#### `lifespan_context` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L319" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `lifespan_context` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L325" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonlifespan_context(self) -> dict[str, Any]@@ -188,7 +188,7 @@ def my_tool(ctx: Context) -> str:```-#### `report_progress` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L350" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `report_progress` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L356" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonreport_progress(self, progress: float, total: float | None = None, message: str | None = None) -> None@@ -205,7 +205,7 @@ Works in both foreground (MCP progress notifications) and background- `message`: Optional status message describing current progress-#### `list_resources` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L444" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `list_resources` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L450" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonlist_resources(self) -> list[SDKResource]@@ -217,7 +217,7 @@ List all available resources from the server.- List of Resource objects available on the server-#### `list_prompts` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L460" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `list_prompts` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L466" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonlist_prompts(self) -> list[SDKPrompt]@@ -229,7 +229,7 @@ List all available prompts from the server.- List of Prompt objects available on the server-#### `get_prompt` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L476" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `get_prompt` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L482" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonget_prompt(self, name: str, arguments: dict[str, Any] | None = None) -> GetPromptResult@@ -245,7 +245,7 @@ Get a prompt by name with optional arguments.- The prompt result-#### `read_resource` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L495" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `read_resource` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L501" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonread_resource(self, uri: str | AnyUrl) -> ResourceResult@@ -260,7 +260,7 @@ Read a resource by URI.- ResourceResult with contents-#### `log` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L511" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `log` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L517" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonlog(self, message: str, level: LoggingLevel | None = None, logger_name: str | None = None, extra: Mapping[str, Any] | None = None) -> None@@ -278,7 +278,7 @@ Messages sent to Clients are also logged to the `fastmcp.server.context.to_clien- `extra`: Optional mapping for additional arguments-#### `transport` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L540" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `transport` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L546" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythontransport(self) -> TransportType | None@@ -290,7 +290,7 @@ Returns the transport type used to run this server: "stdio", "sse",or "streamable-http". Returns None if called outside of a server context.-#### `client_supports_extension` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L548" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `client_supports_extension` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L554" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonclient_supports_extension(self, extension_id: str) -> bool@@ -315,7 +315,7 @@ Example::return "text-only client"-#### `client_id` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L576" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `client_id` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L582" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonclient_id(self) -> str | None@@ -324,7 +324,7 @@ client_id(self) -> str | NoneGet the client ID if available.-#### `request_id` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L585" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `request_id` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L591" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonrequest_id(self) -> str@@ -335,7 +335,7 @@ Get the unique ID for this request.Raises RuntimeError if MCP request context is not available.-#### `session_id` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L598" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `session_id` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L604" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonsession_id(self) -> str@@ -352,7 +352,7 @@ the same client session.- for other transports.-#### `session` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L655" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `session` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L661" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonsession(self) -> ServerSession@@ -366,7 +366,7 @@ In background task mode: Returns the session stored at Context creation.Raises RuntimeError if no session is available.-#### `debug` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L681" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `debug` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L687" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythondebug(self, message: str, logger_name: str | None = None, extra: Mapping[str, Any] | None = None) -> None@@ -377,7 +377,7 @@ Send a `DEBUG`-level message to the connected MCP Client.Messages sent to Clients are also logged to the `fastmcp.server.context.to_client` logger with a level of `DEBUG`.-#### `info` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L697" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `info` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L703" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythoninfo(self, message: str, logger_name: str | None = None, extra: Mapping[str, Any] | None = None) -> None@@ -388,7 +388,7 @@ Send a `INFO`-level message to the connected MCP Client.Messages sent to Clients are also logged to the `fastmcp.server.context.to_client` logger with a level of `DEBUG`.-#### `warning` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L713" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `warning` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L719" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonwarning(self, message: str, logger_name: str | None = None, extra: Mapping[str, Any] | None = None) -> None@@ -399,7 +399,7 @@ Send a `WARNING`-level message to the connected MCP Client.Messages sent to Clients are also logged to the `fastmcp.server.context.to_client` logger with a level of `DEBUG`.-#### `error` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L729" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `error` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L735" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonerror(self, message: str, logger_name: str | None = None, extra: Mapping[str, Any] | None = None) -> None@@ -410,7 +410,7 @@ Send a `ERROR`-level message to the connected MCP Client.Messages sent to Clients are also logged to the `fastmcp.server.context.to_client` logger with a level of `DEBUG`.-#### `list_roots` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L745" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `list_roots` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L751" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonlist_roots(self) -> list[Root]@@ -419,7 +419,7 @@ list_roots(self) -> list[Root]List the roots available to the server, as indicated by the client.-#### `send_notification` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L750" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `send_notification` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L756" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonsend_notification(self, notification: mcp.types.ServerNotificationType) -> None@@ -431,7 +431,7 @@ Send a notification to the client immediately.- `notification`: An MCP notification instance (e.g., ToolListChangedNotification())-#### `close_sse_stream` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L760" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `close_sse_stream` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L766" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonclose_sse_stream(self) -> None@@ -449,7 +449,7 @@ Instead of holding a connection open for minutes, you can periodically closeand let the client reconnect.-#### `sample_step` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L799" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `sample_step` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L805" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonsample_step(self, messages: str | Sequence[str | SamplingMessage]) -> SampleStep@@ -492,7 +492,7 @@ regardless of this setting.- - .text: The text content (if any)-#### `sample` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L878" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `sample` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L884" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonsample(self, messages: str | Sequence[str | SamplingMessage]) -> SamplingResult[ResultT]@@ -501,7 +501,7 @@ sample(self, messages: str | Sequence[str | SamplingMessage]) -> SamplingResult[Overload: With result_type, returns SamplingResult[ResultT].-#### `sample` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L894" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `sample` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L900" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonsample(self, messages: str | Sequence[str | SamplingMessage]) -> SamplingResult[str]@@ -510,7 +510,7 @@ sample(self, messages: str | Sequence[str | SamplingMessage]) -> SamplingResult[Overload: Without result_type, returns SamplingResult[str].-#### `sample` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L909" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `sample` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L915" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonsample(self, messages: str | Sequence[str | SamplingMessage]) -> SamplingResult[ResultT] | SamplingResult[str]@@ -558,43 +558,43 @@ regardless of this setting.- - .history: All messages exchanged during sampling-#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L984" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L990" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonelicit(self, message: str, response_type: None) -> AcceptedElicitation[dict[str, Any]] | DeclinedElicitation | CancelledElicitation```-#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L996" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1002" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonelicit(self, message: str, response_type: type[T]) -> AcceptedElicitation[T] | DeclinedElicitation | CancelledElicitation```-#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1006" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1012" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonelicit(self, message: str, response_type: list[str]) -> AcceptedElicitation[str] | DeclinedElicitation | CancelledElicitation```-#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1016" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1022" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonelicit(self, message: str, response_type: dict[str, dict[str, str]]) -> AcceptedElicitation[str] | DeclinedElicitation | CancelledElicitation```-#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1026" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1032" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonelicit(self, message: str, response_type: list[list[str]]) -> AcceptedElicitation[list[str]] | DeclinedElicitation | CancelledElicitation```-#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1038" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1044" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonelicit(self, message: str, response_type: list[dict[str, dict[str, str]]]) -> AcceptedElicitation[list[str]] | DeclinedElicitation | CancelledElicitation```-#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1050" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `elicit` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1056" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonelicit(self, message: str, response_type: type[T] | list[str] | dict[str, dict[str, str]] | list[list[str]] | list[dict[str, dict[str, str]]] | None = None) -> AcceptedElicitation[T] | AcceptedElicitation[dict[str, Any]] | AcceptedElicitation[str] | AcceptedElicitation[list[str]] | DeclinedElicitation | CancelledElicitation@@ -623,7 +623,7 @@ type or dataclass or BaseModel. If it is a primitive type, anobject schema with a single "value" field will be generated.-#### `set_state` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1164" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `set_state` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1170" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonset_state(self, key: str, value: Any) -> None@@ -644,7 +644,7 @@ requests.The key is automatically prefixed with the session identifier.-#### `get_state` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1206" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `get_state` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1212" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonget_state(self, key: str) -> Any@@ -658,7 +658,7 @@ then falls back to the session-scoped state store.Returns None if the key is not found.-#### `delete_state` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1220" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `delete_state` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1226" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythondelete_state(self, key: str) -> None@@ -669,7 +669,7 @@ Delete a value from the state store.Removes from both request-scoped and session-scoped stores.-#### `enable_components` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1241" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `enable_components` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1247" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonenable_components(self) -> None@@ -693,7 +693,7 @@ ResourceListChangedNotification, and PromptListChangedNotification.- `match_all`: If True, matches all components regardless of other criteria.-#### `disable_components` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1279" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `disable_components` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1285" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythondisable_components(self) -> None@@ -717,7 +717,7 @@ ResourceListChangedNotification, and PromptListChangedNotification.- `match_all`: If True, matches all components regardless of other criteria.-#### `reset_visibility` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1317" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `reset_visibility` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/context.py#L1323" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonreset_visibility(self) -> Nonediff --git a/src/fastmcp/server/context.py b/src/fastmcp/server/context.pyindex c17e13f03..4cf8b7050 100644--- a/src/fastmcp/server/context.py+++ b/src/fastmcp/server/context.py@@ -237,6 +237,12 @@ class Context:async def __aenter__(self) -> Context:"""Enter the context manager and set this context as the current context."""+ # Inherit request-scoped state from parent context so middleware+ # and tool contexts share the same in-memory state dict.+ parent = _current_context.get(None)+ if parent is not None:+ self._request_state = parent._request_state+# Always set this context and save the tokentoken = _current_context.set(self)self._tokens.append(token)diff --git a/tests/server/middleware/test_middleware.py b/tests/server/middleware/test_middleware.pyindex 3c1a7993f..3bee43b19 100644--- a/tests/server/middleware/test_middleware.py+++ b/tests/server/middleware/test_middleware.py@@ -1068,3 +1068,62 @@ class TestToolCallDenial:# Verify both admin tools were deniedassert denied_tools == {"admin_delete", "admin_config"}+++class TestMiddlewareRequestState:+ """Non-serializable state set in middleware must be visible to tools/resources.++ Regression test for https://github.com/PrefectHQ/fastmcp/issues/3228.+ """++ async def test_non_serializable_state_from_middleware_visible_in_tool(self):+ server = FastMCP("test")++ sentinel = object()++ class StateMiddleware(Middleware):+ async def on_call_tool(+ self, context: MiddlewareContext, call_next: CallNext+ ) -> Any:+ assert context.fastmcp_context is not None+ await context.fastmcp_context.set_state(+ "obj", sentinel, serializable=False+ )+ return await call_next(context)++ server.add_middleware(StateMiddleware())++ @server.tool()+ async def read_it(ctx: Context) -> str:+ val = await ctx.get_state("obj")+ return "found" if val is sentinel else "missing"++ async with Client(server) as client:+ result = await client.call_tool("read_it")+ assert result.content[0].text == "found"++ async def test_non_serializable_state_from_middleware_visible_in_resource(self):+ server = FastMCP("test")++ sentinel = object()++ class StateMiddleware(Middleware):+ async def on_read_resource(+ self, context: MiddlewareContext, call_next: CallNext+ ) -> Any:+ assert context.fastmcp_context is not None+ await context.fastmcp_context.set_state(+ "obj", sentinel, serializable=False+ )+ return await call_next(context)++ server.add_middleware(StateMiddleware())++ @server.resource("test://data")+ async def read_it(ctx: Context) -> str:+ val = await ctx.get_state("obj")+ return "found" if val is sentinel else "missing"++ async with Client(server) as client:+ result = await client.read_resource("test://data")+ assert result[0].text == "found"