mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-18 11:39:12 +02:00
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
264 lines
8.5 KiB
Text
264 lines
8.5 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/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L43" 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:**
|
|
|
|
#### `with_transforms` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L67" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L118" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L135" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
list_tools(self) -> Sequence[Tool]
|
|
```
|
|
|
|
Return all available tools.
|
|
|
|
Override to provide tools dynamically.
|
|
|
|
|
|
#### `get_tool` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L142" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L154" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
list_resources(self) -> Sequence[Resource]
|
|
```
|
|
|
|
Return all available resources.
|
|
|
|
Override to provide resources dynamically.
|
|
|
|
|
|
#### `get_resource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L161" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L173" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
list_resource_templates(self) -> Sequence[ResourceTemplate]
|
|
```
|
|
|
|
Return all available resource templates.
|
|
|
|
Override to provide resource templates dynamically.
|
|
|
|
|
|
#### `get_resource_template` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L180" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L196" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
list_prompts(self) -> Sequence[Prompt]
|
|
```
|
|
|
|
Return all available prompts.
|
|
|
|
Override to provide prompts dynamically.
|
|
|
|
|
|
#### `get_prompt` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L203" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L215" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L244" 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 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/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L272" 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/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L301" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/base.py#L317" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
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.
|
|
|