前往 WaytoAGI 飞书知识库 · 1 亿+ 次访问的开源 AI 知识库 →
通往 AGI 之路 WaytoAGI

知识库 · AI 智能体

从0开始,手把手教你开发第一个MCP服务!(2025 年 3 月更新版)

本教程手把手教你用纯自然语言在15分钟内开发第一个MCP服务,无需编程基础。通过Cursor和Windsurf最新更新,快速掌握MCP协议,构建可复用AI工具。

MCPAI开发Cursor教程零基础

Cursor 和 Windsurf 这两款神器又双叒叕更新了!

两天小更新,三天大更新,关于 MCP 的内容,也在一直变化!

为了不让大家掉队,我们的 MCP 开发教程也在疯狂进化中!💪

得益于好友「好记星」的鼎力相助,这次 MCP 开发的门槛又被我们无情地「腰斩」了!

我们将持续探索,普惠 AI 编程,欢迎关注这场技术革命!🔥

📚 这是一个超简单的入门教程,带你用纯自然语言开发出人生第一个MCP服务!

⏱️ 预计动手时间:仅需15分钟!

💡 适合人群:

  • 想快速入门MCP的小伙伴

  • 对AI开发感兴趣但没编程基础的朋友

  • 不想写代码但想做开发的同学

准备好了吗?让我们开始这段有趣的旅程吧!🚗💨

🎯 我用 MCP 做了什么

这是我开发的一个 MCP Server(MCP 服务),它可以实现加法运算:

通过 MCP,我们可以让 AI 使用各种强大工具,比如:

  • 查询实时天气 🌤️

  • 联网搜索最新信息 🔍

  • 调用私人数据库 💾

比如我在 Supabase(云端数据库) 上存放了自己的提示词库,每次使用时都通过 MCP 去调用它:

最棒的是,我不仅自己能用,还能分享给别人,让他们通过 MCP 使用我的提示词!简直不要太方便!

但现在,先跟着我的步伐,学会做一个简版 MCP 吧!🫡

一步一个脚印,我们一起加强开发能力!

🧠 第一步:给AI补课(没错,AI也要学习!)

诶?等等!你可能会问:为啥要先教AI什么是MCP?这不是显得多此一举吗?

重点来了! 就连Claude官方文档都特别说明了:它对MCP的认知还不够完整,需要我们投喂一些资料!(没错,AI也要补课,哈哈哈~)

此处提示词,由「好记星」精心调教:

sql
<基础知识>
## MCP
Model Context Protocol(MCP) 是一种开放协议,它标准化了应用程序如何向LLMs提供上下文。将 MCP 想象成 AI 应用的 USB-C 端口。就像 USB-C 提供了一种标准化的方式将您的设备连接到各种外围设备和配件一样,MCP 提供了一种标准化的方式将 AI 模型连接到不同的数据源和工具。
MCP 帮助您在LLMs之上构建代理和复杂的流程。LLMs通常需要与数据和工具集成,MCP 提供:

## 为什么选择 MCP?

一个不断增长的预构建集成列表,您的LLM可以直接插入
可切换至LLM提供商和供应商的灵活性
最佳实践:在您的基础设施中保护您的数据

## 通用架构
在其核心,MCP 遵循客户端-服务器架构,其中主机应用程序可以连接到多个服务器:
'''mermaid
flowchart TD
    subgraph Your_Computer["Your Computer"]
        Host[Host with MCP Client<br/>(Claude, IDEs, Tools)]
        Server_A[MCP Server A]
        Server_B[MCP Server B]
        Server_C[MCP Server C]
        Data_A[Local Data Source A]
        Data_B[Local Data Source B]
    end
    
    subgraph Internet["Internet"]
        Remote_Service_C[Remote Service C]
    end
    
    Host -- MCP Protocol --> Server_A
    Server_A -- MCP Protocol --> Host
    Server_A --> Data_A
    
    Host -- MCP Protocol --> Server_B
    Server_B -- MCP Protocol --> Host
    Server_B --> Data_B
    
    Host -- MCP Protocol --> Server_C
    Server_C -- MCP Protocol --> Host
    Server_C -->|"Web APIs"| Remote_Service_C
'''
* MCP 主机:如 Claude 桌面、IDE 或希望通过 MCP 访问数据的 AI 工具
* MCP 客户端:与服务器保持 1:1 连接的协议客户端
* MCP 服务器:通过标准化的模型上下文协议暴露特定功能的轻量级程序
* 本地数据源:您的计算机文件、数据库以及 MCP 服务器可以安全访问的服务
* 远程服务:可通过互联网(例如,通过 API)访问的外部系统(例如,MCP 服务器可以连接到)

## 截止2025年3月兼容性
* Cursor 不支持 MCP resources 和 MCP prompts 功能,目前只支持 Tools

## 开发参考

