How to Build an MCP Server in Python and Connect It to Claude Code
说明与用途
使用Python构建MCP服务器,使Claude Code能够调用后端函数,例如查询订单状态,实现内部服务集成。
问题
Claude Code cannot access internal APIs or databases without explicit tools.
结果
After setting up a 20‑line Python MCP server and configuring Claude Code, the agent can query backend data such as order status, enabling seamless integration of internal services.
工作流
- Install the mcp SDK with
pip install mcp - Create a FastMCP server exposing tools via
@mcp.tool()decorators - Define a
get_order_status(order_id: str)function with a descriptive docstring - Define additional tools such as
add(a: int, b: int) - Run the server with
mcp.run() - Add a .mcp.json file pointing to the server script
- Restart Claude Code so it discovers the MCP server
- Ask Claude Code a question that triggers the tool, e.g., "what's the status of order 1002"
证据
Claude Code reads .mcp.json from your project root:
An MCP server is just a small program that exposes a list of tools (functions with descriptions).
Now when you ask "what's the status of order 1002", it can call your tool instead of telling you it can't.
- 记录日期: 2026-10-04T15:55:59.626Z