Skip to main content

A Django package for MCP authentication

Project description

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 mcp management command provided by Django MCPX:

python manage.py mcpx --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

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

django_mcpx-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

django_mcpx-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file django_mcpx-0.1.0.tar.gz.

File metadata

  • Download URL: django_mcpx-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for django_mcpx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 429a51051f70194e226665391f8a800340504eeddbe4d91428ede3dedd3cd72c
MD5 72e7f57ba542d479c31331894b033816
BLAKE2b-256 f7cd51448ce69014204282862d37ab8ef9a439123ec5061555cee770692f8b52

See more details on using hashes here.

File details

Details for the file django_mcpx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_mcpx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for django_mcpx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef411e538b1c676373fbdb4cb29e78e6e07405cae1176bae018ccb9b0950cd02
MD5 81e7349506e78ed435224489171d002d
BLAKE2b-256 ffb2378e6f5092ab0d686d4e5d4f8fca40aa8d48605d54725989d89d86dd52b8

See more details on using hashes here.

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