Skip to main content

PyPI version Supported Python versions Downloads Coverage CI License GitHub stars uv Ruff ty

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.

Shutdown order

setup_di closes the root container from a provider lifespan it adds to the server. Providers added after setup_di tear down before the close and can still resolve from the container. These run after the close and get ContainerClosedError:

  • the teardown of FastMCP(lifespan=...)
  • the teardown of providers added before setup_di

For these, pass manage_lifespan=False and open and close the container in your own server lifespan:

import collections.abc
import contextlib


@contextlib.asynccontextmanager
async def lifespan(server: fastmcp.FastMCP) -> collections.abc.AsyncIterator[None]:
    async with container:
        yield
        ...  # shutdown code that still needs the container


mcp = fastmcp.FastMCP("greeter", lifespan=lifespan)
container = Container(groups=[Dependencies])
setup_di(mcp, container, manage_lifespan=False)

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 the server is mounted in another app whose lifespan runs it, let that app own the container and pass manage_lifespan=False here (see Sharing a container with FastAPI). When apps that share a container run with separate lifecycles, pass manage_lifespan=False to every setup_di and open and close the container yourself with async with container:, or the first app to stop closes it 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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modern-di-fastmcp 4.0.1
File Size Uploaded
modern_di_fastmcp-4.0.1.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for modern-di-fastmcp 4.0.1
File Interpreter ABI Platform
modern_di_fastmcp-4.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.4 kB

Release files / modern_di_fastmcp-4.0.1.tar.gz

Download URL modern_di_fastmcp-4.0.1.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
137940955fa09a224516be522892231c4badf4bedff4bca727d219a62d9fc5cd
BLAKE2b-256 checksum
How to use checksums
e664dd9cff28b5948845dd7d3b885103b50a8bb70ed7cbc55dadbd2a37e1d47a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","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.1-py3-none-any.whl

Download URL modern_di_fastmcp-4.0.1-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d61f45e7b3fc7a92fb8e2946787128512a2ce9c37ed80e2372e57ae1c4aa0df
BLAKE2b-256 checksum
How to use checksums
832a8910302979c543cf3437fb572bf1071f1134cb220cf5b10338253c25b1c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","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 history Release notifications | RSS feed

4.0.3

2 release files

4.0.2

2 release files

This release

4.0.1 This release

2 release files

4.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page