Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Ninja MCP

PyPI version Python Versions License

Automatic Model Context Protocol (MCP) server generator for Django Ninja applications. Huge credit to FastAPI-MCP for the original idea and implementation.

What is MCP?

The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to Large Language Models (LLMs). Think of MCP like a USB-C port for AI applications - it provides a standardized way to connect AI models to different data sources and tools.

MCP helps you build agents and complex workflows on top of LLMs by providing:

  • Pre-built integrations that your LLM can plug into
  • Flexibility to switch between LLM providers
  • Best practices for securing your data within your infrastructure

What is Ninja MCP?

Ninja MCP is a library that automatically converts your Django Ninja API endpoints into MCP tools. This allows LLM clients like Claude to interact with your Django application directly through a standardized protocol.

Features

  • Automatic Tool Generation: Convert your Django Ninja API endpoints to MCP tools automatically
  • OpenAPI Integration: Leverages your API's OpenAPI schema for rich tool descriptions
  • SSE Transport: Built-in Server-Sent Events (SSE) transport for real-time communication
  • Filtering Options: Include or exclude specific operations or tags
  • Customizable Descriptions: Control the level of detail in tool descriptions

Quick Start

Here's a simple example of integrating Ninja MCP with your Django project:

from django.urls import path
from ninja import NinjaAPI
from ninja_mcp import NinjaMCP

# Create your Ninja API as usual
api = NinjaAPI()

@api.get("/hello")
def hello(request):
    return {"message": "Hello, world!"}

@api.get("/greet/{name}")
def greet(request, name: str):
    return {"message": f"Hello, {name}!"}

# Create the MCP server from your API
mcp_server = NinjaMCP(
    ninja=api,
    base_url="http://localhost:8000/api",
    name="My API",
    description="My awesome API with MCP integration"
)

# Mount the MCP server to your API
mcp_server.mount(api, mount_path="/mcp")

# Include in URLconf
urlpatterns = [
    path("api/", api.urls),
]

With this setup, your Django Ninja API is now available as MCP tools at /api/mcp. LLM clients that support MCP can connect to this endpoint and use your API's functionality as tools.

MCP Architecture

Your Django application serves as an MCP server that can be connected to by MCP clients (like Claude Desktop). The client-server architecture allows:

  1. LLM clients to discover the available tools from your API
  2. Tools to be called by the LLM with appropriate parameters
  3. Results to be returned to the LLM in a standardized format

Advanced Usage

Customizing Tool Generation

mcp_server = NinjaMCP(
    ninja=api,
    base_url="http://localhost:8000/api",
    name="My API",
    description="My awesome API with MCP integration",
    describe_all_responses=True,  # Include all response types in descriptions
    describe_full_response_schema=True,  # Include full response schemas
    include_operations=["get_users", "create_user"],  # Only include specific operations
    # exclude_operations=["delete_user"],  # Exclude specific operations
    include_tags=["users"],  # Only include operations with specific tags
    # exclude_tags=["admin"],  # Exclude operations with specific tags
)

Custom Mount Path

You can mount the MCP server at a custom path:

mcp_server.mount(api, mount_path="/custom/mcp/path")

Mounting to a Different Router

You can mount the MCP server to a different router than the one it was created from:

admin_api = NinjaAPI(urls_namespace="admin")
mcp_server.mount(admin_api, mount_path="/admin/mcp")

Requirements

  • Python 3.10+
  • Django
  • Django Ninja
  • httpx
  • mcp (Model Context Protocol Python library)

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Further Reading

Metadata

Release files for django-ninja-mcp 0.0.1a2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-ninja-mcp 0.0.1a2
File Size Uploaded
django_ninja_mcp-0.0.1a2.tar.gz 78.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-ninja-mcp 0.0.1a2
File Interpreter ABI Platform
django_ninja_mcp-0.0.1a2-py3-none-any.whl Python 3 none any Details

Total release size: 95.6 kB

Release files / django_ninja_mcp-0.0.1a2.tar.gz

Download URL django_ninja_mcp-0.0.1a2.tar.gz
Size 78.8 kB
Tags Source
SHA-256 checksum
How to use checksums
468d902c91bcc26d382a4171ad7f5b0a60b1598ce0360e87ea47d32ab11abf82
BLAKE2b-256 checksum
How to use checksums
d82cc2fa4ef46535b611656c67eedb96567bc8dc39b87d02ca8e05011b8d16de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.6.14

Release files / django_ninja_mcp-0.0.1a2-py3-none-any.whl

Download URL django_ninja_mcp-0.0.1a2-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f09c2f3b134a0176242e2b95a25b4ab24c8aceb671412bda48d471f85970466
BLAKE2b-256 checksum
How to use checksums
fe1fdfaa10c6842d071b8de0560eb73f126dd276b7f444b29b6a8790baf27fa4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.6.14

Release history Release notifications | RSS feed

This release

0.0.1a2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page