MCP服务器新手教程2026
现在每个AI工具都提MCP,但没人从零讲起。
MCP Server Tutorial for Beginners 2026: From Zero to Your First AI Tool Integration
This tutorial assumes you have never heard of MCP before and want to understand it by doing. By the end you will have connected an AI assistant to a real external tool.
What You Are Building
An MCP server that lets Claude Desktop read a local todo list. When you ask "what is on my todo list?", Claude calls your server, reads the file, and answers.
Step 1: Understand the Three Roles
- MCP Client - the AI assistant (Claude Desktop, Cline, etc.)
- MCP Server - a small program that wraps a tool or data source
- Transport - how they talk (stdio on your machine, HTTP over network)
Step 2: Write the Server
Create todo_server.py:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Todo Server")
@mcp.tool()
def add_todo(item: str) -> str:
with open("todos.txt", "a") as f:
f.write(item + "
")
return f"Added: {item}"
@mcp.tool()
def list_todos() -> str:
try:
with open("todos.txt") as f:
return f.read() or "Empty"
except FileNotFoundError:
return "Empty"
Step 3: Register with Claude Desktop
- Open Claude Desktop > Settings > Developer
- Click Edit Config, add the server with command python and args pointing to your script
- Restart Claude Desktop
Step 4: Use It
Type: "Add buy milk to my todo list, then show me the list."
Claude calls add_todo, then list_todos, and answers with the file contents. No prompt engineering, no glue code - the protocol did the wiring.
Common Beginner Mistakes
| Mistake | Fix |
|---|---|
| Wrong path in args | Use absolute paths |
| Server crashes on start | Run python todo_server.py in terminal first to see errors |
| Claude says no tools available | Restart Claude after config changes |
| Windows npx errors | Use cmd /c npx ... as the command |
FAQ
Do I need to know programming? For this tutorial, copy-paste is enough. To build production servers you need basic Python or TypeScript.
Is MCP only for local use? No - servers can run remotely over HTTP/SSE, which is how SaaS tools expose their data to AI.
How much does it cost? The protocol and SDK are free and open source.
