fastmcp.server.providers.fastmcp_provider
FastMCPProvider for wrapping FastMCP servers as providers.
This module provides the FastMCPProvider class that wraps a FastMCP server
and exposes its components through the Provider interface.
It also provides FastMCPProvider* component classes that delegate execution to
the wrapped server’s middleware, ensuring middleware runs when components are
executed.
Classes
FastMCPProviderTool
Tool that delegates execution to a wrapped server’s middleware.
When run() is called, this tool invokes the wrapped server’s
_call_tool_middleware() method, ensuring the server’s middleware
chain is executed.
Methods:
wrap
run
get_span_attributes
FastMCPProviderResource
Resource that delegates reading to a wrapped server’s read_resource().
When read() is called, this resource invokes the wrapped server’s
read_resource() method, ensuring the server’s middleware chain is executed.
Methods:
wrap
get_span_attributes
FastMCPProviderPrompt
Prompt that delegates rendering to a wrapped server’s render_prompt().
When render() is called, this prompt invokes the wrapped server’s
render_prompt() method, ensuring the server’s middleware chain is executed.
Methods:
wrap
render
get_span_attributes
FastMCPProviderResourceTemplate
Resource template that creates FastMCPProviderResources.
When create_resource() is called, this template creates a
FastMCPProviderResource that will invoke the wrapped server’s middleware
when read.
Methods:
wrap
create_resource
uri is the external/transformed URI (e.g., with namespace prefix).
We use _original_uri_template with params to construct the internal
URI that the nested server understands.
get_span_attributes
FastMCPProvider
Provider that wraps a FastMCP server.
This provider enables mounting one FastMCP server onto another, exposing
the mounted server’s tools, resources, and prompts through the parent
server.
Components returned by this provider are wrapped in FastMCPProvider*
classes that delegate execution to the wrapped server’s middleware chain.
This ensures middleware runs when components are executed.
Methods:
get_app_tool
get_tool_by_hash
get_tasks
lifespan
_lifespan_root_active=True to signal to the wrapped server’s
_docket_lifespan that it is running below an existing root in the
same runtime tree, then delegates to its full _lifespan_manager.
The root’s Docket / Worker / SharedContext are reused through
ContextVars (_current_docket etc.); the mounted server’s user
lifespan, _lifespan_result cache, and its own sub-providers
(nested mounts) all run normally.
The flag is reset as soon as _lifespan_manager finishes entering,
so it doesn’t leak into the caller’s async scope. Unrelated servers
entered later in the same task (e.g. siblings via AsyncExitStack)
correctly see no active root and start their own infrastructure.
