Use ergonomic provider initialization pattern (#2675)

Updates examples and convenience methods to pass providers directly to FastMCP's __init__ instead of calling add_provider() after instantiation.
This commit is contained in:
Jeremiah Lowin 2025-12-22 14:49:39 -05:00 committed by GitHub
commit bca310cdde
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 7 additions and 20 deletions

View file

@ -101,10 +101,8 @@ class SQLiteToolProvider(Provider):
)
mcp = FastMCP("DynamicToolsServer")
provider = SQLiteToolProvider(db_path=str(DB_PATH))
mcp.add_provider(provider)
mcp = FastMCP("DynamicToolsServer", providers=[provider])
@mcp.tool

View file

@ -8,9 +8,7 @@ This class is deprecated. Use FastMCP with OpenAPIProvider instead:
client = httpx.AsyncClient(base_url="https://api.example.com")
provider = OpenAPIProvider(openapi_spec=spec, client=client)
mcp = FastMCP("My API Server")
mcp.add_provider(provider)
mcp = FastMCP("My API Server", providers=[provider])
"""
from __future__ import annotations
@ -55,9 +53,7 @@ class FastMCPOpenAPI(FastMCP):
client = httpx.AsyncClient(base_url="https://api.example.com")
provider = OpenAPIProvider(openapi_spec=spec, client=client)
mcp = FastMCP("API Server")
mcp.add_provider(provider)
mcp = FastMCP("API Server", providers=[provider])
```
"""
@ -94,8 +90,7 @@ class FastMCPOpenAPI(FastMCP):
warnings.warn(
"FastMCPOpenAPI is deprecated. Use FastMCP with OpenAPIProvider instead:\n"
" provider = OpenAPIProvider(openapi_spec=spec, client=client)\n"
" mcp = FastMCP('name')\n"
" mcp.add_provider(provider)",
" mcp = FastMCP('name', providers=[provider])",
DeprecationWarning,
stacklevel=2,
)

View file

@ -10,9 +10,7 @@ Example:
client = httpx.AsyncClient(base_url="https://api.example.com")
provider = OpenAPIProvider(openapi_spec=spec, client=client)
mcp = FastMCP("API Server")
mcp.add_provider(provider)
mcp = FastMCP("API Server", providers=[provider])
```
"""

View file

@ -2788,9 +2788,7 @@ class FastMCP(Generic[LifespanResultT]):
tags=tags,
timeout=timeout,
)
server = cls(name=name, **settings)
server.add_provider(provider)
return server
return cls(name=name, providers=[provider], **settings)
@classmethod
def from_fastapi(
@ -2847,9 +2845,7 @@ class FastMCP(Generic[LifespanResultT]):
tags=tags,
timeout=timeout,
)
server = cls(name=server_name, **settings)
server.add_provider(provider)
return server
return cls(name=server_name, providers=[provider], **settings)
@classmethod
def as_proxy(