simple-mcp

rnben/simple-mcp

3.2

If you are the rightful owner of simple-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

This project is a demonstration of a Model Context Protocol (MCP) server using FastMCP, showcasing a simple server setup and integration with Cline.

Tools
2
Resources
0
Prompts
0

MCP Demo 项目

这是一个基于 FastMCP 的 Model Context Protocol (MCP) 演示项目,展示了如何创建一个简单的 MCP 服务器并与 Cline 集成。

项目概述

本项目实现了一个简单的计算器 MCP 服务器,提供以下功能:

  • 数学计算工具(加法运算)
  • 天气查询工具
  • 调试中间件,用于监控 MCP 消息流

文件结构

mcp-demo/
├── demo_server.py          # MCP 服务器主文件
├── README.md              # 项目文档
├── requirements.txt       # 项目依赖
├── .gitignore             # Git 忽略文件配置
└── .venv/                 # Python 虚拟环境(不提交到 Git)

快速开始

1. 环境准备

确保已安装 Python 3.8+,然后创建虚拟环境:

# 创建虚拟环境
python -m venv .venv

# 激活虚拟环境
# macOS/Linux:
source .venv/bin/activate
# Windows:
.venv\Scripts\activate

2. 安装依赖

pip install -r requirements.txt

3. 启动服务器

python demo_server.py

服务器将在 http://localhost:8000 启动,使用 SSE (Server-Sent Events) 协议。

Cline 配置说明

配置文件位置

Cline 的 MCP 配置文件位于:

  • macOS: ~/Library/Application Support/Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json
  • Windows: %APPDATA%\Code\User\globalStorage\saoudrizwan.claude-dev\settings\cline_mcp_settings.json
  • Linux: ~/.config/Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json

配置示例

将以下配置添加到 cline_mcp_settings.json 文件中:

{
  "mcpServers": {
    "my_debug_server_network": {
      "autoApprove": [
        "queryWeather"
      ],
      "disabled": false,
      "timeout": 60,
      "type": "sse",
      "url": "http://localhost:8000/sse"
    }
  }
}

配置参数说明

参数类型说明
autoApproveArray自动批准的工具列表,无需手动确认
disabledBoolean是否禁用此服务器
timeoutNumber请求超时时间(秒)
typeString连接类型,这里是 "sse"
urlStringMCP 服务器的 SSE 端点地址

重启 Cline

配置完成后,需要重启 VS Code 或重新加载 Cline 窗口以使配置生效。

使用方法

  1. 确保 MCP 服务器正在运行
  2. 在 Cline 中可以直接使用注册的工具:
    • add: 加法计算
    • queryWeather: 天气查询

示例对话:

用户:帮我计算 5 + 3
Cline:[调用 add 工具] 5 + 3 = 8

用户:查询北京的天气
Cline:[调用 queryWeather 工具] 北京的天气是:晴天☀️

代码结构说明

DebugMiddleware 类

实现了调试中间件,用于监控所有 MCP 消息:

  • 记录接收到的请求和方法
  • 打印完整的请求参数
  • 显示服务器返回的响应

FastMCP 服务器

  • 使用装饰器 @mcp.tool() 注册工具
  • 支持类型提示和文档字符串
  • 通过 SSE 协议提供 HTTP 接口

开发指南

添加新工具

  1. 使用 @mcp.tool() 装饰器
  2. 添加类型提示
  3. 编写文档字符串
@mcp.tool()
def multiply(a: int, b: int) -> int:
    """将两个整数相乘并返回结果。"""
    return a * b

自定义中间件

class CustomMiddleware(Middleware):
    async def on_message(self, context: MiddlewareContext, call_next):
        # 前置处理
        print(f"处理请求: {context.method}")
        
        # 调用下一个中间件或处理器
        result = await call_next(context)
        
        # 后置处理
        print(f"请求完成: {result}")
        return result

故障排除

常见问题

  1. 连接失败

    • 确保服务器正在运行
    • 检查端口 8000 是否被占用
    • 验证 URL 配置是否正确
  2. 工具不可用

    • 重启 Cline 以重新加载配置
    • 检查 disabled 是否为 false
    • 确认工具名称拼写正确
  3. 权限问题

    • 检查 autoApprove 列表是否包含所需工具
    • 手动批准工具使用请求

调试方法

  1. 查看服务器控制台输出的调试信息
  2. 检查 Cline 的 MCP 连接状态
  3. 使用浏览器访问 http://localhost:8000/sse 测试 SSE 端点

参考资源

许可证

本项目仅供学习和演示使用。