feat: Provider abstraction for dynamic MCP components (#2622)

* feat: add Provider abstraction for dynamic components

Introduces a `Provider` base class that allows dynamic provision of
tools, resources, and prompts at runtime. Providers are queried after
static components, enabling database-backed tools, external integrations,
and other dynamic component sources.

```python
from fastmcp import FastMCP, Provider

class DatabaseProvider(Provider):
    async def list_tools(self, context):
        return await db.fetch_tools()

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

* Add execution methods to Provider interface

Enable providers to customize how components are executed, not just listed.
This unlocks MountedProvider (future) invoking wrapped server middleware.

- call_tool, read_resource, read_resource_template, render_prompt
- Server uses provider execution methods after filter checks
- Default implementations delegate to component methods

* Apply unified error handling to provider execution

- Document error semantics: list_* gracefully degrades, execution propagates
- Wrap provider call_tool/read_resource/render_prompt with masking logic
- ToolError/ResourceError/PromptError pass through, others wrapped
This commit is contained in:
Jeremiah Lowin 2025-12-16 22:05:43 -05:00 committed by GitHub
commit 0e5a8fed6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 1398 additions and 63 deletions

View file

@ -133,6 +133,14 @@ async with Client(transport=StreamableHttpTransport(server_url)) as client:
- Use `# type: ignore[attr-defined]` in tests for MCP results instead of type assertions
- Each feature needs corresponding tests
### Module Exports
- **Be intentional about re-exports** - don't blindly re-export everything to parent namespaces
- Core types that define a module's purpose should be exported (e.g., `Middleware` from `fastmcp.server.middleware`)
- Specialized features can live in submodules (e.g., `fastmcp.server.middleware.dynamic`)
- Only re-export to `fastmcp.*` for the most fundamental types (e.g., `FastMCP`, `Client`)
- When in doubt, prefer users importing from the specific submodule over re-exporting
### Documentation
- Uses Mintlify framework