modern-di integration for FastMCP.
Full guide: FastMCP integration docs
Usage example: examples/
Installation
uv add modern-di-fastmcp # or: pip install modern-di-fastmcp
Usage
setup_di attaches the root container to the server and adds a middleware that builds a request container for every MCP request. FromDI, used as a parameter's default value, resolves a provider (or type) from it and keeps the parameter out of the tool schema.
import dataclasses
import fastmcp
from modern_di import Container, Group, Scope, providers
from modern_di_fastmcp import FromDI, setup_di
@dataclasses.dataclass(kw_only=True)
class Settings:
greeting: str = "Hello"
@dataclasses.dataclass(kw_only=True)
class GreetingService:
settings: Settings # auto-injected by type
class Dependencies(Group):
settings = providers.Factory(scope=Scope.APP, creator=Settings)
service = providers.Factory(scope=Scope.REQUEST, creator=GreetingService)
mcp = fastmcp.FastMCP("greeter")
container = Container(groups=[Dependencies])
setup_di(mcp, container)
container.validate() # optional fail-fast; must come after setup_di registers its providers
@mcp.tool
def greet(name: str, service: GreetingService = FromDI(Dependencies.service)) -> str: # noqa: B008
return f"{service.settings.greeting}, {name}!"
FromDI must be the default value, not Annotated metadata: FastMCP keeps an Annotated parameter in the schema and asks the client for it. When the parameter's type has no schema, as with a plain class, FastMCP itself refuses the tool when it is defined. When it has one, as with a dataclass, the server raises TypeError at startup naming the parameter. The check covers the server's own tools, resources and prompts, not those of a mounted server or ones added after startup.
To stop ruff's B008 from flagging every FromDI default, add it to your ruff config:
[tool.ruff.lint.flake8-bugbear]
extend-immutable-calls = ["modern_di_fastmcp.FromDI"]
The current fastmcp.Context is resolvable within DI via the pre-built fastmcp_context_provider context provider.
API
| Symbol | Description |
|---|---|
setup_di(server, container, *, manage_lifespan=True) |
Attaches the container to the server and adds the DI middleware. The server's lifespan opens the container at startup and closes it with close_async() at shutdown. When several apps share one container, exactly one of them should own its lifespan: pass manage_lifespan=False to every other setup_di, or the first app to stop closes the container for the rest. At startup it raises TypeError for a FromDI used inside Annotated. Raises RuntimeError when called a second time for the same server. Returns the container |
FromDI(dependency) |
Parameter default that resolves a provider (or type) from the request container. Raises RuntimeError naming setup_di when no request container is active, including in a background task (task=True), which FastMCP runs outside middleware |
fetch_di_container(server) |
Returns the root container attached to the server. Raises RuntimeError when setup_di was not called |
fastmcp_context_provider |
ContextProvider for the current fastmcp.Context (REQUEST scope) |
📦 PyPI
📝 License
Part of modern-python
Built on modern-di, a dependency-injection framework with an IoC container and scopes.
Browse the full list of templates and libraries in
modern-python; the org profile has the categorized index.
Metadata
Release files for modern-di-fastmcp 4.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modern_di_fastmcp-4.0.0.tar.gz | 5.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di_fastmcp-4.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.8 kB
Release files / modern_di_fastmcp-4.0.0.tar.gz
| Download URL | modern_di_fastmcp-4.0.0.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
561e5a60e93837844d6b53ef391f29389888eb231c051bb78bb6944b976245de
|
|
BLAKE2b-256 checksum How to use checksums |
f5af9eef738394d6088a234718bdaefc88938f289460f5e027962b63be687ceb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / modern_di_fastmcp-4.0.0-py3-none-any.whl
| Download URL | modern_di_fastmcp-4.0.0-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
156c702822217d9023815184bb9a1fba6bed68ae32e7416cafb5f13e9cc590ab
|
|
BLAKE2b-256 checksum How to use checksums |
10c848d58960be6260f896a9749f89cf761ffccb7bc04a9b940d68209f8134a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|