### Setup Development Environment Source: https://github.com/bagowix/dishka-fastmcp/blob/main/CONTRIBUTING.md Clone the repository, navigate to the directory, and use uv to sync dependencies and install git hooks. ```bash git clone https://github.com/bagowix/dishka-fastmcp cd dishka-fastmcp uv sync # creates the venv and installs dev dependencies uv run prek install # one-time: installs the git hooks ``` -------------------------------- ### Basic Dishka-FastMCP Setup and Tool Injection Source: https://github.com/bagowix/dishka-fastmcp/blob/main/README.md This snippet shows the fundamental setup for integrating Dishka with FastMCP. It defines a service (Catalog), a Dishka provider, creates a container, and initializes FastMCP with Dishka's lifespan management. A tool (`get_price`) is defined with dependency injection using `FromDishka`, demonstrating how services are automatically resolved per request. ```python from dishka import Provider, Scope, make_async_container, provide from fastmcp import FastMCP from dishka_fastmcp import FromDishka, dishka_lifespan, inject, setup_dishka class Catalog: _prices: dict[str, int] = {'book': 12, 'pen': 2} def price(self, item: str) -> int: return self._prices.get(item, 0) class AppProvider(Provider): catalog = provide(Catalog, scope=Scope.REQUEST) container = make_async_container(AppProvider()) mcp = FastMCP('shop', lifespan=dishka_lifespan(container)) setup_dishka(container, mcp) @mcp.tool @inject async def get_price(item: str, catalog: FromDishka[Catalog]) -> int: return catalog.price(item) if __name__ == '__main__': mcp.run() ``` -------------------------------- ### Install dishka-fastmcp Source: https://github.com/bagowix/dishka-fastmcp/blob/main/README.md Install the dishka-fastmcp package using uv or pip. Ensure you have the required versions of Python, dishka, and fastmcp. ```bash uv add dishka-fastmcp # or: pip install dishka-fastmcp ``` -------------------------------- ### Basic dishka-fastmcp Setup and Tool Usage Source: https://github.com/bagowix/dishka-fastmcp/blob/main/docs/llms.txt Demonstrates how to set up dishka with FastMCP and define an injected tool. Ensure `@inject` is placed below the FastMCP decorator and `setup_dishka` is called before server execution. ```python from dishka import Provider, Scope, make_async_container, provide from fastmcp import FastMCP from dishka_fastmcp import FromDishka, dishka_lifespan, inject, setup_dishka class Catalog: _prices = {'book': 12, 'pen': 2} def price(self, item: str) -> int: return self._prices.get(item, 0) class AppProvider(Provider): catalog = provide(Catalog, scope=Scope.REQUEST) container = make_async_container(AppProvider()) mcp = FastMCP('shop', lifespan=dishka_lifespan(container)) setup_dishka(container, mcp) @mcp.tool @inject async def get_price(item: str, catalog: FromDishka[Catalog]) -> int: return catalog.price(item) # client sees only `item` in the schema ``` -------------------------------- ### Setting up Dishka Container for Sync Tools Source: https://github.com/bagowix/dishka-fastmcp/blob/main/README.md Illustrates the setup of a sync Dishka container using `make_container` and `setup_dishka` for use with sync FastMCP tools. Ensures correct propagation of the request container into worker threads. ```python from dishka import make_container container = make_container(AppProvider()) setup_dishka(container, mcp) ``` -------------------------------- ### Setting up Async Dishka Container with FastMCPProvider Source: https://github.com/bagowix/dishka-fastmcp/blob/main/README.md Shows how to create an async Dishka container that includes `FastMCPProvider` to expose FastMCP objects like the request context to dependencies. Use `make_async_container` for async handlers. ```python from fastmcp.server.context import Context from dishka_fastmcp import FastMCPProvider container = make_async_container(AppProvider(), FastMCPProvider()) @mcp.tool @inject async def notify(message: str, ctx: FromDishka[Context]) -> None: await ctx.info(message) ``` -------------------------------- ### Run Project Checks Source: https://github.com/bagowix/dishka-fastmcp/blob/main/CONTRIBUTING.md Execute formatting, linting, type checking, and tests locally using uv to ensure code quality and correctness. ```bash uv run ruff format --check # formatting uv run ruff check # linting uv run mypy # type checking (strict) uv run pyright # type checking (strict, second checker) uv run pytest --cov # tests with coverage (threshold: 100%) ``` -------------------------------- ### Injecting Dependencies into FastMCP Prompts Source: https://github.com/bagowix/dishka-fastmcp/blob/main/README.md Shows how to use the `@inject` decorator with FastMCP prompts for dependency resolution. Requires `@mcp.prompt` decorator. ```python @mcp.prompt @inject async def summarize(text: str, summarizer: FromDishka[Summarizer]) -> str: return await summarizer.run(text) ``` -------------------------------- ### Injecting Dependencies into FastMCP Sync Tools Source: https://github.com/bagowix/dishka-fastmcp/blob/main/README.md Demonstrates injecting dependencies into a synchronous FastMCP tool using the `@inject` decorator. The tool runs in a worker thread by default. ```python @mcp.tool @inject def compute(x: int, service: FromDishka[Calculator]) -> int: return service.square(x) ``` -------------------------------- ### Injecting Dependencies into FastMCP Resources Source: https://github.com/bagowix/dishka-fastmcp/blob/main/README.md Demonstrates how to use the `@inject` decorator with FastMCP resources to resolve dependencies per operation. Requires `@mcp.resource` decorator. ```python @mcp.resource('users://{user_id}') @inject async def user(user_id: str, repo: FromDishka[UserRepo]) -> dict: return await repo.get(user_id) ``` -------------------------------- ### Run a Single Test Source: https://github.com/bagowix/dishka-fastmcp/blob/main/CONTRIBUTING.md Execute a specific test case using uv and pytest to isolate and verify a particular functionality. ```bash uv run pytest tests/test_tools.py::test_async_tool_injects_and_hides_deps_from_schema ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.