Skip to main content

FastMCP Injector

Dependency injection integration for FastMCP using injector.

Inspired by fastapi-injector.

Installation

pip install fastmcp-injector

Usage

from fastmcp import FastMCP
from injector import Injector, inject, singleton

from fastmcp_injector import Injected, attach_injector


# Define your services
class Database:
    @inject
    def __init__(self):
        self._data = {"answer": 42}

    def query(self, q: str) -> str:
        return str(self._data)


# Set up DI and MCP
injector = Injector()
mcp = FastMCP()
attach_injector(mcp, injector)


# Use Injected(T) to declare dependencies — they won't appear in the tool schema
@mcp.tool()
def ask_database(question: str, db: Database = Injected(Database)) -> str:
    return db.query(question)


if __name__ == "__main__":
    mcp.run()

Injected(T) parameters are resolved automatically from the injector container at call time. Only regular parameters (like question: str) are exposed in the MCP tool schema. The type annotation (e.g. db: Database) gives your IDE full autocomplete and type checking.

How it works

  1. attach_injector(mcp, injector) patches mcp.tool() to intercept tool registration.
  2. Parameters with an Injected(T) default value are stripped from the function signature before FastMCP generates the tool schema.
  3. At invocation time, a wrapper resolves those dependencies via injector.get(T) and injects them into the original function.

License

MIT

Metadata

Release files for fastmcp-injector 0.1.0

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

Source distribution (sdist)

Source distribution for fastmcp-injector 0.1.0
File Size Uploaded Provenance Actions
fastmcp_injector-0.1.0.tar.gz 4.4 kB With provenance Details for fastmcp_injector-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastmcp-injector 0.1.0
File Interpreter ABI Platform Actions
fastmcp_injector-0.1.0-py3-none-any.whl Python 3 none any Details for fastmcp_injector-0.1.0-py3-none-any.whl

Total release size: 8.8 kB

Release files / fastmcp_injector-0.1.0.tar.gz

Download URL fastmcp_injector-0.1.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e136605a0a52e6a28a8ee181e527a9ba53e2151c4d365eec878efd5ccee7200a
BLAKE2b-256 checksum
How to use checksums
e4ad05f43c8bf73ddf74fa3095bbb874e5b50d4ef0717ee54f700c2e29e22c5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 18, 2026.

Transparency log

Release files / fastmcp_injector-0.1.0-py3-none-any.whl

Download URL fastmcp_injector-0.1.0-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
21f0d6a7f41177097b70f784d8a8a32cdf1e3e308a6392696f74f49219f1a9ed
BLAKE2b-256 checksum
How to use checksums
a23c0767e2944fdc47b5d678543e70f627a33d6afc69811efdb94313e98b9a29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 18, 2026.

Transparency log

Security for fastmcp-injector 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastmcp_injector-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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