Implementation:1Panel dev MaxKB MCPToolHandler
| Knowledge Sources | |
|---|---|
| Domains | MCP, Authentication |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
Concrete handler class for MCP protocol method routing, API key authentication, and tool execution via application chat, provided by MaxKB.
Description
The MCPToolHandler class authenticates incoming MCP requests using a ApplicationApiKey, validates the application is published, and routes JSON-RPC methods to their implementations:
- initialize() — Returns protocol version `2025-06-18` and server capabilities
- list_tools() — Returns the application as a single MCP tool with `inputSchema` for a `message` parameter
- call_tool(params) — Creates a chat session via ChatSerializers, streams the response, concatenates text chunks, strips `<tool_calls_render>` tags, and returns the result as MCP content
The handler also decodes base64-encoded chat files and form data from headers, forwarding them to the application chat pipeline.
Usage
Import this class when you need to:
- Handle MCP protocol method routing for an application
- Authenticate MCP requests via API key
- Execute application chat through the MCP interface
Code Reference
Source Location
- Repository: MaxKB
- File: apps/chat/mcp/tools.py
Signature
class MCPToolHandler:
def __init__(
self,
auth_header: str,
chat_files_header: str = None,
form_data: str = None,
):
"""Authenticate via ApplicationApiKey and load application."""
def initialize(self) -> Dict:
"""Return MCP protocol version and capabilities."""
def list_tools(self) -> Dict:
"""Return the application as an MCP tool schema."""
def call_tool(self, params: Dict) -> Dict:
"""Execute application chat and return text content."""
def build_description(self) -> str:
"""Build tool description with file context info."""
@staticmethod
def decode_chat_files(chat_files_header: str) -> Dict:
"""Decode base64-encoded file list from header."""
@staticmethod
def decode_form_data(form_data: str) -> Dict:
"""Decode base64-encoded form data from header."""
Import
from chat.mcp.tools import MCPToolHandler
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| auth_header | str | Yes | API key from Authorization header (Bearer token) |
| chat_files_header | str | No | Base64-encoded file list (X-MaxKB-Chat-Files) |
| form_data | str | No | Base64-encoded form data (X-MaxKB-Form-Data) |
| params['arguments']['message'] | str | Yes (for call_tool) | User message text |
Outputs
| Name | Type | Description |
|---|---|---|
| initialize() returns | Dict | Protocol version, serverInfo, capabilities |
| list_tools() returns | Dict | Tool schema with name, description, inputSchema |
| call_tool() returns | Dict | {"content": [{"type": "text", "text": "..."}]} |
Usage Examples
Handling MCP Methods
from chat.mcp.tools import MCPToolHandler
# Initialize handler (authenticates via API key)
handler = MCPToolHandler(
auth_header="sk-xxxxx",
chat_files_header=None,
form_data=None,
)
# Initialize protocol handshake
result = handler.initialize()
# {"protocolVersion": "2025-06-18", "serverInfo": {"name": "maxkb-mcp", "version": "1.0.0"}, ...}
# List available tools
tools = handler.list_tools()
# {"tools": [{"name": "agent_<app-id>", "description": "...", "inputSchema": {...}}]}
# Call tool (execute chat)
response = handler.call_tool({"arguments": {"message": "What is RAG?"}})
# {"content": [{"type": "text", "text": "RAG stands for Retrieval-Augmented Generation..."}]}