Skip to main content

Django MCPX

Compose MCP servers in Django using FastMcp

Installation

pip install django-mcpx

Usage

1. Configuration

First, you need to add mcpx to your INSTALLED_APPS in your Django settings file (settings.py):

INSTALLED_APPS = [
    # ... other installed apps ...
    'mcpx',
]

Next, configure the MCP settings in your settings.py. Here is an example configuration:

MCP_SERVERS = ["myapp.mcpserver.mcp_server"]

MCP_AUTH = "fefe865fe4856ferqsijjfhe-fre5qxpokjnEEZ5" # your auth token
MCP_PORT = 8397
MCP_HOST = "localhost"

2. Example Implementation

Let's create a simple MCP server using the User model from Django's authentication system.

Create a file named mcpserver.py in your app:

from typing import Any, Dict
from fastmcp import FastMCP
from pydantic import Field
from django.core.exceptions import PermissionDenied
from mcpx.auth import mcp_auth
from django.contrib.auth.models import User

name = "User Mcp Server"
mcp = FastMCP(name=name)


@mcp.tool()
async def get_user_by_username(
    username: str = Field(description="The username of the user to retrieve")
) -> Dict[str, Any]:
    """Retrieve a user by their username"""
    try:
        mcp_auth()
    except PermissionDenied as e:
        print(f"Mcp authentication error: {e}")
        return {"error": "undefined"}
    
    try:
        user = await User.objects.aget(username=username)
        return {"result": {"username": user.username, "email": user.email}}
    except User.DoesNotExist:
        return {"error": "User not found"}


@mcp.tool()
async def list_users(
    limit: int = Field(description="Maximum number of users to retrieve", default=10),
) -> Dict[str, Any]:
    """List all users"""
    try:
        mcp_auth()
    except PermissionDenied as e:
        print(f"Mcp authentication error: {e}")
        return {"error": "undefined"}
    
    results = []
    async for user in User.objects.all()[:limit]:
        results.append({"username": user.username, "email": user.email})
    return {"result": results}


mcp_server = {
    "name": name,
    "mcp": mcp,
}

3. Running the MCP Server

To run the MCP servers listed in settings use the runmcp management command provided by Django MCPX:

python manage.py runmcp --host localhost --port 8397

This will start the MCP servers on localhost at port 8397.

Test it with a client or Mcp inspector:

{
    "My Mcp server": {
      "command": "npx",
      "args": [
        "mcp-remote",
        "http://localhost:8397/mcp",
        "--header",
        "Authorization: Bearer fefe865fe4856ferqsijjfhe-fre5qxpokjnEEZ5"
      ]
    }
}

Warning

Use with caution: you are responsible for what data and what powers you give to which model

Metadata

Release files for django-mcpx 0.1.2

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-mcpx 0.1.2
File Size Uploaded
django_mcpx-0.1.2.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-mcpx 0.1.2
File Interpreter ABI Platform
django_mcpx-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.4 kB

Release files / django_mcpx-0.1.2.tar.gz

Download URL django_mcpx-0.1.2.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8d34a3abf022c810d202f73214ddbe7dea7a0a5c3883f4dc2d0f21a5ea6bbe2e
BLAKE2b-256 checksum
How to use checksums
a47f7c611acc6dbe9ac45e2f0245a75183fb7f75b21de882c9bc40d7bce0da89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.0

Release files / django_mcpx-0.1.2-py3-none-any.whl

Download URL django_mcpx-0.1.2-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc8372ac1e2c1875ba3bd3ab7b44aa56daa64fec0121c286d0bc3b8ee04d4fa2
BLAKE2b-256 checksum
How to use checksums
c4373e662f264c7814a27182dd3574a068e33ab878b611504942cd70a261df27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.0

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

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