模型上下文协议服务器

FastMCP是一个模型上下文协议(MCP)服务器,通过MCP标准提供LLM服务,作为MCP客户端与本地LLM服务之间的桥梁,实现与MCP兼容应用程序的无缝集成。

  • by Dev00355
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 dev00355/custom-mcp

使用说明

FastMCP-模型上下文协议服务器

FastMCP是一个模型上下文协议(MCP)服务器,通过MCP标准提供LLM服务。它充当MCP客户端和本地LLM服务之间的桥梁,实现与MCP兼容应用程序的无缝集成。

特性

  • 🚀 MCP协议合规性:全面实施模型上下文协议
  • 🔧 工具:聊天完成、模特列表、健康检查
  • 📝 提示:常见任务的预构建提示(助手、代码审查、总结)
  • 📊 资源:服务器配置和LLM服务状态
  • 🔄 流媒体支持:流媒体和非流媒体响应
  • 🔒 可配置的:基于环境的配置
  • 🛡️ 健壮:内置错误处理和健康监控
  • 🔌 集成就绪:适用于任何与OpenAI兼容的LLM服务

入门指南

先决条件

  • Python 3.9+
  • 点
  • 在端口5001上运行的本地LLM服务(与OpenAI兼容的API)
  • MCP客户端(例如,克劳德桌面、MCP检查器)

安装

  1. 克隆存储库:

    git clone https://github.com/yourusername/fastmcp.git
    cd fastmcp
    
  2. 创建虚拟环境并激活它:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
  3. 安装依赖项:

    pip install -r requirements.txt
    
  4. 创建一个 .env 文件(复制自 .env.mcp)并配置:

    # Server Settings
    MCP_SERVER_NAME=fastmcp-llm-router
    MCP_SERVER_VERSION=0.1.0
    
    # LLM Service Configuration
    LOCAL_LLM_SERVICE_URL=http://localhost:5001
    
    # Optional: API Key for LLM service
    # LLM_SERVICE_API_KEY=your_api_key_here
    
    # Timeouts (in seconds)
    LLM_REQUEST_TIMEOUT=60
    HEALTH_CHECK_TIMEOUT=10
    
    # Logging
    LOG_LEVEL=INFO
    

运行MCP服务器

选项1:使用CLI脚本

python run_server.py

选项2:直接执行

python mcp_server.py

选项3:使用自定义配置

python run_server.py --llm-url http://localhost:5001 --log-level DEBUG

MCP服务器将在stdio上运行,可以通过MCP客户端连接。

MCP客户端集成

Claude桌面集成

添加到您的Claude Desktop配置中:

{
  "mcpServers": {
    "fastmcp-llm-router": {
      "command": "python",
      "args": ["/path/to/fastmcp/mcp_server.py"],
      "env": {
        "LOCAL_LLM_SERVICE_URL": "http://localhost:5001"
      }
    }
  }
}

MCP检查员

使用MCP Inspector测试您的服务器:

npx @modelcontextprotocol/inspector python mcp_server.py

可用工具

1.聊天完成

向您的LLM服务发送消息:

{
  "name": "chat_completion",
  "arguments": {
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello!"}
    ],
    "model": "default",
    "temperature": 0.7
  }
}

2.列出型号

从LLM服务中获取可用型号:

{
  "name": "list_models",
  "arguments": {}
}

3.健康检查

检查LLM服务是否正在运行:

{
  "name": "health_check",
  "arguments": {}
}

可用提示

  • 聊天助理:通用AI助手提示
  • 代码查看:代码审查和分析
  • 总结:文本摘要

可用资源

  • config://server:服务器配置
  • status://llm-service:LLM服务状态

项目结构

fastmcp/
├── app/
│   ├── api/
│   │   └── v1/
│   │       └── api.py          # API routes
│   ├── core/
│   │   └── config.py          # Application configuration
│   ├── models/                # Database models
│   ├── services/              # Business logic
│   └── utils/                 # Utility functions
├── tests/                     # Test files
├── .env.example               # Example environment variables
├── requirements.txt           # Project dependencies
└── README.md                  # This file

贡献

  1. 分叉存储库
  2. 创建功能分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add some amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。