LLM模型协议服务器

一个提供多种LLM API访问的MCP服务器,用于Claude Code的工具集成,支持多种模型路由和动态工具生成。

  • by mbosley
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 mbosley/llm-mcp-server

使用说明

LLM MCP服务器

MCP(模型上下文协议)服务器,提供对各种LLM API的访问,作为克劳德代码的工具。

特性

  • 化学 K2 -最先进的1T参数MoE模型,具有128k上下文窗口
  • 双子座2.5 Pro -处理大量上下文窗口以进行全面分析
  • 双子座2.5闪光灯 -快速、功能强大的平衡任务模型
  • GPT-4.1-nano -超快速、轻量级的完成简单任务
  • GPT-4.1分钟 -性能良好的替代平衡模型
  • 自定义模型布线 -为每个任务选择合适的模型

完整安装指南

第一步:克隆或下载

cd ~/projects  # or your preferred directory
git clone [repository-url] llm-mcp-server
cd llm-mcp-server

步骤2:运行安装脚本

chmod +x setup.sh
./setup.sh

这将:

  • 创建Python虚拟环境
  • 安装所有依赖项
  • 创建一个 .env 文件来自 .env.example 模板
  • 使服务器可执行

步骤3:添加您的API密钥

编辑 .env 文件:

nano .env  # or use your preferred editor

至少添加一个API密钥(您不需要所有密钥):

# Anthropic API for Claude models
ANTHROPIC_API_KEY=your_anthropic_api_key_here

# OpenAI API for GPT models  
OPENAI_API_KEY=your_openai_api_key_here

# Google API for Gemini models
GOOGLE_API_KEY=your_google_api_key_here

# Moonshot API for Kimi K2 models
MOONSHOT_API_KEY=your_moonshot_api_key_here

# Optional: Log costs to file (JSON lines format)
# LLM_COST_LOG=/path/to/llm_costs.jsonl

步骤4:配置Claude代码

在Claude Code中,使用以下命令添加MCP服务器:

claude mcp add --scope user llm-tools python /full/path/to/llm-mcp-server/server.py

例如:

claude mcp add --scope user llm-tools python /Users/yourname/projects/llm-mcp-server/server.py

步骤5:验证安装

检查是否已配置:

/mcp

您应该看到列出了“llm工具”。

步骤6:重新启动Claude代码

  • 完全退出克劳德代码(Mac上的Cmd+Q,Windows上的Alt+F4)
  • 重新打开克劳德代码

步骤7:测试工具

在任何克劳德对话中,工具现在都可用。通过提问进行测试:

  • “使用analyze_with_gemini检查工具是否正常工作”
  • “使用check_costs查看成本跟踪”

可用工具

与戈米尼一起分析

使用Gemini 2.5 Pro的海量上下文窗口分析大型代码库或文档。

Parameters:
- prompt: The analysis query
- files: List of file paths to include in context (optional)
- context: Direct context string (optional)

quick_gpt

使用GPT-4.1-nano快速响应简单任务。

Parameters:
- prompt: The task or question
- temperature: Control randomness (0-1, default 0.3)

平衡llm

使用Gemini 2.5 Flash或GPT-4.1-mini进行平衡任务。

Parameters:
- prompt: The task or question
- model: "gemini-flash" or "gpt-mini" (default: "gemini-flash")
- max_tokens: Maximum response length (default 1000)

路由到测试模型

根据任务自动选择最佳模型。

Parameters:
- prompt: The task description
- task_type: "analysis", "generation", "simple", or "auto"

Kimi聊天

灵活的Kimi/Moonshot聊天界面,支持简单提示、多回合对话和部分预填充。

Parameters (all optional):
- prompt: Simple prompt for single-turn, or latest user message
- messages: Full conversation history array (overrides prompt if provided)
  - Each message: {"role": "system/user/assistant", "content": "...", "name": "optional", "partial": true/false}
