Implementation:1Panel dev MaxKB mcp view
| Knowledge Sources | |
|---|---|
| Domains | MCP, API |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
Concrete Django view function that serves as the MCP JSON-RPC 2.0 endpoint, exposing MaxKB applications as MCP tool servers, provided by MaxKB.
Description
The mcp_view function is a CSRF-exempt Django view that handles MCP (Model Context Protocol) JSON-RPC 2.0 requests. It parses the incoming JSON body, extracts the `method` (`initialize`, `tools/list`, `tools/call`), creates a MCPToolHandler with the Authorization header, and routes to the appropriate handler method. Successful responses are returned as `JsonResponse` with the standard JSON-RPC 2.0 envelope. Unknown methods return `-32601` (method not found); exceptions return `-32603` (internal error).
Usage
Import this view when you need to:
- Expose a MaxKB application as an MCP tool server
- Handle incoming JSON-RPC requests for MCP protocol methods
- Route MCP method calls to application chat execution
Code Reference
Source Location
- Repository: MaxKB
- File: apps/chat/views/mcp.py
Signature
@csrf_exempt
def mcp_view(request) -> HttpResponse | JsonResponse:
"""
Handle MCP JSON-RPC 2.0 requests.
Supported methods:
initialize - Protocol handshake (version, capabilities)
tools/list - List available tools (the application itself)
tools/call - Execute a tool (send message to application chat)
Returns:
JsonResponse with JSON-RPC 2.0 envelope:
Success: {"jsonrpc": "2.0", "id": <id>, "result": <result>}
Error: {"jsonrpc": "2.0", "id": <id>, "error": {"code": <code>, "message": <msg>}}
"""
Import
from chat.views.mcp import mcp_view
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| request.body | str | Yes | JSON-RPC 2.0 body with method, params, id |
| request.headers['Authorization'] | str | Yes | Bearer API key for the application |
| request.headers['X-MaxKB-Chat-Files'] | str | No | Base64-encoded file list |
| request.headers['X-MaxKB-Form-Data'] | str | No | Base64-encoded form data |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | JsonResponse | JSON-RPC 2.0 envelope with result or error |
Usage Examples
URL Configuration
# In apps/chat/urls.py
from django.urls import path
from chat.views.mcp import mcp_view
urlpatterns = [
path('mcp', mcp_view, name='mcp'),
]
Example JSON-RPC Requests
# Initialize
POST /chat/api/mcp
{"jsonrpc": "2.0", "method": "initialize", "id": 1}
# Response: {"jsonrpc": "2.0", "id": 1, "result": {"protocolVersion": "2025-06-18", ...}}
# List tools
POST /chat/api/mcp
{"jsonrpc": "2.0", "method": "tools/list", "id": 2}
# Response: {"jsonrpc": "2.0", "id": 2, "result": {"tools": [...]}}
# Call tool
POST /chat/api/mcp
Authorization: Bearer <api-key>
{"jsonrpc": "2.0", "method": "tools/call", "params": {"arguments": {"message": "Hello"}}, "id": 3}
# Response: {"jsonrpc": "2.0", "id": 3, "result": {"content": [{"type": "text", "text": "..."}]}}