Skip to main content

fastmcp.server.providers.base

Base Provider class for dynamic MCP components. This module provides the Provider abstraction for providing tools, resources, and prompts dynamically at runtime. Example:
from fastmcp import FastMCP
from fastmcp.server.providers import Provider
from fastmcp.tools import Tool

class DatabaseProvider(Provider):
    def __init__(self, db_url: str):
        super().__init__()
        self.db = Database(db_url)

    async def list_tools(self) -> list[Tool]:
        rows = await self.db.fetch("SELECT * FROM tools")
        return [self._make_tool(row) for row in rows]

    async def get_tool(self, name: str) -> Tool | None:
        row = await self.db.fetchone("SELECT * FROM tools WHERE name = ?", name)
        return self._make_tool(row) if row else None

mcp = FastMCP("Server", providers=[DatabaseProvider(db_url)])

Classes

Provider

Base class for dynamic component providers. Subclass and override whichever methods you need. Default implementations return empty lists / None, so you only need to implement what your provider supports. Methods:

with_transforms

with_transforms(self) -> Provider
Apply transformations to this provider’s components. Returns a TransformingProvider that wraps this provider and applies the specified transformations. Can be chained - each call creates a new wrapper that composes with the previous. Args:
  • namespace: Prefix for tools/prompts (“namespace_name”), path segment for resources (“protocol://namespace/path”).
  • tool_renames: Map of original_name → final_name. Tools in this map use the specified name instead of namespace prefixing.
Returns:
  • A TransformingProvider wrapping this provider.

with_namespace

with_namespace(self, namespace: str) -> Provider
Shorthand for with_transforms(namespace=…). Args:
  • namespace: The namespace to apply.
Returns:
  • A TransformingProvider wrapping this provider.

list_tools

list_tools(self) -> Sequence[Tool]
Return all available tools. Override to provide tools dynamically.

get_tool

get_tool(self, name: str) -> Tool | None
Get a specific tool by name. Default implementation lists all tools and finds by name. Override for more efficient single-tool lookup. Returns:
  • The Tool if found, or None to continue searching other providers.

list_resources

list_resources(self) -> Sequence[Resource]
Return all available resources. Override to provide resources dynamically.

get_resource

get_resource(self, uri: str) -> Resource | None
Get a specific resource by URI. Default implementation lists all resources and finds by URI. Override for more efficient single-resource lookup. Returns:
  • The Resource if found, or None to continue searching other providers.

list_resource_templates

list_resource_templates(self) -> Sequence[ResourceTemplate]
Return all available resource templates. Override to provide resource templates dynamically.

get_resource_template

get_resource_template(self, uri: str) -> ResourceTemplate | None
Get a resource template that matches the given URI. Default implementation lists all templates and finds one whose pattern matches the URI. Override for more efficient lookup. Returns:
  • The ResourceTemplate if a matching one is found, or None to continue searching.

list_prompts

list_prompts(self) -> Sequence[Prompt]
Return all available prompts. Override to provide prompts dynamically.

get_prompt

get_prompt(self, name: str) -> Prompt | None
Get a specific prompt by name. Default implementation lists all prompts and finds by name. Override for more efficient single-prompt lookup. Returns:
  • The Prompt if found, or None to continue searching other providers.

get_component

get_component(self, key: str) -> Tool | Resource | ResourceTemplate | Prompt | None
Get a component by its prefixed key. Args:
  • key: The prefixed key (e.g., “tool:name”, “resource:uri”, “template:uri”).
Returns:
  • The component if found, or None to continue searching other providers.

get_tasks

get_tasks(self) -> Sequence[FastMCPComponent]
Return components that should be registered as background tasks. Override to customize which components are task-eligible. Default calls list_* methods and filters for components with task_config.mode != ‘forbidden’. Used by the server during startup to register functions with Docket.

lifespan

lifespan(self) -> AsyncIterator[None]
User-overridable lifespan for custom setup and teardown. Override this method to perform provider-specific initialization like opening database connections, setting up external resources, or other state management needed for the provider’s lifetime. The lifespan scope matches the server’s lifespan - code before yield runs at startup, code after yield runs at shutdown.

enable

enable(self) -> None
Enable components by removing from blocklist, or set allowlist with only=True. Args:
  • keys: Keys to enable (e.g., “tool:my_tool”).
  • tags: Tags to enable - components with these tags will be enabled.
  • only: If True, switches to allowlist mode - ONLY show these keys/tags.

disable

disable(self) -> None
Disable components by adding to the blocklist. Args:
  • keys: Keys to disable (e.g., “tool:my_tool”).
  • tags: Tags to disable - components with these tags will be disabled.