Model Context Protocol 是一个开放标准它的目标是给 LLM 一种干净、统一的方式去发现和调用外部工具。不用再写自定义解析、不用再维护脆弱的胶水代码就是一个好用的协议。大多数 MCP 教程上来就讲 JSON-RPC 规范、传输层协议搞得很复杂。其实用 MCP 不需要理解协议内部构造就像写 Web 应用不需要去读 HTTP 规范一样。真正需要掌握的东西就三个概念花 15 分钟就够了。三个核心概念MCP 的核心就三样东西Server对外暴露工具的服务端本质上是一个 Python 脚本声明这些函数可以被 LLM 调用跑起来之后就在监听请求。Tool希望 LLM 使用的函数可以是任何东西查天气、查数据库、发邮件。这跟写普通 Python 函数没什么区别加个装饰器剩下的交给 MCP。Client连接 Server 并调用工具的客户端。生产环境里一般就是 LLM 应用本身。测试阶段可以用 FastMCP 自带的客户端开箱即用。Server 暴露工具Client 调用工具。就这么简单。传输方式、JSON-RPC、能力协商这些都是实现细节上生产之前不用管。步骤 1安装 FastMCPFastMCP 是让 MCP 用起来简单的 Python 框架。装一下就行不需要任何配置。pip install fastmcp本教程不需要虚拟环境生产环境建议还是用一个。步骤 2创建 Server新建一个my_server.py文件from fastmcp import FastMCP # Initialize the server with a name mcp FastMCP(my-first-server) # Define a tool using the mcp.tool decorator mcp.tool def get_weather(city: str) - dict: Get the current weather for a city. # In production, youd call a real weather API # For now, well return mock data weather_data { new york: {temp: 72, condition: sunny}, london: {temp: 59, condition: cloudy}, tokyo: {temp: 68, condition: rainy}, } city_lower city.lower() if city_lower in weather_data: return {city: city, **weather_data[city_lower]} else: return {city: city, temp: 70, condition: unknown} # Run the server if __name__ __main__: mcp.run(transportstdio)FastMCP(my-first-server)创建一个带名称的服务器实例。mcp.tool装饰器把普通函数注册为 MCP 工具。函数的 docstring 会变成工具描述——LLM 靠这个来判断什么时候该调用它。类型提示city: str、- dict告诉 MCP 输入输出的类型。transportstdio表示通过标准输入输出通信本地测试够用了。整个 Server 就这些实际代码 15 行。步骤 3写个 Client 测试一下新建test_client.pyimport asyncio from fastmcp import Client async def main(): # Point the client at your server file client Client(my_server.py) # Connect to the server async with client: # List available tools tools await client.list_tools() print(Available tools:) for tool in tools: print(f - {tool.name}: {tool.description}) print(\n *50 \n) # Call the weather tool result await client.call_tool( get_weather, {city: Tokyo} ) print(fWeather result: {result}) if __name__ __main__: asyncio.run(main())Client(my_server.py)指定要连接的 Server 文件async with client:自动管理连接生命周期list_tools()负责动态发现可用工具这是 MCP 的核心能力之一call_tool(get_weather, {city: Tokyo})带参数调用具体工具。步骤 4跑起来终端里执行python test_client.py输出应该是这样的Available tools: - get_weather: Get the current weather for a city.Weather result: {city: Tokyo, temp: 68, condition: rainy}到这里就完成了。一个 MCP Server 搭好了Client 也成功调用了它。步骤 5增加更多工具MCP 真正好用的地方在于扩展成本极低再往 Server 里再加两个工具from fastmcp import FastMCP from datetime import datetime mcp FastMCP(my-first-server) mcp.tool def get_weather(city: str) - dict: Get the current weather for a city. weather_data { new york: {temp: 72, condition: sunny}, london: {temp: 59, condition: cloudy}, tokyo: {temp: 68, condition: rainy}, } city_lower city.lower() if city_lower in weather_data: return {city: city, **weather_data[city_lower]} return {city: city, temp: 70, condition: unknown} mcp.tool def get_time(timezone: str UTC) - str: Get the current time in a specified timezone. # Simplified - in production use pytz or zoneinfo return fCurrent time ({timezone}): {datetime.now().strftime(%H:%M:%S)} mcp.tool def calculate(expression: str) - dict: Safely evaluate a mathematical expression. try: # Only allow safe math operations allowed_chars set(0123456789-*/.() ) if not all(c in allowed_chars for c in expression): return {error: Invalid characters in expression} result eval(expression) # Safe because we validated input return {expression: expression, result: result} except Exception as e: return {error: str(e)} if __name__ __main__: mcp.run(transportstdio)再跑一次测试客户端三个工具全部自动发现Available tools: - get_weather: Get the current weather for a city. - get_time: Get the current time in a specified timezone. - calculate: Safely evaluate a mathematical expression.不需要改配置不需要写路由。加了工具就直接可用。最后接入 LLM前面写的 Client 是测试用的。生产环境里LLM 框架本身充当 Client 角色。概念上大概是这样How MCP connects your LLM to external tools: the framework calls the client, which discovers and invokes tools from your server.Server 端的代码完全不用动这正是 MCP 的价值所在——工具写一次任何兼容 MCP 的客户端都能用。生产部署时需要把传输方式从stdio换成httpif__name____main__: mcp.run(transporthttp, host0.0.0.0, port8000)这样 MCP Server 就以 HTTP 端点的形式对外暴露远程客户端可以直接连接。总结现在你手头已经有一个能跑的 MCP Server 了前后也就 15 分钟。下一步就是把它接到实际的 LLM 上做点真正有用的东西出来。https://avoid.overfit.cn/post/c9314c34543a4ed1a1bb15b92d1c6ca2by Paolo Perrone