通过实例学习 MCP Server 开发

MCP 是一个连接了大语言模型与工具的协议。本文通过一个简单的实例项目来讲解一下如何开发一个自己的 MCP Server 。

项目背景

Quip 云笔记将于2027年3月31号停止服务,因此我本人使用了近10年的笔记数据需要导出并迁移到其他笔记工具。
因为 Quip 有提供 api 的功能,最简单的一个办法就是直接通过一个脚本来导出数据。
然而最近 AI 这么火,所以就打算想自己写一个 MCP Server 练手,用 AI 来导出。

准备工作

  1. 创建 API Token: https://quip.com/dev/token
  2. 查看 API 文档: https://quip.com/api
  3. 下载 Python SDK: https://github.com/quip/quip-api/tree/master/python
  4. 安装 gofastmcp 框架: https://gofastmcp.com/getting-started/installation

创建 MCP Server

首先创建一个 MCP Server 并包含一个简单的 tool。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from fastmcp import FastMCP
import quip

client = quip.QuipClient(api_token)
mcp = FastMCP("Quip")

@mcp.tool(output_schema={
"type": "object",
"properties": {
"id": {"type": "string", "description": "用户ID"},
"name": {"type": "string", "description": "用户名称"},
"desktop_folder_id": {"type": "string", "description": "桌面文件夹ID"},
"private_folder_id": {"type": "string", "description": "私有文件夹ID"},
"trash_folder_id": {"type": "string", "description": "回收站文件夹ID"},
"shared_folder_ids": {"type": "array", "items": {"type": "string"}, "description": "共享文件夹ID列表"},
"group_folder_ids": {"type": "array", "items": {"type": "string"}, "description": "群组文件夹ID列表"},
"subdomain": {"type": "string", "description": "用户子域"},
"url": {"type": "string", "description": "用户URL"},
},
"required": ["id", "name", "private_folder_id", "trash_folder_id"],
})
def get_current_user() -> dict:
"""获取当前用户信息"""
data = client.get_authenticated_user()
return data


if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
mcp.run(transport="sse", host="127.0.0.1", port=port)

接着运行该服务,然后通过 MCP Inspector 来进行调试: npx @modelcontextprotocol/inspector
在 MCP Inspector 界面内,Transport TypeSSEURLhttp://127.0.0.1:5000/sse,再点击 Connect 看看能否连接成功。
然后在右边的 Tools 标签点击 List Tools 看看能否显示出刚刚定义的这个 get_current_user 工具。
选择 get_current_user 然后点击 Run Tool 看看能否正常返回用户信息。

接着通过 AI 工具连接上该 MCP 服务,然后进行对话操作。

添加 MCP 服务:

1
2
3
4
5
6
7
8
9
{
"mcp": {
"quip": {
"type": "remote",
"url": "http://126.0.0.1:5000/sse",
"enabled": true
}
}
}

开启对话:

1
通过 Quip MCP 获取当前用户信息

看看能否正常返回信息。

以上只是一个简单的例子,完整的代码: https://github.com/wusuopu/QuipNoteExporter