Skip to main content

Dishka dependency injection integration for FastMCP

Project description

FastMCP Dishka

CI PyPI Python License Ruff Coverage

Integration package for using dishka dependency injection with FastMCP tools, resources, and prompts.

It provides:

  • FastMCPProvider with FastMCP request context objects for dishka.
  • setup_dishka() middleware that opens a Scope.REQUEST container per tool/resource/prompt call.
  • @inject support for parameters marked as FromDishka[T].

Usage

from dishka import Provider, Scope, make_async_container, provide
from fastmcp import FastMCP
from fastmcp_dishka import FastMCPProvider, FromDishka, inject, setup_dishka


class GreetingService:
    def greet(self, name: str) -> str:
        return f"Hello, {name}!"


class AppProvider(Provider):
    greeting = provide(GreetingService, scope=Scope.REQUEST)


mcp = FastMCP("GreetMCP")
container = make_async_container(AppProvider(), FastMCPProvider())
setup_dishka(container, mcp)


@mcp.tool
@inject
async def greet(name: str, service: FromDishka[GreetingService]) -> str:
    return service.greet(name)

Place @inject below FastMCP decorators so FastMCP registers the wrapped function signature.

@mcp.prompt defines a reusable prompt template exposed to MCP clients. A prompt does not perform an action like a tool; it returns text or chat messages that a client can insert into an LLM conversation.

@mcp.prompt
@inject
async def welcome_prompt(
    name: str,
    service: FromDishka[GreetingService],
) -> str:
    return f"Write a short welcome message for: {service.greet(name)}"

See examples/fastmcp_app.py for a complete FastMCP server with a tool, resource, prompt, and Dishka providers.

Development

Install development dependencies:

uv sync --dev

Run tests with coverage:

make test

Run linting and formatting:

make lint
make format

Install pre-commit hooks:

make pre-commit-install

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_dishka-0.2.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

fastmcp_dishka-0.2.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file fastmcp_dishka-0.2.0.tar.gz.

File metadata

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

File hashes

Hashes for fastmcp_dishka-0.2.0.tar.gz
Algorithm Hash digest
SHA256 05564f325817f526e32a725f64c4268e2764181e0587f94d98fa429cd2396b8e
MD5 96bd77fa390cafc3ea26dff331dc4f3c
BLAKE2b-256 bb012318f71c7b98e327cb61c45d24612d40a8674e181442f4e400c9a0549a21

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastmcp_dishka-0.2.0.tar.gz:

Publisher: ci.yml on vfaddey/fastmcp-dishka

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_dishka-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: fastmcp_dishka-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastmcp_dishka-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 86fad7887e7e72f7a175d742672f08e19dc81fcc0a0101e04c7f29e2c3ac17ae
MD5 7d00ff01102fdf82897e04bdafcc14dd
BLAKE2b-256 25dc6d35ff86b829dab4dbbc3ab7c274f90f43c79870208caaa7ee88e9767bba

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastmcp_dishka-0.2.0-py3-none-any.whl:

Publisher: ci.yml on vfaddey/fastmcp-dishka

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