OpenRouter MCP 服务器
模型上下文协议 (MCP) 服务器,可与 OpenRouter.ai 丰富的模型生态系统无缝集成。通过统一、类型安全的接口访问各种 AI 模型,该接口内置缓存、速率限制和错误处理功能。
特征
- 模型访问
- 直接访问所有 OpenRouter.ai 模型
- 自动模型验证和能力检查
- 默认模型配置支持
- 性能优化
- 智能模型信息缓存(1小时有效)
- 自动速率限制管理
- 失败请求的指数退避
- 统一响应格式
- 所有响应的
ToolResult
结构一致 - 使用
isError
标志清除错误标识 - 带有上下文的结构化错误消息
安装
pnpm install @mcpservers/openrouterai
配置
先决条件
- 从OpenRouter Keys获取您的 OpenRouter API 密钥
- 选择默认模型(可选)
环境变量
OPENROUTER_API_KEY=your-api-key-here
OPENROUTER_DEFAULT_MODEL=optional-default-model
设置
添加到您的 MCP 设置配置文件( cline_mcp_settings.json
或claude_desktop_config.json
):
{
"mcpServers": {
"openrouterai": {
"command": "npx",
"args": ["@mcpservers/openrouterai"],
"env": {
"OPENROUTER_API_KEY": "your-api-key-here",
"OPENROUTER_DEFAULT_MODEL": "optional-default-model"
}
}
}
}
响应格式
所有工具都以标准化结构返回响应:
interface ToolResult {
isError: boolean;
content: Array<{
type: "text";
text: string; // JSON string or error message
}>;
}
成功案例:
{
"isError": false,
"content": [{
"type": "text",
"text": "{\"id\": \"gen-123\", ...}"
}]
}
错误示例:
{
"isError": true,
"content": [{
"type": "text",
"text": "Error: Model validation failed - 'invalid-model' not found"
}]
}
可用工具
聊天完成
向 OpenRouter.ai 模型发送消息:
interface ChatCompletionRequest {
model?: string;
messages: Array<{role: "user"|"system"|"assistant", content: string}>;
temperature?: number; // 0-2
}
// Response: ToolResult with chat completion data or error
搜索模型
搜索并过滤可用型号:
interface ModelSearchRequest {
query?: string;
provider?: string;
minContextLength?: number;
capabilities?: {
functions?: boolean;
vision?: boolean;
};
}
// Response: ToolResult with model list or error
获取模型信息
获取特定型号的详细信息:
{
model: string; // Model identifier
}
验证模型
检查模型 ID 是否有效:
interface ModelValidationRequest {
model: string;
}
// Response:
// Success: { isError: false, valid: true }
// Error: { isError: true, error: "Model not found" }
错误处理
服务器提供带有上下文信息的结构化错误:
// Error response structure
{
isError: true,
content: [{
type: "text",
text: "Error: [Category] - Detailed message"
}]
}
常见错误类别:
Validation Error
:输入参数无效API Error
:OpenRouter API 通信问题Rate Limit
:请求节流检测Internal Error
:服务器端处理失败
处理响应:
async function handleResponse(result: ToolResult) {
if (result.isError) {
const errorMessage = result.content[0].text;
if (errorMessage.startsWith('Error: Rate Limit')) {
// Handle rate limiting
}
// Other error handling
} else {
const data = JSON.parse(result.content[0].text);
// Process successful response
}
}
发展
有关以下方面的详细信息,请参阅CONTRIBUTING.md :
# Install dependencies
pnpm install
# Build project
pnpm run build
# Run tests
pnpm test
变更日志
查看CHANGELOG.md了解最新更新,包括:
- 统一响应格式实现
- 增强的错误处理系统
- 类型安全接口改进
执照
该项目根据 Apache License 2.0 获得许可 - 有关详细信息,请参阅LICENSE文件。