Metadata-Version: 2.4
Name: fastmcp-injector
Version: 0.1.0
Summary: Dependency injection integration for FastMCP using injector
Author: Ulises Hernandez
License-Expression: MIT
Project-URL: Homepage, https://github.com/uliseshdzc/fastmcp-injector
Project-URL: Repository, https://github.com/uliseshdzc/fastmcp-injector
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastmcp>=2.0.0
Requires-Dist: injector>=0.21.0
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.24; extra == "dev"
Requires-Dist: ruff>=0.4; extra == "dev"
Dynamic: license-file

# FastMCP Injector

Dependency injection integration for [FastMCP](https://github.com/jlowin/fastmcp) using [injector](https://github.com/python-injector/injector).

Inspired by [fastapi-injector](https://github.com/macieyng/fastapi-injector).

## Installation

```bash
pip install fastmcp-injector
```

## Usage

```python
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
