📦
Fastmcp Injector
Integrates injector with FastMCP
0 installs
Trust: 34 — Low
Devtools
Ask AI about Fastmcp Injector
Powered by Claude · Grounded in docs
I know everything about Fastmcp Injector. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Loading tools...
Reviews
Documentation
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: 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.
How it works
attach_injector(mcp, injector)patchesmcp.tool()to intercept tool registration.- Parameters annotated with
Injected[T]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
