MCP Router
Automatically selects the optimal LLM model for each task in Cursor IDE by analyzing query complexity, task type, and applying customizable routing strategies across 17 different AI models.
README
🚀 MCP Router
Intelligent Model Context Protocol Router for Cursor IDE
Automatically selects the optimal LLM model for each task based on query analysis, complexity, and your preferred strategy.
📐 System Architecture
┌─────────────────────────────────────────────────────────────────────────────┐
│ CURSOR IDE │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ User Query │ │
│ │ "Refactor this authentication system across multiple files" │ │
│ └──────────────────────────────┬─────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ MCP Router Server │ │
│ │ ┌──────────────────┐ ┌───────────────────┐ ┌──────────────────┐ │ │
│ │ │ Query Analyzer │───▶│ Model Scorer │──▶│ Routing Decision │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ • Task Type │ │ • Quality Score │ │ • Selected Model │ │ │
│ │ │ • Complexity │ │ • Cost Score │ │ • Confidence │ │ │
│ │ │ • Requirements │ │ • Speed Score │ │ • Reasoning │ │ │
│ │ │ • Token Estimate │ │ • Strategy Weight │ │ • Alternatives │ │ │
│ │ └──────────────────┘ └───────────────────┘ └──────────────────┘ │ │
│ └──────────────────────────────┬─────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ Model Registry (17 Models) │ │
│ │ │ │
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────────┐ │ │
│ │ │ FLAGSHIP │ │ REASONING │ │ NATIVE/FAST │ │ BUDGET/LEGACY │ │ │
│ │ │ │ │ │ │ │ │ │ │ │
│ │ │ • GPT-5.2 │ │ • o3 │ │ • Composer1 │ │ • GPT-4o-mini │ │ │
│ │ │ • Claude4.5 │ │ • o3-mini │ │ • Gemini 3 │ │ • Claude Haiku │ │ │
│ │ │ Opus │ │ • Claude3.7 │ │ Pro/Flash │ │ • DeepSeek V3 │ │ │
│ │ │ • Claude4.5 │ │ Sonnet │ │ │ │ • DeepSeek R1 │ │ │
│ │ │ Sonnet │ │ │ │ │ │ │ │ │
│ │ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────────┘ │ │
│ └──────────────────────────────┬─────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ Cursor Executes Query │ │
│ │ (Using its own API keys for selected model) │ │
│ └────────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
Data Flow
┌──────────┐ ┌──────────────┐ ┌───────────────┐ ┌────────────┐
│ Query │─────▶│ Analyze │─────▶│ Score │─────▶│ Recommend │
└──────────┘ └──────────────┘ └───────────────┘ └────────────┘
│ │ │
▼ ▼ ▼
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Task Type: │ │ Apply │ │ Model: │
│ • reasoning │ │ Strategy: │ │ Claude 4.5 │
│ • code_gen │ │ • balanced │ │ Sonnet │
│ • edit │ │ • quality │ │ │
│ Complexity: │ │ • speed │ │ Confidence: │
│ • medium │ │ • cost │ │ 88.45% │
└─────────────┘ └─────────────┘ └─────────────┘
✨ Features
| Feature | Description |
|---|---|
| 🤖 Intelligent Routing | Automatically selects the best model based on query analysis |
| 🧠 Context-Aware Routing | Uses chat history and conversation context for smarter model selection |
| 📊 4 Routing Strategies | balanced / cost / speed / quality |
| 🔍 Query Analysis | Detects task type, complexity, and special requirements |
| 💬 Chat History Analysis | Analyzes conversation patterns, topics, files, languages, and complexity |
| 💰 Cost Estimation | Estimates costs before execution |
| ⚡ 17 Models | Latest 2025 models from OpenAI, Anthropic, Google, Cursor, DeepSeek |
| 🔧 Cursor Native | Zero API keys needed - Cursor handles execution |
🏆 Supported Models (2025)
Tier 1: Flagship Models (Complex Architecture & Refactoring)
| Model | Provider | Context | Cost (in/out) | Quality |
|---|---|---|---|---|
| GPT-5.2 | OpenAI | 256K | $5.00/$15.00 | 0.99/0.98 |
| Claude 4.5 Opus | Anthropic | 200K | $25.00/$75.00 | 0.99/0.99 |
| Claude 4.5 Sonnet | Anthropic | 200K | $5.00/$25.00 | 0.97/0.98 |
Tier 2: Reasoning Models (Chain of Thought)
| Model | Provider | Context | Cost (in/out) | Quality |
|---|---|---|---|---|
| o3 | OpenAI | 200K | $10.00/$40.00 | 0.99/0.95 |
| o3-mini (High) | OpenAI | 128K | $1.50/$6.00 | 0.95/0.92 |
| Claude 3.7 Sonnet | Anthropic | 200K | $4.00/$20.00 | 0.96/0.96 |
Tier 3: Native & Fast Models
| Model | Provider | Context | Cost (in/out) | Quality |
|---|---|---|---|---|
| Composer 1 | Cursor | 128K | $0.10/$0.30 | 0.88/0.92 |
| Gemini 3 Pro | 2M | $2.00/$8.00 | 0.96/0.94 | |
| Gemini 3 Flash | 1M | $0.10/$0.40 | 0.88/0.90 |
Tier 4: Budget/Legacy Models
| Model | Provider | Context | Quality |
|---|---|---|---|
| GPT-4o / GPT-4o-mini | OpenAI | 128K | 0.95/0.85 |
| Claude 3.5 Sonnet/Haiku | Anthropic | 200K | 0.96/0.88 |
| Gemini 2.0 Pro/Flash | 2M/1M | 0.94/0.85 | |
| DeepSeek V3 | DeepSeek | 128K | 0.92/0.94 |
| DeepSeek R1 | DeepSeek | 128K | 0.96/0.92 |
🚀 Quick Start
1. Install
git clone https://github.com/AI-Castle-Labs/mcp-router.git
cd mcp-router
pip install -r requirements.txt
pip install mcp # MCP SDK for Cursor integration
2. Configure Cursor
Add to ~/.cursor/mcp.json:
{
"version": "1.0",
"mcpServers": {
"mcp-router": {
"command": "python3",
"args": ["/path/to/mcp-router/src/mcp_server.py"],
"env": {}
}
}
}
Note: No API keys needed! Cursor handles all API calls with its own keys.
3. Restart Cursor
The MCP router will appear in your agent tools. Use it with:
@mcp-router get_model_recommendation "your task description"@mcp-router analyze_query "your query"@mcp-router list_models
💻 CLI Usage
# Route a query (shows which model would be selected)
python main.py route "Explain how neural networks work"
# Route with strategy
python main.py route "Refactor this codebase" --strategy quality
# List all registered models
python main.py list
# Show routing statistics
python main.py stats
Example Output
============================================================
Routing Decision
============================================================
Query: Refactor this complex authentication system...
Selected Model: Claude 4.5 Sonnet
Model ID: claude-4.5-sonnet
Provider: anthropic
Confidence: 88.45%
Reasoning: Model is optimized for code_edit tasks; Selected for highest quality
Alternatives:
- Composer 1 (composer-1)
- Claude 3.5 Haiku (claude-3-5-haiku-20241022)
- GPT-4o-mini (gpt-4o-mini)
🎯 Routing Strategies
| Strategy | Description | Best For |
|---|---|---|
balanced |
Optimizes for cost, speed, and quality equally | General use |
quality |
Prioritizes highest capability models | Complex tasks, refactoring |
speed |
Prioritizes fastest response time | Quick edits, simple tasks |
cost |
Prioritizes cheapest models | Budget-conscious usage |
🐍 Python API
from src.router import MCPRouter
# Initialize router (loads 17 default models)
router = MCPRouter()
# Route a query
decision = router.route(
"Analyze this codebase architecture",
strategy="quality"
)
print(f"Selected: {decision.selected_model.name}")
print(f"Model ID: {decision.selected_model.model_id}")
print(f"Confidence: {decision.confidence:.1%}")
print(f"Reasoning: {decision.reasoning}")
# Get alternatives
for alt in decision.alternatives[:3]:
print(f" Alternative: {alt.name}")
📁 Project Structure
mcp-router/
├── src/
│ ├── router.py # Core routing logic + 17 model definitions
│ ├── mcp_server.py # MCP server for Cursor integration
│ ├── client.py # API client for model execution
│ └── cursor_wrapper.py # Cursor-specific utilities
├── config/
│ └── cursor_mcp_config.json # Template for Cursor config
├── scripts/
│ └── setup_cursor.sh # Automated setup script
├── docs/
│ ├── cursor_integration.md
│ ├── QUICKSTART_CURSOR.md
│ └── AGENT_SETTINGS.md
├── main.py # CLI entry point
├── requirements.txt
└── README.md
🔧 Adding Custom Models
from src.router import MCPRouter, ModelCapabilities, TaskType
router = MCPRouter()
router.register_model(ModelCapabilities(
name="My Custom Model",
provider="custom",
model_id="custom-model-v1",
supports_reasoning=True,
supports_code=True,
supports_streaming=True,
max_tokens=8192,
context_window=32000,
cost_per_1k_tokens_input=1.0,
cost_per_1k_tokens_output=2.0,
avg_latency_ms=600,
reasoning_quality=0.85,
code_quality=0.90,
speed_score=0.80,
preferred_tasks=[TaskType.CODE_GENERATION],
api_key_env_var="CUSTOM_API_KEY"
))
🎮 Cursor Commands
Create .cursor/commands/route.md:
---
description: "Get model recommendation from MCP router for the current task"
---
Use the MCP router to determine the best model for the task at hand.
1. Analyze the current context
2. Call `@mcp-router get_model_recommendation` with task description
3. Present the recommendation with confidence and alternatives
4. Suggest switching models if needed
📊 MCP Tools Available
| Tool | Description |
|---|---|
route_query |
Route a query and get model recommendation (supports chat_history) |
get_model_recommendation |
Get recommendation without execution (supports chat_history) |
analyze_chat_summary |
Analyze chat history text to extract routing signals |
list_models |
List all 17 registered models |
get_routing_stats |
Get usage statistics |
analyze_query |
Analyze query characteristics |
Context-Aware Routing with Chat History
The router can now analyze chat history to make smarter routing decisions:
// Example: Using chat history for context-aware routing
{
"query": "Fix the authentication bug we discussed",
"strategy": "quality",
"chat_history": [
{
"role": "user",
"content": "I'm working on auth.py and users can't log in",
"timestamp": 1704067200
},
{
"role": "assistant",
"content": "Let me check the authentication flow...",
"timestamp": 1704067205
}
]
}
The router analyzes chat history to detect:
- Context depth: Shallow/medium/deep based on token count
- Dominant task type: Code generation, editing, debugging, etc.
- Programming languages: Detects Python, JavaScript, Rust, etc.
- Files mentioned: Tracks files being worked on
- Error patterns: Identifies debugging sessions
- Topics: Authentication, database, API, testing, etc.
- Complexity: Based on files, languages, and conversation depth
These signals influence model selection:
- Deep context → Models with larger context windows
- Debugging sessions → High-reasoning models
- Multi-file tasks → Code-focused models
- Multiple languages → Polyglot-capable models
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
📄 License
MIT License - see LICENSE for details.
<p align="center"> <b>Built for the Cursor IDE ecosystem</b><br> <a href="https://github.com/AI-Castle-Labs/mcp-router">AI Castle Labs</a> </p>
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。