MCP Support

djangosdk supports the Model Context Protocol (MCP)arrow-up-right — the emerging standard for connecting AI agents to external tools and data sources.

Overview

MCP allows AI agents to:

  • Call tools hosted on remote MCP servers

  • Access resources (files, databases, APIs) via a standardized protocol

  • Expose Django endpoints as MCP servers for other agents to use

Connecting to MCP Servers

Add mcp_servers to your agent class. Each entry is a connection config:

class ResearchAgent(Agent):
    provider = "openai"
    model = "gpt-4.1"
    mcp_servers = [
        # HTTP MCP server
        {
            "url": "https://mcp.example.com/server",
            "transport": "http",
        },
        # Local MCP server via stdio
        {
            "command": "npx",
            "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
        },
    ]

When the agent handles a request, it automatically:

  1. Fetches tool schemas from all configured MCP servers

  2. Includes them alongside the agent's own tools

  3. Routes tool calls to the correct MCP server

Exposing a Django App as an MCP Server

Use @mcp_tool and @mcp_resource decorators to expose Django functionality via MCP:

Mount the MCP server in your URL configuration:

MCP Transport

djangosdk supports two MCP transport types:

Transport
Use case

http

Remote MCP servers over HTTPS

stdio

Local MCP servers launched as subprocesses

Error Handling

MCP tool failures are non-fatal by default. If an MCP server is unreachable or returns an error, the failure is silently logged and the agent continues without that server's tools.

To make MCP failures fatal:

Last updated

Was this helpful?