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
attach_injector(mcp, injector)patchesmcp.tool()to intercept tool registration.- Parameters with an
Injected(T)default value are stripped from the function signature before FastMCP generates the tool schema. - At invocation time, a wrapper resolves those dependencies via
injector.get(T)and injects them into the original function.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e136605a0a52e6a28a8ee181e527a9ba53e2151c4d365eec878efd5ccee7200a
|
|
| MD5 |
28132d5de6280cf354d0c9952abaaf0e
|
|
| BLAKE2b-256 |
e4ad05f43c8bf73ddf74fa3095bbb874e5b50d4ef0717ee54f700c2e29e22c5a
|
Provenance
The following attestation bundles were made for fastmcp_injector-0.1.0.tar.gz:
Publisher:
publish.yaml on uliseshdzc/fastmcp-injector
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastmcp_injector-0.1.0.tar.gz -
Subject digest:
e136605a0a52e6a28a8ee181e527a9ba53e2151c4d365eec878efd5ccee7200a - Sigstore transparency entry: 1566403058
- Sigstore integration time:
-
Permalink:
uliseshdzc/fastmcp-injector@80f518b54022a89d208ea7d875d600516cab4b2d -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/uliseshdzc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@80f518b54022a89d208ea7d875d600516cab4b2d -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastmcp_injector-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastmcp_injector-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21f0d6a7f41177097b70f784d8a8a32cdf1e3e308a6392696f74f49219f1a9ed
|
|
| MD5 |
b871ffcc1a9b108346b1fe0c814deee6
|
|
| BLAKE2b-256 |
a23c0767e2944fdc47b5d678543e70f627a33d6afc69811efdb94313e98b9a29
|
Provenance
The following attestation bundles were made for fastmcp_injector-0.1.0-py3-none-any.whl:
Publisher:
publish.yaml on uliseshdzc/fastmcp-injector
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastmcp_injector-0.1.0-py3-none-any.whl -
Subject digest:
21f0d6a7f41177097b70f784d8a8a32cdf1e3e308a6392696f74f49219f1a9ed - Sigstore transparency entry: 1566403066
- Sigstore integration time:
-
Permalink:
uliseshdzc/fastmcp-injector@80f518b54022a89d208ea7d875d600516cab4b2d -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/uliseshdzc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@80f518b54022a89d208ea7d875d600516cab4b2d -
Trigger Event:
release
-
Statement type: