Back to Fastmcp

__init__

docs/python-sdk/fastmcp-server-transforms-__init__.mdx

3.2.46.2 KB
Original Source

fastmcp.server.transforms

Transform system for component transformations.

Transforms modify components (tools, resources, prompts). List operations use a pure function pattern where transforms receive sequences and return transformed sequences. Get operations use a middleware pattern with call_next to chain lookups.

Unlike middleware (which operates on requests), transforms are observable by the system for task registration, tag filtering, and component introspection.

Example: ```python from fastmcp import FastMCP from fastmcp.server.transforms import Namespace

server = FastMCP("Server")
mount = server.mount(other_server)
mount.add_transform(Namespace("api"))  # Tools become api_toolname
```

Classes

GetToolNext <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L36" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Protocol for get_tool call_next functions.

GetResourceNext <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L44" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Protocol for get_resource call_next functions.

GetResourceTemplateNext <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L52" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Protocol for get_resource_template call_next functions.

GetPromptNext <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L60" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Protocol for get_prompt call_next functions.

Transform <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L68" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Base class for component transformations.

List operations use a pure function pattern: transforms receive sequences and return transformed sequences. Get operations use a middleware pattern with call_next to chain lookups.

Methods:

list_tools <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L95" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
list_tools(self, tools: Sequence[Tool]) -> Sequence[Tool]

List tools with transformation applied.

Args:

  • tools: Sequence of tools to transform.

Returns:

  • Transformed sequence of tools.

get_tool <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L106" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_tool(self, name: str, call_next: GetToolNext) -> Tool | None

Get a tool by name.

Args:

  • name: The requested tool name (may be transformed).
  • call_next: Callable to get tool from downstream.
  • version: Optional version filter to apply.

Returns:

  • The tool if found, None otherwise.

list_resources <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L125" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
list_resources(self, resources: Sequence[Resource]) -> Sequence[Resource]

List resources with transformation applied.

Args:

  • resources: Sequence of resources to transform.

Returns:

  • Transformed sequence of resources.

get_resource <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L136" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_resource(self, uri: str, call_next: GetResourceNext) -> Resource | None

Get a resource by URI.

Args:

  • uri: The requested resource URI (may be transformed).
  • call_next: Callable to get resource from downstream.
  • version: Optional version filter to apply.

Returns:

  • The resource if found, None otherwise.

list_resource_templates <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L159" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
list_resource_templates(self, templates: Sequence[ResourceTemplate]) -> Sequence[ResourceTemplate]

List resource templates with transformation applied.

Args:

  • templates: Sequence of resource templates to transform.

Returns:

  • Transformed sequence of resource templates.

get_resource_template <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L172" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_resource_template(self, uri: str, call_next: GetResourceTemplateNext) -> ResourceTemplate | None

Get a resource template by URI.

Args:

  • uri: The requested template URI (may be transformed).
  • call_next: Callable to get template from downstream.
  • version: Optional version filter to apply.

Returns:

  • The resource template if found, None otherwise.

list_prompts <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L195" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
list_prompts(self, prompts: Sequence[Prompt]) -> Sequence[Prompt]

List prompts with transformation applied.

Args:

  • prompts: Sequence of prompts to transform.

Returns:

  • Transformed sequence of prompts.

get_prompt <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/transforms/__init__.py#L206" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_prompt(self, name: str, call_next: GetPromptNext) -> Prompt | None

Get a prompt by name.

Args:

  • name: The requested prompt name (may be transformed).
  • call_next: Callable to get prompt from downstream.
  • version: Optional version filter to apply.

Returns:

  • The prompt if found, None otherwise.