什么是MCP

alt text

首先需要一个支持MCP的客户端,可以是vscode,cursor,claude app等等。

然后在本地部署好各种server,比如百度地图,browser use等等,主要是搞一个server.py,采用FastMCP格式,定义好tools,输入参数,函数内就是具体干啥,可以是一堆python代码直接把事干完了,诸如加减乘除操作然后return,也可以是发送一个请求等待接收结果然后return。

请求外部的话,一般比如百度地图,接收请求,返回内容。所以除了自己电脑上server.py,外部工具也需要一个xxx.py来响应请求。

MCP客户端和server之间,最简单就是一个json来关联,诸如cline_mcp_settings.json。写清楚server名,怎么启动。

所以MCP究竟在干啥呢?简单理解或许就是提供了FastMCP的一套标准格式,然后各个工具内部写个xxx.py处理请求。各个用户在本地配置server.py来发送请求。然后这么一套标准化的东西整完,大模型就可以知道有哪些东西可以调用(通过cline_mcp_settings.json)

MCP Server

本地需要server.py,然后部署(给大模型看的,都会作为prompt的一部分,让大模型知道有什么函数,怎么用)

看起来就是import FastMCP,然后写函数,参数形式,发请求。 alt text

MCP客户端和MCP server的桥梁

最最简单,就是个json:cline_mcp_settings.json

{
  "mcpServers": {
    "amap-amap-sse": {
      "autoApprove": [
        "maps_direction_bicycling",
        "maps_direction_driving",
        "maps_direction_transit_integrated",
        "maps_direction_walking",
        "maps_distance",
        "maps_geo",
        "maps_regeocode",
        "maps_ip_location",
        "maps_around_search",
        "maps_search_detail",
        "maps_text_search",
        "maps_weather"
      ],
      "timeout": 60,
      "url": "https://mcp.amap.com/sse?key=xxx",
      "transportType": "sse",
      "disabled": true
    },
    "zapier": {
      "disabled": true,
      "timeout": 60,
      "url": "https://actions.zapier.com/mcp/xxx",
      "transportType": "sse"
    },
    "local-weather": {
      "timeout": 60,
      "url": "http://localhost:8000/sse",
      "transportType": "sse"
    }
  }
}

Remote Service X

各个被调用的服务,接收server.py发送的请求,响应,然后返回。

具体例子感受下MCP过程的输入输出 只有一个MCP服务,百度地图。

q=“北京玉渊潭公园逛完还能去哪?请帮我安排一下午的行程”

操作步骤

安装软件

  • UV安装(可选)

    什么是UV,简单来说是一个与Pip一样的python虚拟环境管理,是开发ruff的公司 Astral 前一段时间发布的高性能Python工具,用途是安装python包,以及解析包版本之间的依赖。它的最大特点是快,相比现有的的工具都能够快一大截

  # 直接下载安装脚本,支持 macOS和Linux.
  curl -LsSf https://astral.sh/uv/install.sh | sh

  # 使用pip安装
  pip install uv
  • 创建项目

    uv init mcp-server-demo
    cd mcp-server-demo
    uv add "mcp[cli]"
    
  • Server Code

    注意: 需要注意一下 FastMCP的配置,特别是sse_path 这个配置,需要与 cline_mcp_setting.json 中的 url 参数对上,要不然调用不成功


from mcp.server.fastmcp import FastMCP

mcp = FastMCP(
    name="MyServer",
    host="0.0.0.0",  # 允许远程访问
    port=8000,       # 自定义端口
    sse_path="/mcp"  # SSE端点路径
)


@mcp.resource("echo://{message}")
def echo_resource(message: str) -> str:
    """Echo a message as a resource"""
    return f"Resource echo: {message}"


@mcp.tool()
def echo_tool(message: str) -> str:
    """Echo a message as a tool"""
    return f"Tool echo: {message}"


@mcp.prompt()
def echo_prompt(message: str) -> str:
    """Create an echo prompt"""
    return f"Please process this message: {message}"
  • 启动mcp服务

    注意:这里需要加 -t sse 启用 transport,这个参数的值默认是 None,直接启动会没有反映,与之对应的是 cline_mcp_setting.json文件中的 transportType: sse 这个配置。

    mcp run  main.py -t sse
    
  • cline mcp配置 alt text

  • 运行效果 alt text

Claude For Deskstop配置MCP

添加文件系统 MCP 服务器 为了添加文件系统功能,我们将为 Claude for Desktop 安装一个预构建的 Filesystem MCP Server。这是由 Anthropic 和社区创建的数十个服务器之一。

开始操作:

打开 Claude 菜单,选择“设置(Settings)”。 请注意,这里的设置不是应用窗口中的“Claude 账户设置(Claude Account Settings)”。 在 Mac 上,界面如下所示: alt text

点击左侧栏中的“开发者(Developer)”,然后点击“编辑配置(Edit Config)”:

alt text

此操作会在以下位置创建一个配置文件:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json 如果文件不存在,将会自动创建,并显示在您的文件系统中

其它注意事项

Claude Desktop 不支持remote mcp server,可以使用以下方式,注意这里貌似不能使用http,使用http协议时一直报:

Usage: npx tsx proxy.ts <https://server-url> [callback-port]

试了半天,把http协议改成https协议就好了。

{
  "mcpServers": {
    "remote-example": {
      "command": "npx",
      "args": [
        "mcp-remote",
        "https://remote.mcp.server/sse"
      ]
    }
  }
}

相关文档

mcp python sdk 说明 mcp 指南 一文看懂:MCP(大模型上下文协议)