π The fast, Pythonic way to build MCP servers and clients.
Fix NameError with future annotations and Context/Depends parameters (#3243)
Jeremiah Lowinea1328660e70
diff --git a/docs/python-sdk/fastmcp-server-dependencies.mdx b/docs/python-sdk/fastmcp-server-dependencies.mdxindex eb1cbaa8e..88a756ebc 100644--- a/docs/python-sdk/fastmcp-server-dependencies.mdx+++ b/docs/python-sdk/fastmcp-server-dependencies.mdx@@ -213,7 +213,7 @@ Handles:- Async wrapper function without injected parameters-### `resolve_dependencies` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L696" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `resolve_dependencies` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L704" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonresolve_dependencies(fn: Callable[..., Any], arguments: dict[str, Any]) -> AsyncGenerator[dict[str, Any], None]@@ -239,7 +239,7 @@ time, so all injection goes through the unified DI system.which will be filtered out)-### `CurrentContext` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L837" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `CurrentContext` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L845" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonCurrentContext() -> Context@@ -258,7 +258,7 @@ current MCP operation (tool/resource/prompt call).- `RuntimeError`: If no active context found (during resolution)-### `CurrentDocket` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L880" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `CurrentDocket` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L888" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonCurrentDocket() -> Docket@@ -278,7 +278,7 @@ automatically creates for background task scheduling.- `ImportError`: If fastmcp[tasks] not installed-### `CurrentWorker` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L925" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `CurrentWorker` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L933" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonCurrentWorker() -> Worker@@ -298,7 +298,7 @@ automatically creates for background task processing.- `ImportError`: If fastmcp[tasks] not installed-### `CurrentFastMCP` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L967" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `CurrentFastMCP` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L975" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonCurrentFastMCP() -> FastMCP@@ -316,7 +316,7 @@ This dependency provides access to the active FastMCP server.- `RuntimeError`: If no server in context (during resolution)-### `CurrentRequest` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1002" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `CurrentRequest` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1010" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonCurrentRequest() -> Request@@ -336,7 +336,7 @@ current HTTP request. Only available when running over HTTP transports- `RuntimeError`: If no HTTP request in context (e.g., STDIO transport)-### `CurrentHeaders` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1038" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `CurrentHeaders` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1046" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonCurrentHeaders() -> dict[str, str]@@ -353,7 +353,7 @@ safe to use in code that might run over any transport.- A dependency that resolves to a dictionary of header name -> value-### `CurrentAccessToken` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1228" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `CurrentAccessToken` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1236" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonCurrentAccessToken() -> AccessToken@@ -372,7 +372,7 @@ authenticated request. Raises an error if no authentication is present.- `RuntimeError`: If no authenticated user (use get_access_token() for optional)-### `TokenClaim` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1280" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `TokenClaim` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1288" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonTokenClaim(name: str) -> str@@ -406,7 +406,7 @@ Returned by ``get_task_context()`` when running inside a Docket worker.Contains identifiers needed to communicate with the MCP session.-### `ProgressLike` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1065" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `ProgressLike` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1073" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Protocol for progress tracking interface.@@ -417,7 +417,7 @@ and Docket's Progress (worker context).**Methods:**-#### `current` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1073" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `current` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1081" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythoncurrent(self) -> int | None@@ -426,7 +426,7 @@ current(self) -> int | NoneCurrent progress value.-#### `total` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1078" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `total` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1086" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythontotal(self) -> int@@ -435,7 +435,7 @@ total(self) -> intTotal/target progress value.-#### `message` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1083" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `message` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1091" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonmessage(self) -> str | None@@ -444,7 +444,7 @@ message(self) -> str | NoneCurrent progress message.-#### `set_total` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1087" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `set_total` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1095" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonset_total(self, total: int) -> None@@ -453,7 +453,7 @@ set_total(self, total: int) -> NoneSet the total/target value for progress tracking.-#### `increment` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1091" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `increment` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1099" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonincrement(self, amount: int = 1) -> None@@ -462,7 +462,7 @@ increment(self, amount: int = 1) -> NoneAtomically increment the current progress value.-#### `set_message` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1095" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `set_message` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1103" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonset_message(self, message: str | None) -> None@@ -471,7 +471,7 @@ set_message(self, message: str | None) -> NoneUpdate the progress status message.-### `InMemoryProgress` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1100" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `InMemoryProgress` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1108" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>In-memory progress tracker for immediate tool execution.@@ -483,25 +483,25 @@ progress doesn't need to be observable across processes.**Methods:**-#### `current` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1120" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `current` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1128" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythoncurrent(self) -> int | None```-#### `total` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1124" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `total` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1132" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythontotal(self) -> int```-#### `message` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1128" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `message` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1136" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonmessage(self) -> str | None```-#### `set_total` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1131" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `set_total` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1139" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonset_total(self, total: int) -> None@@ -510,7 +510,7 @@ set_total(self, total: int) -> NoneSet the total/target value for progress tracking.-#### `increment` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1137" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `increment` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1145" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonincrement(self, amount: int = 1) -> None@@ -519,7 +519,7 @@ increment(self, amount: int = 1) -> NoneAtomically increment the current progress value.-#### `set_message` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1146" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+#### `set_message` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1154" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>```pythonset_message(self, message: str | None) -> None@@ -528,7 +528,7 @@ set_message(self, message: str | None) -> NoneUpdate the progress status message.-### `Progress` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1151" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>+### `Progress` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1159" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>FastMCP Progress dependency that works in both server and worker contexts.diff --git a/src/fastmcp/server/dependencies.py b/src/fastmcp/server/dependencies.pyindex acd7fea19..15c8c3124 100644--- a/src/fastmcp/server/dependencies.py+++ b/src/fastmcp/server/dependencies.py@@ -607,15 +607,23 @@ def without_injected_parameters(fn: Callable[..., Any]) -> Callable[..., Any]:result = await resultreturn result- # Set wrapper metadata (only parameter annotations, not return type)+ # Resolve string annotations (from `from __future__ import annotations`) using+ # the original function's module context. The wrapper's __globals__ points to+ # this module (dependencies.py) and is read-only, so some Pydantic versions+ # can't resolve names like Annotated or Literal from string annotations.+ try:+ resolved_hints = get_type_hints(fn, include_extras=True)+ except Exception:+ resolved_hints = getattr(fn, "__annotations__", {})+wrapper.__signature__ = new_sig # type: ignore[attr-defined]wrapper.__annotations__ = {- k: v- for k, v in getattr(fn, "__annotations__", {}).items()- if k not in exclude and k != "return"+ k: v for k, v in resolved_hints.items() if k not in exclude and k != "return"}wrapper.__name__ = getattr(fn, "__name__", "wrapper")wrapper.__doc__ = getattr(fn, "__doc__", None)+ wrapper.__module__ = fn.__module__+ wrapper.__qualname__ = getattr(fn, "__qualname__", wrapper.__qualname__)return wrapperdiff --git a/tests/tools/test_tool_future_annotations.py b/tests/tools/test_tool_future_annotations.pyindex a5d9d8c0d..10ca9c57a 100644--- a/tests/tools/test_tool_future_annotations.py+++ b/tests/tools/test_tool_future_annotations.py@@ -1,9 +1,9 @@from __future__ import annotations-from typing import Any, cast+from typing import Annotated, Any, Literal, castimport mcp.types-import pytest+from pydantic import Fieldfrom fastmcp import Context, FastMCPfrom fastmcp.client import Client@@ -55,6 +55,20 @@ async def async_with_context(ctx: Context) -> str:return f"Async request: {ctx.request_id}"+@fastmcp_server.tool+def annotated_with_context(+ query: Annotated[str, Field(description="Search query")], ctx: Context+) -> str:+ """Tool using Annotated + Field with context."""+ return f"Result for: {query}"+++@fastmcp_server.tool+def literal_with_context(mode: Literal["fast", "slow"], ctx: Context) -> str:+ """Tool using Literal with context."""+ return f"Mode: {mode}"++class TestFutureAnnotations:async def test_simple_with_context(self):async with Client(fastmcp_server) as client:@@ -102,6 +116,23 @@ class TestFutureAnnotations:"Async request:" in cast(mcp.types.TextContent, result.content[0]).text)+ async def test_annotated_with_context(self):+ """Test Annotated[str, Field(...)] works with Context and future annotations."""+ async with Client(fastmcp_server) as client:+ result = await client.call_tool(+ "annotated_with_context", {"query": "hello"}+ )+ assert (+ "Result for: hello"+ in cast(mcp.types.TextContent, result.content[0]).text+ )++ async def test_literal_with_context(self):+ """Test Literal types work with Context and future annotations."""+ async with Client(fastmcp_server) as client:+ result = await client.call_tool("literal_with_context", {"mode": "fast"})+ assert "Mode: fast" in cast(mcp.types.TextContent, result.content[0]).text+async def test_modern_union_syntax_works(self):"""Test that modern | union syntax works with future annotations."""# This demonstrates that our solution works with | syntax when types@@ -138,33 +169,16 @@ class TestFutureAnnotations:)- reason="Closure-scoped types cannot be resolved with 'from __future__ import annotations'. "- "When using future annotations, all type annotations become strings that need to be evaluated "- "using eval() in the function's global namespace. Types defined only in closure scope "- "(like local imports or type aliases) are not available in the function's __globals__ "- "and therefore cannot be resolved by get_type_hints()."-)-def test_closure_scoped_types_limitation():- """- This test demonstrates that closure-scoped types don't work with future annotations.-- The fundamental issue is that 'from __future__ import annotations' converts all- annotations to strings, and those strings can only be resolved using the function's- global namespace, not local variables from closures.- """-- def create_failing_closure():- # This import is only available in the closure scope+def test_closure_scoped_types_with_builtins():+ """Closure-scoped tools work when annotations only reference builtins."""+ def create_closure():mcp = FastMCP()@mcp.tooldef closure_tool(value: str | None) -> str:- """This will fail because Optional can't be resolved from closure import."""return str(value)return mcp- # This should raise an error during tool registration- create_failing_closure()+ create_closure()