Skip to main content

MCP server providing a stateful Django shell for AI assistants.

Project description

mcp-django-shell

PyPI PyPI - Python Version Django Version

A Model Context Protocol (MCP) server providing a stateful Django shell for AI assistants to interact with Django projects.

Requirements

  • Python 3.10, 3.11, 3.12, 3.13
  • Django 4.2, 5.1, 5.2

Installation

  1. Install the package from PyPI.

    python -m pip install mcp-django-shell
    
    # or if you like the new hotness
    
    uv add mcp-django-shell
    uv sync
    
  2. (Optional) Add to your Django project's INSTALLED_APPS if you want to use the management command:

    DEBUG = ...
    
    if DEBUG:
        INSTALLED_APPS.append("mcp_django_shell")
    

    Note: You can now run mcp-django-shell without adding it to INSTALLED_APPS by using python -m mcp_django_shell directly. See below for more info.

[!WARNING]

Only enable in development!

Look, it should go without saying, but I will say it anyway - this gives full shell access to your Django project. Only enable and use this in development and in a project that does not have access to any production data.

LLMs can go off the rails, get spooked by some random error, and in trying to fix things drop a production database.

[!CAUTION]

I'm not kidding, this library just passes the raw Python code an LLM produces straight to a Python environment with full access to the Django project and everything it has access to.

Most LLMs have basic safety protections in place if you ask to delete any data and will refuse to delete production data, but it is pretty trivial to bypass. (Hint: Just tell the LLM it's not production, it's in a development environment, and it will be the bull in a china shop deleting anything you want.)

I suggest using something like django-read-only if you need some CYA protection against this. Or, you know, don't use this in any sensitive environments.

Getting Started

Run the MCP server directly from your Django project directory:

python -m mcp_django_shell

# With explicit settings module
python -m mcp_django_shell --settings myproject.settings

# With debug logging
python -m mcp_django_shell --debug

Or using uv:

uv run -m mcp_django_shell

The server automatically detects DJANGO_SETTINGS_MODULE from your environment. You can override it with --settings or add to your Python path with --pythonpath.

There's also a Django management command if you prefer, but that requires adding mcp-django-shell to INSTALLED_APPS:

python manage.py mcp_shell

Transport

The server supports multiple transport protocols:

# Default: STDIO
python -m mcp_django_shell

# HTTP
python -m mcp_django_shell --transport http --host 127.0.0.1 --port 8000

# SSE
python -m mcp_django_shell --transport sse --host 127.0.0.1 --port 8000

Client Configuration

Configure your MCP client using one of the examples below. The command is the same for all clients, just expressed in annoyingly different JSON soup.

Don't see your client? Submit a PR with setup instructions.

Claude Code

{
  "mcpServers": {
    "django_shell": {
      "command": "python",
      "args": ["-m", "mcp_django_shell"],
      "cwd": "/path/to/your/django/project",
      "env": {
        "DJANGO_SETTINGS_MODULE": "myproject.settings"
      }
    }
  }
}

Opencode

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "django_shell": {
      "type": "local",
      "command": ["python", "-m", "mcp_django_shell"],
      "enabled": true,
      "environment": {
        "DJANGO_SETTINGS_MODULE": "myproject.settings"
      }
    }
  }
}

Features

mcp-django-shell provides an MCP server with a stateful Django shell for AI assistants. It sets up Django, maintains session state between calls, and lets the AI write and execute Python code directly against your project.

It wouldn't be an MCP server README without a gratuitous list of features punctuated by emojis, so:

  • 🐚 Stateful shell - django_shell executes Python code in your Django environment
  • 🔍 Project exploration - MCP resources for discovering apps, models, and configuration
  • 🔄 Persistent state - Imports and variables stick around between calls
  • 🧹 Reset when needed - django_reset clears the session when things get weird
  • 🚀 Zero configuration - No schemas, no settings, just Django
  • 🤖 LLM-friendly - Designed for AI assistants that already know Python
  • 📦 Minimal dependencies - Just FastMCP and Django (you already have Django)
  • 🌐 Multiple transports - STDIO, HTTP, SSE.. It has it all!
  • 🎯 Does one thing well - Runs code. That's it. That's the feature.

Inspired by Armin Ronacher's Your MCP Doesn't Need 30 Tools: It Needs Code.

Resources

Read-only resources are provided for project exploration without executing code (note that resource support varies across MCP clients):

  • django://project - Python environment and Django configuration details
  • django://apps - All installed Django applications with their models
  • django://models - Detailed model information with import paths and field types

The idea is to give just enough information about the project to hopefully guide the LLM assistant and prevent needless shell exploration, allowing it to get straight to work.

Tools

Two tools handle shell operations and session management:

  • django_shell - Execute Python code in a persistent Django shell session
  • django_reset - Reset the session, clearing all variables and imports

Imports and variables persist between calls within the shell tool, so the AI can work iteratively - exploring your models, testing queries, debugging issues.

Development

For detailed instructions on setting up a development environment and contributing to this project, see CONTRIBUTING.md.

For release procedures, see RELEASING.md.

License

mcp-django-shell is licensed under the MIT license. See the LICENSE file for more information.

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

mcp_django_shell-0.7.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

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

mcp_django_shell-0.7.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file mcp_django_shell-0.7.0.tar.gz.

File metadata

  • Download URL: mcp_django_shell-0.7.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.12

File hashes

Hashes for mcp_django_shell-0.7.0.tar.gz
Algorithm Hash digest
SHA256 2ab0d5e5b4caaa995dc7b7dd70512e8ad756a1fba2dc3f2b27657c22e0d8ef9a
MD5 a5a60889a2c89bfaf98cbb04a34aa798
BLAKE2b-256 bf13e7f0c7d3cf6deb20d7b5284ec38fb1e9c24229975ca74c87c3b4a22f5f9c

See more details on using hashes here.

File details

Details for the file mcp_django_shell-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_django_shell-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34a3fb182483b0e65549c42ee249a816e4b1f3f2ec65ddffb9ec166fcfe9ac2d
MD5 e2c28e3a1744993e6e68881984ccd3dd
BLAKE2b-256 296f90266d805a781c87c0959b81a80a21cc420c1977ecfe20cde16f9494568d

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