mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 15:19:10 +02:00
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
321 lines
11 KiB
Text
321 lines
11 KiB
Text
---
|
|
title: base
|
|
sidebarTitle: base
|
|
---
|
|
|
|
# `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:
|
|
```python
|
|
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` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L51" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
|
|
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:**
|
|
|
|
#### `transforms` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L76" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
transforms(self) -> list[Transform]
|
|
```
|
|
|
|
All transforms applied to components from this provider.
|
|
|
|
|
|
#### `add_transform` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L80" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
add_transform(self, transform: Transform) -> None
|
|
```
|
|
|
|
Add a transform to this provider.
|
|
|
|
Transforms modify components (tools, resources, prompts) as they flow
|
|
through the provider. They're applied in order - first added is innermost.
|
|
|
|
**Args:**
|
|
- `transform`: The transform to add.
|
|
|
|
|
|
#### `wrap_transform` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L100" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
wrap_transform(self, transform: Transform) -> Provider
|
|
```
|
|
|
|
Return a new provider with this transform applied (immutable).
|
|
|
|
Unlike add_transform() which mutates this provider, wrap_transform()
|
|
returns a new provider that wraps this one. The original provider
|
|
is unchanged.
|
|
|
|
This is useful when you want to apply transforms without side effects,
|
|
such as adding the same provider to multiple aggregators with different
|
|
namespaces.
|
|
|
|
**Args:**
|
|
- `transform`: The transform to apply.
|
|
|
|
**Returns:**
|
|
- A new provider that wraps this one with the transform applied.
|
|
|
|
|
|
#### `list_tools` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L136" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
list_tools(self) -> Sequence[Tool]
|
|
```
|
|
|
|
List tools with all transforms applied.
|
|
|
|
Applies transforms sequentially: base → transforms (in order).
|
|
Each transform receives the result from the previous transform.
|
|
Components may be marked as disabled but are NOT filtered here -
|
|
filtering happens at the server level to allow session transforms to override.
|
|
|
|
**Returns:**
|
|
- Transformed sequence of tools (including disabled ones).
|
|
|
|
|
|
#### `get_tool` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L152" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
get_tool(self, name: str, version: VersionSpec | None = None) -> Tool | None
|
|
```
|
|
|
|
Get tool by transformed name with all transforms applied.
|
|
|
|
Note: This method does NOT filter disabled components. The Server
|
|
(FastMCP) performs enabled filtering after all transforms complete,
|
|
allowing session-level transforms to override provider-level disables.
|
|
|
|
**Args:**
|
|
- `name`: The transformed tool name to look up.
|
|
- `version`: Optional version filter. If None, returns highest version.
|
|
|
|
**Returns:**
|
|
- The tool if found (may be marked disabled), None if not found.
|
|
|
|
|
|
#### `get_app_tool` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L178" 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
|
|
```
|
|
|
|
Look up an app-visible tool by original name, bypassing transforms.
|
|
|
|
Searches for a tool named ``tool_name`` tagged with the given app
|
|
name. Skips the transform chain entirely.
|
|
|
|
**Returns:**
|
|
- The tool if found and tagged with the given app name, else None.
|
|
|
|
|
|
#### `list_resources` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L204" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
list_resources(self) -> Sequence[Resource]
|
|
```
|
|
|
|
List resources with all transforms applied.
|
|
|
|
Components may be marked as disabled but are NOT filtered here.
|
|
|
|
|
|
#### `get_resource` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L214" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
get_resource(self, uri: str, version: VersionSpec | None = None) -> Resource | None
|
|
```
|
|
|
|
Get resource by transformed URI with all transforms applied.
|
|
|
|
Note: This method does NOT filter disabled components. The Server
|
|
(FastMCP) performs enabled filtering after all transforms complete.
|
|
|
|
**Args:**
|
|
- `uri`: The transformed resource URI to look up.
|
|
- `version`: Optional version filter. If None, returns highest version.
|
|
|
|
**Returns:**
|
|
- The resource if found (may be marked disabled), None if not found.
|
|
|
|
|
|
#### `list_resource_templates` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L239" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
list_resource_templates(self) -> Sequence[ResourceTemplate]
|
|
```
|
|
|
|
List resource templates with all transforms applied.
|
|
|
|
Components may be marked as disabled but are NOT filtered here.
|
|
|
|
|
|
#### `get_resource_template` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L249" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
get_resource_template(self, uri: str, version: VersionSpec | None = None) -> ResourceTemplate | None
|
|
```
|
|
|
|
Get resource template by transformed URI with all transforms applied.
|
|
|
|
Note: This method does NOT filter disabled components. The Server
|
|
(FastMCP) performs enabled filtering after all transforms complete.
|
|
|
|
**Args:**
|
|
- `uri`: The transformed template URI to look up.
|
|
- `version`: Optional version filter. If None, returns highest version.
|
|
|
|
**Returns:**
|
|
- The template if found (may be marked disabled), None if not found.
|
|
|
|
|
|
#### `list_prompts` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L276" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
list_prompts(self) -> Sequence[Prompt]
|
|
```
|
|
|
|
List prompts with all transforms applied.
|
|
|
|
Components may be marked as disabled but are NOT filtered here.
|
|
|
|
|
|
#### `get_prompt` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L286" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
get_prompt(self, name: str, version: VersionSpec | None = None) -> Prompt | None
|
|
```
|
|
|
|
Get prompt by transformed name with all transforms applied.
|
|
|
|
Note: This method does NOT filter disabled components. The Server
|
|
(FastMCP) performs enabled filtering after all transforms complete.
|
|
|
|
**Args:**
|
|
- `name`: The transformed prompt name to look up.
|
|
- `version`: Optional version filter. If None, returns highest version.
|
|
|
|
**Returns:**
|
|
- The prompt if found (may be marked disabled), None if not found.
|
|
|
|
|
|
#### `get_tasks` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L444" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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, applies provider transforms, and filters
|
|
for components with task_config.mode != 'forbidden'.
|
|
|
|
Used by the server during startup to register functions with Docket.
|
|
|
|
|
|
#### `lifespan` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L489" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L518" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
enable(self) -> Self
|
|
```
|
|
|
|
Enable components matching all specified criteria.
|
|
|
|
Adds a visibility transform that marks matching components as enabled.
|
|
Later transforms override earlier ones, so enable after disable makes
|
|
the component enabled.
|
|
|
|
With only=True, switches to allowlist mode - first disables everything,
|
|
then enables matching components.
|
|
|
|
**Args:**
|
|
- `names`: Component names or URIs to enable.
|
|
- `keys`: Component keys to enable (e.g., {"tool\:my_tool@v1"}).
|
|
- `version`: Component version spec to enable (e.g., VersionSpec(eq="v1") or
|
|
VersionSpec(gte="v2")). Unversioned components will not match.
|
|
- `tags`: Enable components with these tags.
|
|
- `components`: Component types to include (e.g., {"tool", "prompt"}).
|
|
- `only`: If True, ONLY enable matching components (allowlist mode).
|
|
|
|
**Returns:**
|
|
- Self for method chaining.
|
|
|
|
|
|
#### `disable` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L567" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
disable(self) -> Self
|
|
```
|
|
|
|
Disable components matching all specified criteria.
|
|
|
|
Adds a visibility transform that marks matching components as disabled.
|
|
Components can be re-enabled by calling enable() with matching criteria
|
|
(the later transform wins).
|
|
|
|
**Args:**
|
|
- `names`: Component names or URIs to disable.
|
|
- `keys`: Component keys to disable (e.g., {"tool\:my_tool@v1"}).
|
|
- `version`: Component version spec to disable (e.g., VersionSpec(eq="v1") or
|
|
VersionSpec(gte="v2")). Unversioned components will not match.
|
|
- `tags`: Disable components with these tags.
|
|
- `components`: Component types to include (e.g., {"tool", "prompt"}).
|
|
|
|
**Returns:**
|
|
- Self for method chaining.
|
|
|