这是 Claude Desktop 实战指南的最终篇。
前六篇,我们学会了用 Claude Desktop 聊天、读文件、搜代码、管理项目、配置权限、使用 MCP 扩展。
今天,我们做一件更硬核的事:自己写一个 MCP 服务器,让 Claude 连接你们公司的内部系统。
现成的 MCP 扩展(GitHub、PostgreSQL、Playwright)覆盖的是通用场景。
但每家公司都有自己的内部系统:
这些系统,通用 MCP 扩展覆盖不了。你需要自己写一个。
简单说,MCP 服务器就是一个 遵循 MCP 协议的外部程序。
它做两件事:
Claude Desktop 通过 stdio(标准输入输出)和 MCP 服务器通信。你可以用任何语言写 MCP 服务器——Python、TypeScript、Go,都行。
假设你们公司有一个内部工单系统,提供一个 REST API:
GET https://internal.yourcompany.com/api/tickets?status=open
POST https://internal.yourcompany.com/api/tickets
{ "title": "...", "description": "..." }
你想让 Claude 能直接查询和创建工单。下面是完整的 Python MCP 服务器代码:
#!/usr/bin/env python3
# mcp_tickets.py
import sys, json, requests
BASE_URL = "https://internal.yourcompany.com/api"
API_TOKEN = "your-internal-api-token"
def handle_request(req):
method = req.get("method", "")
params = req.get("params", {})
if method == "initialize":
return {"protocolVersion": "2024-11-05", "capabilities": {}}
if method == "tools/list":
return {"tools": [
{
"name": "list_tickets",
"description": "列出所有开放状态的工单",
"inputSchema": {"type": "object", "properties": {}}
},
{
"name": "create_ticket",
"description": "创建一个新的工单",
"inputSchema": {
"type": "object",
"properties": {
"title": {"type": "string"},
"description": {"type": "string"}
},
"required": ["title", "description"]
}
}
]}
if method == "tools/call":
tool_name = params.get("name", "")
args = params.get("arguments", {})
if tool_name == "list_tickets":
resp = requests.get(BASE_URL+"/tickets?status=open",
headers={"Authorization": "Bearer "+API_TOKEN})
return {"content": [{"type": "text", "text": json.dumps(resp.json(), ensure_ascii=False)}]}
if tool_name == "create_ticket":
resp = requests.post(BASE_URL+"/tickets",
json={"title": args["title"], "description": args["description"]},
headers={"Authorization": "Bearer "+API_TOKEN})
return {"content": [{"type": "text", "text": "工单已创建,ID: "+str(resp.json().get("id"))}]}
return {"error": "unknown method"}
# MCP 主循环:从 stdin 读请求,向 stdout 写响应
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
req = json.loads(line)
resp = handle_request(req)
print(json.dumps(resp), flush=True)
except Exception as e:
print(json.dumps({"error": str(e)}), flush=True)
把上面的脚本保存为 mcp_tickets.py,然后在 C:\Users\[用户名]\.claude\config.json 里添加:
{
"mcpServers": {
"tickets": {
"command": "python3",
"args": ["/path/to/mcp_tickets.py"],
"env": {
"API_TOKEN": "your-token-here"
}
}
}
}
重启 Claude Desktop。现在你可以直接说:
list_ticketscreate_ticketMCP 服务器是通过 stdio 通信的,调试不太直观。几个实用技巧:
1. 手动测试 MCP 请求
用 echo 向你的脚本发 JSON 请求:
echo '{"method":"tools/list","params":{}}' | python3 mcp_tickets.py
2. 查看 Claude Desktop 的 MCP 日志
日志在 ~/.claude/logs/mcp.log。每次 Claude 调用 MCP 工具,这里都会有记录。
3. 用 claude mcp list 检查配置
这个命令会列出所有已配置的 MCP 服务器,以及它们的状态(运行中/未启动/报错)。
自定义 MCP 服务器的安全风险,比官方扩展更高:
七篇文章,从安装到精通:
| 篇 | 主题 |
|---|---|
| ① | 安装与初体验 |
| ② | 读懂 CLAUDE.md,让 AI 按你的规则工作 |
| ③ | 开发者模式与第三方模型接入 |
| ④ | 多会话工作流管理 |
| ⑤ | 项目与工作区管理 |
| ⑥ | MCP 扩展:连接数据库和 API |
| ⑦ | 自定义 MCP 服务器(本篇) |
到这里,Claude Desktop 的核心能力你已经全部掌握。剩下的,是在真实项目里用起来。
Claude Desktop 实战指南,七篇完结。关注我们,下个系列见。
评论区