学习MCP
这个存储库是为了让我通过构建一个 简单的任务管理器应用程序。此应用程序使用
- MCP服务器(fastmcp)管理LLM和数据库之间的工具和交互
- API服务器(FastAPI)为MCP服务器公开端点,并允许用户与系统交互
- LLM (人类)解释用户请求并确定使用哪些工具,并生成对用户的响应
- 用于存储任务的PostgreSQL数据库
我还没有构建一个web界面(还没有?),所以与系统交互的主要方式是 通过WebSocket API或RESTful API。示例如下。
初始设置
docker compose build
docker compose up唯一需要的环境变量是 ANTHROPIC_API_KEY,但您也可以设置:
ANTHROPIC_MODELLOG_LEVEL
系统运行后,自动生成的FastAPI文档 可在http://localhost:8004/docs.
WebSocket API示例用法
WebSocket API接受JSON(预期会有web接口)。所以 对于像这样的工具来说,这不是最用户友好的体验 wscat.
为了让它更容易忍受,我编写了一个简单的python脚本来连接WebSocket API 并允许您以更用户友好的方式键入消息。你可以在以下网址找到它 pychat.py 在这个repo的根目录中。
输出被格式化以显示工具使用和结果,以及助手的响应(此项目 这是一个学习练习)。
下面是一些交互示例。
注意:Claude有时反应迟钝,因此您可能需要等待几秒钟才能收到回复。
$ python3 pychat.py
Connected to the chat server!
╭────────────── Chat Instructions ───────────────╮
│ Type your messages below. Type 'exit' to quit. │
╰────────────────────────────────────────────────╯
>> Hi! Can you add a task to buy berries?
>>
╭───────── Tool Use ──────────╮
│ Tool Used: create_task_tool │
│ Input: { │
│ "task": { │
│ "title": "Buy berries" │
│ } │
│ } │
╰─────────────────────────────╯
╭────────────────────────────────────────────────────────────────────────────────── Tool Result ──────────────────────────────────────────────────────────────────────────────────╮
│ Result: {"id":62,"title":"Buy berries","description":null,"status":"To Do","due_date":null,"created_at":"2026-01-05T22:03:05.238914","updated_at":"2026-01-05T22:03:05.238919"} │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────── Assistant ───────────────────────────────────────────────╮
│ Done! I've added a task "Buy berries" to your list. The task has been created with a status of "To Do". │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
>> Can you add a task to wash the car before next Saturday?
>>
╭────────────── Tool Use ───────────────╮
│ Tool Used: create_task_tool │
│ Input: { │
│ "task": { │
│ "title": "Wash the car", │
│ "due_date": "2026-01-10T23:59:59" │
│ } │
│ } │
╰───────────────────────────────────────╯
╭─────────────────────────────────────────────────────────────────────────────────────────── Tool Result ───────────────────────────────────────────────────────────────────────────────────────────╮
│ Result: {"id":63,"title":"Wash the car","description":null,"status":"To Do","due_date":"2026-01-10T23:59:59","created_at":"2026-01-05T22:03:39.236930","updated_at":"2026-01-05T22:03:39.236935"} │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────────────────────────────── Assistant ────────────────────────────────────────────────────────────────────────────╮
│ Done! I've added a task "Wash the car" with a due date of January 10th, 2026 (next Friday, before Saturday). The task has been created with a status of "To Do". │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
>> Could you list all the existing tasks?
>>
╭────────────────────────────────────────────────────────────────────────────────── Assistant ───────────────────────────────────────────────────────────────────────────────────╮
│ I don't have a tool available to list or retrieve existing tasks. I can only create new tasks with the `create_task_tool`. │
│ │
│ From our conversation, I know I've created these two tasks for you: │
│ 1. "Buy berries" (ID: 62, Status: To Do) │
│ 2. "Wash the car" (ID: 63, Status: To Do, Due: January 10th, 2026) │
│ │
│ But I'm unable to show you a complete list of all tasks that might exist in your system. You would need to check your task management interface directly to see the full list. │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
exit
Bye!
Connection closed by the server.RESTful API示例用法
以下是一些REST示例(更多信息请参见上述FastAPI文档):
列出所有MCP工具:
调用MCP工具端点以列出所有可用工具。
$ curl -s http://localhost:8004/api/mcp/tools | jq .
{
"tools": [
{
"name": "return_fourty_two",
"description": "Return the number 42"
},
{
"name": "create_task_tool",
"description": "MCP Tool: Create a new task"
},
{
"name": "get_tasks_tool",
"description": "MCP Tool: get all tasks from database"
}
]
}列出数据库中的所有任务:
调用MCP工具 get_tasks_tool 从PostgreSQL数据库中检索所有任务。
$ curl -s http://localhost:8004/api/mcp/tasks | jq .
{
"tasks": [
{
"id": 40,
"title": "Clean the garage",
"description": null,
"status": "To Do",
"due_date": null,
"created_at": "2026-01-05T15:53:59.960202",
"updated_at": "2026-01-05T15:53:59.960203"
},
...
]
}通过聊天端点创建新任务:
这将调用LLM来解释请求,并使用适当的MCP工具来创建 任务:
$ curl -s -X POST http://localhost:8044/api/chat \
-H "Content-Type: application/json" \
-d '{
"messages": "Could you please create a new task to finish the report by next Friday?"
}' | jq .这种POST的最终响应类似于:
I've created the task \"Finish the report\" with a note that it's due next Friday.
Since today appears to be January 5, 2026, next Friday would be January 9, 2026.
Would you like me to update the task with the specific due date of January 9, 2026?对于HTTP请求,回答LLM的后续问题有点笨拙。 但您可以在数据库中看到新创建的任务:
$ curl -s http://localhost:8004/api/mcp/tasks | jq .
...
{
"id": 41,
"title": "Finish the report",
"description": "Due by next Friday",
"status": "To Do",
"due_date": null,
"created_at": "2026-01-05T16:14:37.251711",
"updated_at": "2026-01-05T16:14:37.251714"
},
...