- system: System message (used only if messages not provided)
- partial_response: Pre-fill the assistant's response to maintain character/format
- model: Model name - Common options:
  - 'kimi-k2-0711-preview' (default) - Kimi K2 instruct model for chat, tool use, structured responses
  - 'kimi-k2-base' - Base model for creative completions (when available via API)
  - 'moonshot-v1-auto' - Automatically selects appropriate context size
  - 'moonshot-v1-128k' - 128k context window
  - Note: Currently only instruct models are available via API
- temperature: Control randomness (0-1, default 0.6)
- max_tokens: Maximum response length (default 4096)
- available_tools: List of built-in tool names to enable (optional)
  - Built-in tools: ["get_current_time", "add_numbers", "list_files"]
- dynamic_tools: Array of custom tool definitions for on-the-fly CLI execution (optional)
  - Each tool needs: name, command, and schema properties
- session_id: Persistent conversation management (optional)
  - New sessions: Use descriptive keywords like "python-async-debugging" (auto-timestamped)
  - Continue existing: Use full ID like "python-async-debugging_20250712_2055"
  - Special commands: "@last" (continue most recent), "@list" (show all sessions), "@clear:id" (delete session)
- return_conversation: Return full conversation history with response (default: false)

Usage modes:
1. Simple: kimi_chat(prompt="Hello")
2. With system: kimi_chat(system="You are a pirate", prompt="Tell me about treasure")
3. Multi-turn: kimi_chat(messages=[...])
4. With pre-filling: kimi_chat(prompt="...", partial_response="*thinks carefully* ")
5. Full conversation + pre-fill: kimi_chat(messages=[...], partial_response="...")
6. With built-in tools: kimi_chat(prompt="What time is it?", available_tools=["get_current_time"])
7. With dynamic tools: kimi_chat(prompt="Check port 8080", dynamic_tools=[{...}])
8. Start new session: kimi_chat(prompt="Let's discuss Python", session_id="python-basics")
9. Continue session: kimi_chat(prompt="Tell me more", session_id="python-basics_20250712_2055")
10. Continue last session: kimi_chat(prompt="What were we discussing?", session_id="@last")
11. List sessions: kimi_chat(session_id="@list")
12. Get conversation state: kimi_chat(prompt="Hello", return_conversation=true)

会话管理

Kimi对话可以持久到磁盘上以实现长期连续性:

会话存储:

  • 会话存储在 .kimi_sessions/ 目录(gitignored)
  • 每个会话都是一个包含消息、元数据和时间戳的JSON文件
  • 会话ID使用格式: keywords_YYYYMMDD_HHMM

会话工作流示例:

# Start a new project discussion
kimi_chat(
    prompt="I need help designing a REST API",
    session_id="api-design"  # Creates: api-design_20250712_2100.json
)

# Continue later (use full ID)
kimi_chat(
    prompt="What about authentication?",
    session_id="api-design_20250712_2100"
)

# Or continue the most recent session
kimi_chat(
    prompt="Should we use JWT or sessions?",
    session_id="@last"
)

# Check all active sessions
kimi_chat(session_id="@list")

会话功能:

  • 接近上下文限制时自动截断消息
  • 从会话ID中提取主题以便于识别
  • 创建和上次访问的时间戳
  • 会话元数据保存

动态工具生成

这 kimi_chat 该工具支持使用 dynamic_tools 参数:

kimi_chat(
    prompt="What's my public IP address?",
    dynamic_tools=[{
        "name": "get_public_ip",
        "command": "curl -s ifconfig.me",
        "schema": {
            "type": "function",
            "function": {
                "name": "get_public_ip",
                "description": "Get public IP address",
                "parameters": {
                    "type": "object",
                    "properties": {},
                    "required": []
                }
            }
        }
    }]
)

动态工具示例:

# Network operations
{
    "name": "check_port",
    "command": "netstat -an | grep :8080",
    "schema": {...}
}

# File operations  
{
    "name": "find_large_files",
    "command": "find . -size +100M -type f -exec ls -lh {} \\;",
    "schema": {...}
}

# System information
{
    "name": "memory_usage", 
    "command": "free -h",
    "schema": {...}
}

