跳转到主要内容

跨模型、工具、服务、公司、提供商、API、套餐、文章和新闻进行搜索。

MCP 协议进阶 45 分钟

构建你的第一个 MCP 服务器:动手分步指南

从零到可用:搭建项目、定义工具、连接客户端并测试——全程约一小时。

构建你的第一个 MCP 服务器:动手分步指南

What you will have at the end

A small but real MCP server with two tools (reading a file and counting words), registered in a real client and working. About an hour of work.

Step 1: scaffold the project

Create a folder, init a Python virtualenv or a Node project, and install the official MCP SDK. The whole server fits in one file; do not over-engineer it.

Step 2: define your first tool

Every tool has three parts: a name, a description (what the model reads to choose the tool) and an input schema. Deliberately start simple — say, "read the contents of a text file". Write the description seriously: "takes a file path and returns the full text" beats "read".

Step 3: connect it to a client

Register the server in your MCP client (editor or assistant) with one config entry: name, run command and arguments. If the connection fails, it is almost always one of: wrong executable path, an import error, or a stray print to stdout corrupting the protocol.

Step 4: test and debug

Call the tool in a chat and check three things: does the model pick the right tool? Does the output come back clean? And with bad input (a missing file), does it get a useful error message? Add the second tool right here, so the pattern "define → register → test" sticks.

Where to go next

After your first server, build something more useful: an internal database, a team API, a daily automation. The same four steps repeat — input schemas just get more professional and error handling more precise.