Back to Fastmcp

fastmcp_provider

docs/python-sdk/fastmcp-server-providers-fastmcp_provider.mdx

4.0.09.2 KB
Original Source

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 <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L37" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

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 <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L59" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
wrap(cls, server: Any, tool: Tool) -> FastMCPProviderTool

Wrap a Tool to delegate execution to the server's middleware.

run <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L101" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
run(self, arguments: dict[str, Any]) -> ToolResult

Delegate to the child server's call_tool().

This is called when the tool is used within a TransformedTool forwarding function or other contexts.

get_span_attributes <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L114" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_span_attributes(self) -> dict[str, Any]

FastMCPProviderResource <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L121" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

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 <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L142" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
wrap(cls, server: Any, resource: Resource) -> FastMCPProviderResource

Wrap a Resource to delegate reading to the server's middleware.

get_span_attributes <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L176" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_span_attributes(self) -> dict[str, Any]

FastMCPProviderPrompt <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L183" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

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 <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L204" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
wrap(cls, server: Any, prompt: Prompt) -> FastMCPProviderPrompt

Wrap a Prompt to delegate rendering to the server's middleware.

render <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L238" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
render(self, arguments: dict[str, Any] | None = None) -> PromptResult

Delegate to the child server's render_prompt().

This is called when the prompt is used within a transformed context or other contexts.

get_span_attributes <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L251" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_span_attributes(self) -> dict[str, Any]

FastMCPProviderResourceTemplate <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L258" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

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 <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L280" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
wrap(cls, server: Any, template: ResourceTemplate) -> FastMCPProviderResourceTemplate

Wrap a ResourceTemplate to create FastMCPProviderResources.

create_resource <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L302" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
create_resource(self, uri: str, params: dict[str, Any]) -> Resource

Create a FastMCPProviderResource for the given URI.

The 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 <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L344" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_span_attributes(self) -> dict[str, Any]

FastMCPProvider <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L356" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

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 <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L431" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_app_tool(self, app_name: str, tool_name: str) -> Tool | None

Delegate to nested server's get_app_tool, wrapping for middleware.

get_tool_by_hash <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L442" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_tool_by_hash(self, tool_hash: str, tool_name: str) -> Tool | None

Delegate to nested server's get_tool_by_hash, wrapping for middleware.

get_tasks <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L541" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_tasks(self) -> Sequence[FastMCPComponent]

Return task-eligible components from the mounted server.

Returns the child's ACTUAL components (not wrapped) so their actual functions get registered with Docket. Gets components with child server's transforms applied, then applies this provider's transforms for correct registration keys.

lifespan <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py#L582" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
lifespan(self) -> AsyncIterator[None]

Start the mounted server's lifespan.

Sets _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.