LLM路由工具

一个智能分发AI工作负载的工具,自动根据任务类型选择最优的LLM模型,以最小化token成本。

  • by Devatva24
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 Devatva24/LLM-Router-MCP

使用说明

LLM路由器MCP

路由在Claude、Gemini和GPT-4o之间智能提示——自动为每个任务选择最佳模型,同时最大限度地降低令牌成本。


✨ 这是什么?

llm-router-mcp 是一个 模型上下文协议(MCP)服务器 它充当您的AI工作负载的智能调度器。路由器不会将单个LLM硬编码到您的工作流程中,而是分析每个提示的意图,并自动为该特定任务类型选择最具成本效益和能力的模型。

Your prompt ──► LLM Router ──► PLANNING      → Gemini
                            ├── SCAFFOLDING  → Gemini
                            ├── CODEGEN      → Claude
                            ├── REFACTOR     → Claude
                            ├── REVIEW       → Claude
                            ├── TESTING      → GPT-4o
                            └── IMPLEMENT    → GPT-4o

🚀 特性

  • 基于智能意图的路由 --将提示分为8个任务类别,并分派到最优模型
  • 三种前沿模型 --集成了Claude(Anthropic)、Gemini(谷歌)和GPT-4o(OpenAI)
  • 零配置模拟模式 -在没有API密钥的情况下开箱即用;当按键缺失时,自动启用模拟模式
  • 会话感知上下文缓存 --在同一会话中跨回合维护对话上下文
  • 明确的工具快捷方式 --使用专用旁路自动路由 plan_workflow, generate_code,以及 implement_feature 工具
  • MCP本地 --将其放入任何兼容MCP的主机(Claude Desktop、Cursor、VS Code Continue等)

📦 快速开始

选项A--npx(无需安装)

npx llm-router-mcp

选项B——全局安装

npm install -g llm-router-mcp
llm-router-mcp

选项C——来源

git clone https://github.com/Devatva24/LLM-Router-MCP.git
cd LLM-Router-MCP
npm install
npm run build
node dist/index.js

没有API密钥? 没问题服务器会自动回退到模拟模式并记录一条有用的消息。只有当你想要真实的模型响应时,你才需要密钥。


🗺️ 路由逻辑

任务类别 触发关键字 路由到
规划 架构、设计、系统设计、策略、工作流程 ✦ 双子座
脚手架 脚手架、样板、设置、文件夹结构 ✦ 双子座
代码生成 编写函数、实现、创建类、算法 ✦ 克劳德
重构 重构、清理、改进、重写、重组 ✦ 克劳德
代码审查 审查、调试、解释、问题所在、批评 ✦ 克劳德
测试 单元测试、Jest、Vitest、测试套件、测试用例 ✦ GPT-4o
实施 实现、添加端点、构建、创建API ✦ GPT-4o
通用 其他内容 ✦ 克劳德(后备)

⚙️ 配置

将API键设置为环境变量以使用真实模型:

# Mac / Linux
export ANTHROPIC_API_KEY=sk-ant-...
export GOOGLE_API_KEY=AIza...
export OPENAI_API_KEY=sk-...

# Windows (PowerShell)
$env:ANTHROPIC_API_KEY="sk-ant-..."
$env:GOOGLE_API_KEY="AIza..."
$env:OPENAI_API_KEY="sk-..."

如果缺少任何密钥,服务器将自动启用模拟模式——无需崩溃,也不需要配置。


🖥️ 编辑器集成

克劳德桌面版

编辑 ~/Library/Application Support/Claude/claude_desktop_config.json (Mac)或 %APPDATA%\Claude\claude_desktop_config.json (Windows):

{
  "mcpServers": {
    "llm-router": {
      "command": "npx",
      "args": ["llm-router-mcp"]
    }
  }
}

光标

添加到光标MCP设置中:

{
  "mcpServers": {
    "llm-router": {
      "command": "npx",
      "args": ["llm-router-mcp"]
    }
  }
}

VS代码(继续扩展)

添加 ~/.continue/config.json:

{
  "mcpServers": {
    "llm-router": {
      "command": "npx",
      "args": ["llm-router-mcp"]
    }
  }
}

Cursor和Continue的现成配置文件包含在 cursor-config/ 和 %USERPROFILE%/.continue/ 此仓库的目录。


🧰 可用的MCP工具

route_prompt

自动将提示分类并路由到最佳模型。

{
  "prompt": "Write a recursive function to flatten deeply nested objects",
  "session_id": "my-session"
}

plan_workflow

明确路线 双子座 用于高级规划和架构任务。

{
  "prompt": "Design a checkout flow for an e-commerce app",
  "session_id": "my-session"
}

generate_code

明确路线 克劳德 用于复杂的逻辑、算法和重构。

{
  "prompt": "Write a binary search tree with insert and delete",
  "session_id": "my-session"
}

implement_feature

明确路线 GPT-4o 用于功能实现和测试生成。

{
  "prompt": "Implement the /api/products CRUD endpoints",
  "session_id": "my-session"
}

clear_context

清除给定会话的缓存会话上下文。

{
  "session_id": "my-session"
}

🧪 运行测试

测试套件以模拟模式验证所有路由决策-不需要API密钥:

npm test

预期产量:

🧪 LLM Router — mock test suite

  ✅ planning → Gemini
  ✅ codegen → Claude
  ✅ testing → GPT-4o
  ✅ review → Claude
  ✅ explicit plan_workflow
  ✅ explicit implement_feature
  ✅ context cache — 2nd turn
  ✅ clear_context

──────────────────────────────────────────
  8 passed  0 failed (8 total)

📁 项目结构

LLM-Router-MCP/
├── src/
│   ├── index.ts          # MCP server entrypoint & routing logic
│   ├── classifier.ts     # Prompt intent classifier
│   ├── context-cache.ts  # Session-aware context management
│   ├── mock.ts           # Mock responses for zero-cost testing
│   └── logger.ts         # Lightweight logger
├── dist/                 # Compiled JavaScript (after npm run build)
├── cursor-config/        # Ready-made Cursor MCP config
├── test-router.cjs       # End-to-end test suite (mock mode)
├── tsconfig.json
└── package.json

🤝 贡献

欢迎投稿!请随时打开问题或提交拉取请求:

  • 添加对其他LLM提供商的支持
  • 提高路由分类的准确性
  • 添加流媒体响应支持
  • 编写更多测试覆盖率

📄 许可证

麻省理工学院——见 许可证 了解详情。