Create a FastMCP proxy server for the given target.This is the recommended way to create a proxy server. For lower-level control,
use FastMCPProxy or ProxyProvider directly from fastmcp.server.providers.proxy.Args:
target: The backend to proxy to. Can be:
A Client instance (connected or disconnected)
A ClientTransport
A FastMCP server instance
A URL string or AnyUrl
A Path to a server script
An MCPConfig or dict
**settings: Additional settings passed to FastMCPProxy (name, etc.)
The server’s local provider, which stores directly-registered components.Use this to remove components:mcp.local_provider.remove_tool(“my_tool”)
mcp.local_provider.remove_resource(“data://info”)
mcp.local_provider.remove_prompt(“my_prompt”)
Add a provider for dynamic tools, resources, and prompts.Providers are queried in registration order. The first provider to return
a non-None result wins. Static components (registered via decorators)
always take precedence over providers.Args:
provider: A Provider instance that will provide components dynamically.
Get task-eligible components with all transforms applied.Overrides AggregateProvider.get_tasks() to apply server-level transforms
after aggregation. AggregateProvider handles provider-level namespacing.
Add a server-level transform.Server-level transforms are applied after all providers are aggregated.
They transform tools, resources, and prompts from ALL providers.Args:
List all enabled tools from providers.Overrides Provider.list_tools() to add visibility filtering, auth filtering,
and middleware execution. Returns all versions (no deduplication).
Protocol handlers deduplicate for MCP wire format.
Get a tool by name, filtering disabled tools.Overrides Provider.get_tool() to add visibility filtering after all
transforms (including session-level) have been applied. This ensures
session transforms can override provider-level disables.When the highest version is disabled and no explicit version was
requested, falls back to the next-highest enabled version.Args:
name: The tool name.
version: Version filter (None returns highest version).
List all enabled resources from providers.Overrides Provider.list_resources() to add visibility filtering, auth filtering,
and middleware execution. Returns all versions (no deduplication).
Protocol handlers deduplicate for MCP wire format.
Get a resource by URI, filtering disabled resources.Overrides Provider.get_resource() to add visibility filtering after all
transforms (including session-level) have been applied.When the highest version is disabled and no explicit version was
requested, falls back to the next-highest enabled version.Args:
uri: The resource URI.
version: Version filter (None returns highest version).
Returns:
The resource if found and enabled, None otherwise.
List all enabled resource templates from providers.Overrides Provider.list_resource_templates() to add visibility filtering,
auth filtering, and middleware execution. Returns all versions (no deduplication).
Protocol handlers deduplicate for MCP wire format.
Get a resource template by URI, filtering disabled templates.Overrides Provider.get_resource_template() to add visibility filtering after
all transforms (including session-level) have been applied.When the highest version is disabled and no explicit version was
requested, falls back to the next-highest enabled version.Args:
uri: The template URI.
version: Version filter (None returns highest version).
Returns:
The template if found and enabled, None otherwise.
List all enabled prompts from providers.Overrides Provider.list_prompts() to add visibility filtering, auth filtering,
and middleware execution. Returns all versions (no deduplication).
Protocol handlers deduplicate for MCP wire format.
Get a prompt by name, filtering disabled prompts.Overrides Provider.get_prompt() to add visibility filtering after all
transforms (including session-level) have been applied.When the highest version is disabled and no explicit version was
requested, falls back to the next-highest enabled version.Args:
name: The prompt name.
version: Version filter (None returns highest version).
Read a resource by URI.This is the public API for reading resources. By default, middleware is applied.
Checks concrete resources first, then templates.Args:
uri: The resource URI
version: Specific version to read. If None, reads highest version.
run_middleware: If True (default), apply the middleware chain.
Set to False when called from middleware to avoid re-applying.
task_meta: If provided, execute as a background task and return
CreateTaskResult. If None (default), execute synchronously and
return ResourceResult.
Render a prompt by name.This is the public API for rendering prompts. By default, middleware is applied.
Use get_prompt() to retrieve the prompt definition without rendering.Args:
name: The prompt name
arguments: Prompt arguments (optional)
version: Specific version to render. If None, renders highest version.
run_middleware: If True (default), apply the middleware chain.
Set to False when called from middleware to avoid re-applying.
task_meta: If provided, execute as a background task and return
CreateTaskResult. If None (default), execute synchronously and
return PromptResult.
Add a tool to the server.The tool function can optionally request a Context object by adding a parameter
with the Context type annotation. See the @tool decorator for examples.Args:
tool: The Tool instance or @tool-decorated function to register
Decorator to register a tool.Tools can optionally request a Context object by adding a parameter with the
Context type annotation. The context provides access to MCP capabilities like
logging, progress reporting, and resource access.This decorator supports multiple calling patterns:
@server.tool (without parentheses)
@server.tool (with empty parentheses)
@server.tool(“custom_name”) (with name as first argument)
@server.tool(name=“custom_name”) (with name as keyword argument)
server.tool(function, name=“custom_name”) (direct function call)
Args:
name_or_fn: Either a function (when used as @tool), a string name, or None
name: Optional name for the tool (keyword-only, alternative to name_or_fn)
description: Optional description of what the tool does
tags: Optional set of tags for categorizing the tool
output_schema: Optional JSON schema for the tool’s output
annotations: Optional annotations about the tool’s behavior
exclude_args: Optional list of argument names to exclude from the tool schema.
Deprecated: Use Depends() for dependency injection instead.
meta: Optional meta information about the tool
Examples:Register a tool with a custom name:
@server.tooldef my_tool(x: int) -> str: return str(x)# Register a tool with a custom name@server.tooldef my_tool(x: int) -> str: return str(x)@server.tool("custom_name")def my_tool(x: int) -> str: return str(x)@server.tool(name="custom_name")def my_tool(x: int) -> str: return str(x)# Direct function callserver.tool(my_function, name="custom_name")
Decorator to register a function as a resource.The function will be called when the resource is read to generate its content.
The function can return:
str for text content
bytes for binary content
other types will be converted to JSON
Resources can optionally request a Context object by adding a parameter with the
Context type annotation. The context provides access to MCP capabilities like
logging, progress reporting, and session information.If the URI contains parameters (e.g. “resource://”) or the function
has parameters, it will be registered as a template resource.Args:
uri: URI for the resource (e.g. “resource://my-resource” or “resource://”)
name: Optional name for the resource
description: Optional description of the resource
mime_type: Optional MIME type for the resource
tags: Optional set of tags for categorizing the resource
annotations: Optional annotations about the resource’s behavior
meta: Optional meta information about the resource
Examples:Register a resource with a custom name:
@server.resource("resource://my-resource")def get_data() -> str: return "Hello, world!"@server.resource("resource://my-resource")async get_data() -> str: data = await fetch_data() return f"Hello, world! {data}"@server.resource("resource://{city}/weather")def get_weather(city: str) -> str: return f"Weather for {city}"@server.resource("resource://{city}/weather")async def get_weather_with_context(city: str, ctx: Context) -> str: await ctx.info(f"Fetching weather for {city}") return f"Weather for {city}"@server.resource("resource://{city}/weather")async def get_weather(city: str) -> str: data = await fetch_weather(city) return f"Weather for {city}: {data}"
Decorator to register a prompt.Prompts can optionally request a Context object by adding a parameter with the
Context type annotation. The context provides access to MCP capabilities like
logging, progress reporting, and session information.This decorator supports multiple calling patterns:
@server.prompt (without parentheses)
@server.prompt() (with empty parentheses)
@server.prompt(“custom_name”) (with name as first argument)
@server.prompt(name=“custom_name”) (with name as keyword argument)
server.prompt(function, name=“custom_name”) (direct function call)
Args:
name_or_fn: Either a function (when used as @prompt), a string name, or None
name: Optional name for the prompt (keyword-only, alternative to name_or_fn)
description: Optional description of what the prompt does
tags: Optional set of tags for categorizing the prompt
meta: Optional meta information about the promptExamples:
Mount another FastMCP server on this server with an optional namespace.Unlike importing (with import_server), mounting establishes a dynamic connection
between servers. When a client interacts with a mounted server’s objects through
the parent server, requests are forwarded to the mounted server in real-time.
This means changes to the mounted server are immediately reflected when accessed
through the parent.When a server is mounted with a namespace:
Tools from the mounted server are accessible with namespaced names.
Example: If server has a tool named “get_weather”, it will be available as “namespace_get_weather”.
Resources are accessible with namespaced URIs.
Example: If server has a resource with URI “weather://forecast”, it will be available as
“weather://namespace/forecast”.
Templates are accessible with namespaced URI templates.
Example: If server has a template with URI “weather://location/”, it will be available
as “weather://namespace/location/”.
Prompts are accessible with namespaced names.
Example: If server has a prompt named “weather_prompt”, it will be available as
“namespace_weather_prompt”.
When a server is mounted without a namespace (namespace=None), its tools, resources, templates,
and prompts are accessible with their original names. Multiple servers can be mounted
without namespaces, and they will be tried in order until a match is found.The mounted server’s lifespan is executed when the parent server starts, and its
middleware chain is invoked for all operations (tool calls, resource reads, prompts).Args:
server: The FastMCP server to mount.
namespace: Optional namespace to use for the mounted server’s objects. If None,
the server’s objects are accessible with their original names.
as_proxy: Deprecated. Mounted servers now always have their lifespan and
middleware invoked. To create a proxy server, use create_proxy()
explicitly before mounting.
tool_names: Optional mapping of original tool names to custom names. Use this
to override namespaced names. Keys are the original tool names from the
mounted server.
Import the MCP objects from another FastMCP server into this one,
optionally with a given prefix... deprecated::
Use :meth:mount instead. import_server will be removed in a
future version.Note that when a server is imported, its objects are immediately
registered to the importing server. This is a one-time operation and
future changes to the imported server will not be reflected in the
importing server. Server-level configurations and lifespans are not imported.When a server is imported with a prefix:
The tools are imported with prefixed names
Example: If server has a tool named “get_weather”, it will be
available as “prefix_get_weather”
The resources are imported with prefixed URIs using the new format
Example: If server has a resource with URI “weather://forecast”, it will
be available as “weather://prefix/forecast”
The templates are imported with prefixed URI templates using the new format
Example: If server has a template with URI “weather://location/”, it will
be available as “weather://prefix/location/”
The prompts are imported with prefixed names
Example: If server has a prompt named “weather_prompt”, it will be available as
“prefix_weather_prompt”
When a server is imported without a prefix (prefix=None), its tools, resources,
templates, and prompts are imported with their original names.Args:
server: The FastMCP server to import
prefix: Optional prefix to use for the imported server’s objects. If None,
objects are imported with their original names.
Create a FastMCP server from an OpenAPI specification.Args:
openapi_spec: OpenAPI schema as a dictionary
client: Optional httpx AsyncClient for making HTTP requests.
If not provided, a default client is created using the first
server URL from the OpenAPI spec with a 30-second timeout.
name: Name for the MCP server
route_maps: Optional list of RouteMap objects defining route mappings
route_map_fn: Optional callable for advanced route type mapping
mcp_component_fn: Optional callable for component customization
mcp_names: Optional dictionary mapping operationId to component names
tags: Optional set of tags to add to all components
validate_output: If True (default), tools use the output schema
extracted from the OpenAPI spec for response validation. If
False, a permissive schema is used instead, allowing any
response structure while still returning structured JSON.
**settings: Additional settings passed to FastMCP
Returns:
A FastMCP server with an OpenAPIProvider attached.
Create a FastMCP proxy server for the given backend... deprecated::
Use :func:fastmcp.server.create_proxy instead.
This method will be removed in a future version.The backend argument can be either an existing fastmcp.client.Client
instance or any value accepted as the transport argument of
fastmcp.client.Client. This mirrors the convenience of the
fastmcp.client.Client constructor.