'''
server.tool() 的类型定义
/**
* Registers a zero-argument tool `name`, which will run the given function when the client calls it.
*/
tool(name: string, cb: ToolCallback): void;
/**
* Registers a zero-argument tool `name` (with a description) which will run the given function when the client calls it.
*/
tool(name: string, description: string, cb: ToolCallback): void;
/**
* Registers a tool `name` accepting the given arguments, which must be an object containing named properties associated with Zod schemas. When the client calls it, the function will be run with the parsed and validated arguments.
*/
tool<Args extends ZodRawShape>(name: string, paramsSchema: Args, cb: ToolCallback<Args>): void;
/**
* Registers a tool `name` (with a description) accepting the given arguments, which must be an object containing named properties associated with Zod schemas. When the client calls it, the function will be run with the parsed and validated arguments.
*/
tool<Args extends ZodRawShape>(name: string, description: string, paramsSchema: Args, cb: ToolCallback<Args>): void;
 '''

'''
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

// Create an MCP server
const server = new McpServer({
  name: "Demo",
  version: "1.0.0"
});

// Add an addition tool
server.tool("add",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a + b) }]
  })
);

// Async tool with external API call
server.tool(
  "fetch-weather",
  { city: z.string() },
  async ({ city }) => {
    const response = await fetch(`https://api.weather.com/${city}`);
    const data = await response.text();
    return {
      content: [{ type: "text", text: data }]
    };
  }
);

// Start receiving messages on stdin and sending messages on stdout
const transport = new StdioServerTransport();
await server.connect(transport);
'''

## 开发目录结构

'''
mcp-server-demo
├── bin
│   └── index.js     可执行文件入口
├── dist
│   └── index.js     构建 Bundle
├── src
│   └── index.js     项目入口
└── package.json     包配置
'''

## package.json

'''
{
  "name": "{需要询问用户}",
  "version": "1.0.0",
  "type": "module",
  "files": [
    "bin",
    "dist"
  ],
  "bin": {
    "{需要询问用户}": "bin/index.js"
  },
  "scripts": {
    "start": "node dist/index.js",
    "dev": "npm run build && npx -y @modelcontextprotocol/inspector npm run start",
    "build": "esbuild src/index.ts --bundle --loader:.md=text --format=esm --platform=node --target=node22 --outfile=dist/index.js",
    "prepublishOnly": "npm run build"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@modelcontextprotocol/sdk": "^1.5.0",
    "zod": "^3.24.2",
    "zod-to-json-schema": "^3.24.1"
  },
  "devDependencies": {
    "@types/node": "22",
    "esbuild": "^0.25.0",
    "typescript": "^5.7.3"
  }
}

'''

## json配置

'''
{
  "mcpServers": {
    "prompt": {
      "command": "node",
      "args": ["server.js", "--sse"],
    }
  }
}

'''
</基础知识>

<核心指令>
用户是一个小白用户,请引导用户完成需求梳理,再帮助用户完成MCP Server 从搭建到调试
MCP Server是一个比较新的概念,当涉及到该概念的任何问题时,首先参考<基础知识>,然后使用web_search进行搜索后,再进行回答
注意用户并没有技术背景,你需要深入浅出的回答问题,更主动的引导和尝试
</核心指令>

<需求描述>
我想要做一个xxxx MCP Server,它要提供3个tool,分别是xxx xxx
</需求描述>

PS:我做了个有趣的实验,如果不告诉AI这些资料会发生什么...

结果惨不忍睹...全是错的!这波操作告诉我们:AI也需要好好学习天天向上!😂

🎮 第二步:开始我们的指挥!

复制下方指令,发送给AI:

java
开发一个 MCP Server @说明文件

AI就会开始行动啦!

中间如果AI卡住了,别担心!只要轻轻地对它说一声「继续」,它就会重新燃烧起来!🔥

🎉 第三步:见证奇迹的时刻!

当你看到类似下图的内容时,恭喜你!这意味着我们的部署已经成功啦!

⚙️ 第四步:进行配置(最后一步啦!)

如果你是 Cursor 0.46 版本

用这个口令召唤配置:

java
如何配置

我们会看到他的配置项,记住这些怎么写

我们打开 cursor,按照 1、2、3 的顺序,打开设置

根据上图的说明,填入下面的表格里:

  • 名称(Name):输入一个方便识别的名称,例如「演示服务器」

  • 类型(Type):选择「command」

  • 命令(Command):粘贴AI给你的命令

如果你是 Windsurf 或者 Cursor 0.47+ 版本

需要这样询问AI:

AI就会给出一个这样的配置说明:

我们复制这段代码,粘贴到 MCP 设置里

Cursor 0.47设置在哪

按照图中顺序点击

cursor 会看到这个名称

我们把代码粘贴进去就好啦!

Windsurf 设置在哪

按照图中顺序点击

打开设置后, Windsurf会看到这个名称

我们把代码粘贴进去就好啦!