MCP Integration
Model Context Protocol (MCP) integration allows extensions to register custom AI agent capabilities that integrate seamlessly with Superset's MCP service. Extensions can provide both tools (executable functions) and prompts (interactive guidance) that AI agents can discover and use.
What is MCP?
MCP enables extensions to extend Superset's AI capabilities in two ways:
MCP Tools
Tools are Python functions that AI agents can call to perform specific tasks. They provide executable functionality that extends Superset's capabilities.
Examples of MCP tools:
- Data processing and transformation functions
- Custom analytics calculations
- Integration with external APIs
- Specialized report generation
- Business-specific operations
MCP Prompts
Prompts provide interactive guidance and context to AI agents. They help agents understand how to better assist users with specific workflows or domain knowledge.
Examples of MCP prompts:
- Step-by-step workflow guidance
- Domain-specific context and knowledge
- Interactive troubleshooting assistance
- Template generation helpers
- Best practices recommendations
Getting Started
MCP Tools
Basic Tool Registration
The simplest way to create an MCP tool is using the @tool decorator:
from superset_core.mcp import tool
@tool
def hello_world() -> dict:
"""A simple greeting tool."""
return {"message": "Hello from my extension!"}
This creates a tool that AI agents can call by name. The tool name defaults to the function name.
Decorator Parameters
The @tool decorator accepts several optional parameters:
Parameter details:
name: Tool identifier (AI agents use this to call your tool)description: Explains what the tool does (helps AI agents decide when to use it)tags: Categories for organization and discoveryprotect: Whether the tool requires user authentication (defaults toTrue)
Naming Your Tools
For extensions, include your extension ID in tool names to avoid conflicts:
Complete Example
Here's a more comprehensive example showing best practices:
# backend/mcp_tools.py
import random
from datetime import datetime, timezone
from pydantic import BaseModel, Field
from superset_core.mcp import tool
class RandomNumberRequest(BaseModel):
"""Request schema for random number generation."""
min_value: int = Field(
description="Minimum value (inclusive) for random number generation",
ge=-2147483648,
le=2147483647
)
max_value: int = Field(
description="Maximum value (inclusive) for random number generation",
ge=-2147483648,
le=2147483647
)
@tool(
name="example_extension.random_number",
tags=["extension", "utility", "random", "generator"]
)
def random_number_generator(request: RandomNumberRequest) -> dict:
"""
Generate a random integer between specified bounds.
This tool validates input ranges and provides detailed error messages
for invalid requests.
"""
# Validate business logic (Pydantic handles type/range validation)
if request.min_value > request.max_value:
return {
"status": "error",
"error": f"min_value ({request.min_value}) cannot be greater than max_value ({request.max_value})",
"timestamp": datetime.now(timezone.utc).isoformat()
}
# Generate random number
result = random.randint(request.min_value, request.max_value)
return {
"status": "success",
"random_number": result,
"min_value": request.min_value,
"max_value": request.max_value,
"range_size": request.max_value - request.min_value + 1,
"timestamp": datetime.now(timezone.utc).isoformat()
}
Best Practices
Response Format
Use consistent response structures:
# Success response
{
"status": "success",
"result": "your_data_here",
"timestamp": "2024-01-01T00:00:00Z"
}
# Error response
{
"status": "error",
"error": "Clear error message",
"timestamp": "2024-01-01T00:00:00Z"
}
Documentation
Write clear descriptions and docstrings:
@tool(
name="my_extension.process_data",
description="Process customer data and generate insights. Requires valid customer ID and date range.",
tags=["analytics", "customer", "reporting"]
)
def process_data(customer_id: int, start_date: str, end_date: str) -> dict:
"""
Process customer data for the specified date range.
This tool analyzes customer behavior patterns and generates
actionable insights for business decision-making.
Args:
customer_id: Unique customer identifier
start_date: Analysis start date (YYYY-MM-DD format)
end_date: Analysis end date (YYYY-MM-DD format)
Returns:
Dictionary containing analysis results and recommendations
"""
# Implementation here
pass
Tool Naming
- Extension tools: Use prefixed names like
my_extension.tool_name - Descriptive names:
calculate_tax_amountvscalculate - Consistent naming: Follow patterns within your extension
How AI Agents Use Your Tools
Once registered, AI agents can discover and use your tools automatically:
User: "Generate a random number between 1 and 100"
Agent: I'll use the random number generator tool.
→ Calls: example_extension.random_number(min_value=1, max_value=100)
← Returns: {"status": "success", "random_number": 42, ...}
Agent: I generated the number 42 for you.
The AI agent sees your tool's:
- Name: How to call it
- Description: What it does and when to use it
- Parameters: What inputs it expects (from Pydantic schema)
- Tags: Categories for discovery
Troubleshooting
Tool Not Available to AI Agents
- Check extension registration: Verify your tool module is listed in extension entrypoints
- Verify decorator: Ensure
@toolis correctly applied - Extension loading: Confirm your extension is installed and enabled
Input Validation Errors
- Pydantic models: Ensure field types match expected inputs
- Field constraints: Check min/max values and string lengths are reasonable
- Required fields: Verify which parameters are required vs optional
Runtime Issues
- Error handling: Add try/catch blocks with clear error messages
- Response format: Use consistent status/error/timestamp structure
- Testing: Test your tools with various input scenarios
Development Tips
- Start simple: Begin with basic tools, add complexity gradually
- Test locally: Use MCP clients (like Claude Desktop) to test your tools
- Clear descriptions: Write tool descriptions as if explaining to a new user
- Meaningful tags: Use tags that help categorize and discover tools
- Error messages: Provide specific, actionable error messages