# Git operations
{
    "name": "recent_commits",
    "command": "git log --oneline -5", 
    "schema": {...}
}

工具执行功能:

  • 30秒安全超时
  • Stout/stderr捕获
  • 错误处理和报告
  • 适用于任何shell命令
  • 与内置工具无缝结合

完整的动态工具示例:

# Request: "Check if port 8080 is open and count Python files"
kimi_chat(
    prompt="Check if port 8080 is open and count Python files in this directory",
    dynamic_tools=[
        {
            "name": "check_port_8080",
            "command": "netstat -an | grep :8080",
            "schema": {
                "type": "function",
                "function": {
                    "name": "check_port_8080",
                    "description": "Check if port 8080 is listening",
                    "parameters": {"type": "object", "properties": {}, "required": []}
                }
            }
        },
        {
            "name": "count_python_files", 
            "command": "find . -name '*.py' -type f | wc -l",
            "schema": {
                "type": "function",
                "function": {
                    "name": "count_python_files",
                    "description": "Count Python files in directory",
                    "parameters": {"type": "object", "properties": {}, "required": []}
                }
            }
        }
    ]
)

# Result: 
# "Port 8080 is not currently listening, and there are 3,234 Python files in this directory.
# 
# **Tool Executions:**
# 🔧 check_port_8080({}) → (no output - port not listening)
# 🔧 count_python_files({}) → 3234"

## Kimi K2 Tool Calling Capabilities

Kimi K2 has native tool-calling capabilities that allow it to autonomously execute functions and orchestrate workflows. The model can:

- **Autonomous Task Decomposition**: Break down complex requests into tool sequences
- **Multiple Tool Calls**: Execute several functions in a single response
- **Parameter Parsing**: Correctly extract and format function arguments
- **Context Preservation**: Maintain conversation state throughout tool interactions

### Tool Calling API Flow

The tool calling process follows OpenAI's tools format:

