Skip to main content

Dependency injection integration for FastMCP using injector

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastmcp_injector-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastmcp_injector-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file fastmcp_injector-0.1.0.tar.gz.

File metadata

  • Download URL: fastmcp_injector-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastmcp_injector-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e136605a0a52e6a28a8ee181e527a9ba53e2151c4d365eec878efd5ccee7200a
MD5 28132d5de6280cf354d0c9952abaaf0e
BLAKE2b-256 e4ad05f43c8bf73ddf74fa3095bbb874e5b50d4ef0717ee54f700c2e29e22c5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastmcp_injector-0.1.0.tar.gz:

Publisher: publish.yaml on uliseshdzc/fastmcp-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastmcp_injector-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastmcp_injector-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21f0d6a7f41177097b70f784d8a8a32cdf1e3e308a6392696f74f49219f1a9ed
MD5 b871ffcc1a9b108346b1fe0c814deee6
BLAKE2b-256 a23c0767e2944fdc47b5d678543e70f627a33d6afc69811efdb94313e98b9a29

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastmcp_injector-0.1.0-py3-none-any.whl:

Publisher: publish.yaml on uliseshdzc/fastmcp-injector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page