暗无天日

=============>DarkSun的个人博客

TIL: MCP 服务器不到 20 行 Python 就能写出来

是什么

Al Sweigart 在 The Simplest MCP Example Possible in Python 里演示了一个不到 20 行的 MCP 服务器:

from mcp.server.fastmcp import FastMCP
import datetime

mcp = FastMCP("TimeServer")

@mcp.tool()
def get_current_time() -> str:
    """Get the current local time of day, formatted as HH:MM:SS (24-hour clock)."""
    return str(datetime.datetime.now().time())[:8]

@mcp.tool()
def get_current_date() -> str:
    """Get the current local date, formatted as YYYY-MM-DD."""
    return str(datetime.datetime.now())[:10]

if __name__ == "__main__":
    mcp.run()

为什么这么简单

三个设计决策把代码量压到最少:

  1. @mcp.tool() 装饰器把普通函数注册为 MCP 工具,不用手写 JSON Schema
  2. docstring 直接当工具描述,LLM 靠它判断什么时候调用。用英文写效果最好,因为大多数 LLM 训练数据以英文为主
  3. stdio 传输( mcp.run() ),客户端把 server 当子进程启动,stdin/stdout 管道通信,不走网络

客户端怎么连

不用写客户端代码。Claude Code 原生支持 MCP 协议,配置文件加两行就行。

在项目根目录创建(或编辑)=.mcp.json=:

{
  "mcpServers": {
    "time-server": {
      "command": "python",
      "args": ["mcp_server.py"]
    }
  }
}

或者直接用 claude mcp add 命令添加(注意 -- 后面有空格):

claude mcp add time-server -- python mcp_server.py

添加后在对话中就能直接用了。比如问 "What time is it?" —— Claude Code 检测到 get_current_time 工具 → 自动调用 → 拿到结果回复你。

整个过程 LLM 自己决定「要不要调工具」「调哪个工具」。协议和 Claude Code 里用的 MCP 完全一致 —— 毕竟 Claude Code 本身就是 MCP 客户端。

MCP : Python : tool-calling