#### 1. Tool Definition
Tools are defined using JSON schemas:
```json
{
  "type": "function",
  "function": {
    "name": "get_current_time",
    "description": "Get the current date and time",
    "parameters": {
      "type": "object",
      "properties": {},
      "required": []
    }
  }
}

2.工具调用请求

当Kimi想要使用工具时,它会回应:

{
  "role": "assistant",
  "content": "I'll get the current time for you.",
  "tool_calls": [
    {
      "index": 0,
      "id": "get_current_time:0",
      "type": "function",
      "function": {
        "name": "get_current_time",
        "arguments": "{}"
      }
    }
  ]
}

3.工具执行

您的应用程序执行函数并返回结果:

{
  "role": "tool",
  "tool_call_id": "get_current_time:0",
  "content": "2025-07-12 14:30:45"
}

4.最终答复

Kimi将工具结果合成为自然语言:

{
  "role": "assistant",
  "content": "It is currently 2:30:45 PM on July 12, 2025."
}

示例:多个工具调用

请求:

"What time is it, and then add 10 plus 5?"

Kimi的回应:

{
  "tool_calls": [
    {
      "id": "get_current_time:0",
      "function": {"name": "get_current_time", "arguments": "{}"}
    },
    {
      "id": "add_numbers:1", 
      "function": {"name": "add_numbers", "arguments": "{\"a\": 10, \"b\": 5}"}
    }
  ]
}

工具结果:

[
  {"role": "tool", "tool_call_id": "get_current_time:0", "content": "2025-07-12 14:45:30"},
  {"role": "tool", "tool_call_id": "add_numbers:1", "content": "15"}
]

最终回复:

"It's currently **2:45:30 PM** on July 12, 2025.
And **10 + 5 = 15**."

关键实施说明

  • 工具调用ID:每个工具调用都有一个唯一的ID(例如。, "function_name:index")
  • 参数格式:函数参数是JSON字符串,即使是简单的参数也是如此
  • 多种工具:Kimi可以在一个响应中调用多个工具来处理复杂的请求
  • 错误处理:工具执行错误应作为工具消息返回,供Kimi处理
  • 流媒体:工具调用可用于流式和非流式API调用

check_costs

检查此会话中所有LLM使用的累积成本。

Parameters: none
Returns: Detailed cost breakdown by model

Prompts中的文件插值

所有工具都支持使用简单语法进行自动文件插值:

单个文件

# Include entire file
analyze_with_gemini(
    prompt="Review this code: {file:server.py}"
)

# Include specific line range
quick_gpt(
    prompt="Explain this function: {file:utils/helper.py:10-25}"
)

多个文件

# Use glob patterns
analyze_with_gemini(
    prompt="Review all test files: {files:tests/*.py}"
)

# Mix multiple files
kimi_chat(
    prompt="Compare {file:old_version.py} with {file:new_version.py}"
)

复杂示例

# Combine with regular text
analyze_with_gemini(
    prompt="""Based on the README: {file:README.md}
    
    Please explain how this implementation works:
    {file:core/engine.py:50-150}
    
    And how it relates to these tests:
    {files:tests/test_engine*.py}"""
)

文件内容会自动读取并插入到您的提示中,使引用代码变得容易,而无需手动复制粘贴。

使用示例

在Claude Code中,这些工具可供使用:

# Analyze entire codebase
result = analyze_with_gemini(
    prompt="Find all API endpoints and their authentication methods",
    files=["src/**/*.py"]
)

# Quick formatting
formatted = quick_gpt(
    prompt="Format this JSON: {a:1,b:2}"
)

# Smart routing
response = route_to_best_model(
    prompt="Explain the authentication flow",
    task_type="analysis"
)

# Kimi/Moonshot examples
# Simple query
answer = kimi_chat(prompt="What is quantum computing?")

# Role-playing with system message
story = kimi_chat(
    system="You are a wise wizard who speaks in riddles",
    prompt="Tell me about dragons"
)

# Multi-turn conversation
chat = kimi_chat(
    messages=[
        {"role": "system", "content": "You are a helpful coding assistant"},
        {"role": "user", "content": "How do I sort a list in Python?"},
        {"role": "assistant", "content": "You can use the sorted() function or list.sort() method."},
        {"role": "user", "content": "What's the difference?"}
    ]
)

# With partial pre-filling for character consistency
response = kimi_chat(
    system="You are Dr. Kelsier from Arknights",
    prompt="What do you think about the current situation?",
    partial_response="*adjusts her monocle with a slight frown* The situation, you ask? "
)

# Using different Moonshot models
auto_response = kimi_chat(
    prompt="Explain quantum computing",
    model="moonshot-v1-auto"  # Automatically selects context size
)

成本跟踪

每个请求都会自动跟踪:

  • 令牌使用情况(估计)
  • 每次请求的成本
  • 每种型号的累计成本
  • 所有型号的总成本

每个回复都包括成本信息:

[Response content]

---
💰 Cost: $0.000125 | Total: $0.0045

使用 check_costs() 工具随时查看详细明细。

故障排除

工具没有出现?

  1. 确保您在中使用了完整的绝对路径 claude mcp add 命令
  2. 检查 /mcp 将服务器显示为“正在运行”
  3. 确保您完全重新启动了Claude Code
  4. 验证中是否至少设置了一个API密钥 .env

正在获取API错误?

  • 检查您的API密钥是否有效
  • 确保您的帐户上有信用/配额
  • 谷歌API关键需求Gemini API在谷歌云控制台中启用
  • OpenAI密钥需要适当的模型访问
  • Anthropic密钥需要来自Anthropics控制台

Python错误?

  • 确保已安装Python 3.8+
  • 尝试使用虚拟环境: source venv/bin/activate
  • 检查已安装的所有依赖项: pip list

服务器未启动?

  • 检查server.py是否具有执行权限: chmod +x server.py
  • 验证MCP配置中的路径是否正确
  • 检查克劳德代码日志中的错误消息

Slash命令

您可以使用以下工具构建斜线命令:

# .claude/commands/analyze-all.md
Analyze entire codebase with Gemini

Use Gemini's 1M context to analyze all files:
- Security vulnerabilities
- Architecture patterns
- Code quality issues