從官方拷貝的架構圖,幾個組件介紹下:

MCP Server 的通信方式有多種,下面介紹兩種主要的:基于標準輸入輸出(stdio)的本地通信和基于SSE(Server-Sent Events)的遠程通信。

最后看下MCP的基本工作流程:

圖片來自https://zhuanlan.zhihu.com/p/29001189476

1. 工具描述與上下文注入:MCP Server 會預先定義工具的 名稱、描述、通信方式 ,并將這些信息以結構化文本形式注入大模型的上下文;

2. 選擇工具:大模型通過分析用戶指令和工具描述,推理出需調用哪種工具。當然也可以選擇不調用,如上圖的Case2;

3. MCP Client發起工具調用:大模型結合用戶輸入和實時上下文,生成符合工具要求的 JSON-RPC 請求 ,包含工具名稱和參數;

4. MCP Server執行工具:Server 解析請求,調用對應的工具處理函數,并將結果封裝為 JSON-RPC 響應;

5. 結果返回與模型整合:大模型可根據工具返回的結果動態更新上下文,生成最終回答。

1.2 Cursor

官方文檔:https://docs.cursor.com/get-started/introduction

IDE側邊欄的Chat面板大家都很熟悉了,之前使用起來總覺得差點意思。這兩天試玩了Cusor,直呼“真香”。

因為下面Cursor實戰的時候會用到Docs、Rules、MCP這幾個功能。MCP前面介紹過了,下面看下Cursor中關于Docs和Rules的介紹:

1.2.1 Docs

Docs:通過對第三方文檔索引后插入到上下文。

比如上圖我在Docs插入了MCP的文檔,在Chat中就可以通過@Docs選擇文檔添加到上下文。

1.2.2 Rules

官方文檔:https://docs.cursor.com/context/rules-for-ai

Rules:規則,可以簡單理解為提示詞,有全局User Rules(應用于所有項目)和項目Project Rules

項目規則如何匹配,有以下幾種方式:

常用的就是Agent Requested,提供相關規則描述后,在使用Agent對話時會自動根據上下文內容選擇規則應用。

二、使用Cursor實戰開發MCP

2.1 cursor開發MCP Server

1.先創建一個文件夾,然后終端輸入cursor .根據當前目錄打開cursor。

2.創建兩個Project Rule:

提示詞當然不需要我們寫,直接將參考的內容丟給cursor:

有了上面兩個項目得力助手,下面正式開發一個查詢天氣信息的mcp server。

3. 執行pnpm init命令建立typeScript初始環境,然后在src目錄下創建一個index.ts文件,基于該文件開啟Agent Chat(兩個github倉庫分別是官方的typeScript SDK和quickStart demo):

4. main文件即index.ts有了,還需要對這個項目執行打包。從下圖可以看到,他會自動幫我們完善package.json引入相關依賴和創建tsconfig.json。更令人驚喜的是,像執行npm run build等命令時,他能結合命令報錯信息修改優化代碼,直到項目能順利構建(想起以前構建項目各種報錯只能谷歌,一個個翻歷史帖子,現在真的太幸福了bushi)。

下面貼出cursor生成的index.ts文件:

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

interface WeatherData {
city: string;
temperature: number;
condition: string;
forecast: {
date: string;
condition: string;
high: number;
low: number;
}[];
lastUpdated: string;
}

// 模擬天氣數據存儲
const weatherDatabase: Record<string, WeatherData> = {
"北京": {
city: "北京",
temperature: 22,
condition: "晴朗",
forecast: [
{ date: "2025-04-08", condition: "晴朗", high: 24, low: 15 },
{ date: "2025-04-09", condition: "大雪", high: 23, low: 14 },
{ date: "2025-04-10", condition: "大風", high: 20, low: 12 }
],
lastUpdated: new Date().toISOString()
},
"上海": {
city: "上海",
temperature: 24,
condition: "多云",
forecast: [
{ date: "2025-04-08", condition: "多云", high: 25, low: 17 },
{ date: "2025-04-09", condition: "陰天", high: 24, low: 16 },
{ date: "2025-04-10", condition: "小雨", high: 22, low: 15 }
],
lastUpdated: new Date().toISOString()
},
"廣州": {
city: "廣州",
temperature: 28,
condition: "晴朗",
forecast: [
{ date: "2025-04-08", condition: "晴朗", high: 30, low: 21 },
{ date: "2025-04-09", condition: "晴朗", high: 29, low: 21 },
{ date: "2025-04-10", condition: "多云", high: 28, low: 20 }
],
lastUpdated: new Date().toISOString()
}
};

// 創建 MCP 服務器
const server = new McpServer({
name: "天氣服務",
version: "1.0.0",
description: "提供城市天氣信息的MCP服務"
});

// 定義資源:獲取城市天氣數據
server.resource(
"weatherData",
"weather://{city}",
async (uri) => {
// 從URI中提取城市名稱
const city = uri.pathname.split('/').pop() || "";

if (!weatherDatabase[city]) {
return {
contents: [{
uri: uri.href,
text: 未找到城市 ${city} 的天氣數據 }], isError: true }; } return { contents: [{ uri: uri.href, text: JSON.stringify(weatherDatabase[city], null, 2) }] }; } ); // 定義資源:獲取所有支持的城市列表 server.resource( "cityList", "weather://cities", async (uri) => { return { contents: [{ uri: uri.href, text: JSON.stringify(Object.keys(weatherDatabase), null, 2) }] }; } ); // 定義工具:獲取天氣預報 server.tool( "getWeatherForecast", { city: z.string().describe("要查詢天氣預報的城市名") }, async ({ city }) => { if (!weatherDatabase[city]) { return { content: [{ type: "text", text: 城市 ${city} 的天氣數據不存在 }], isError: true }; } const weather = weatherDatabase[city]; return { content: [{ type: "text", text: `${city}天氣預報:\n當前溫度: ${weather.temperature}°C, 天氣狀況: ${weather.condition}\n未來三天預報:\n${ weather.forecast.map(day => ${day.date}: ${day.condition}, ${day.low}°C - ${day.high}°C).join('\n') }\n最后更新時間: ${weather.lastUpdated}` }] }; } ); // 定義工具:更新城市天氣 server.tool( "updateWeather", { city: z.string().describe("要更新天氣的城市名"), temperature: z.number().describe("當前溫度(°C)"), condition: z.string().describe("天氣狀況描述") }, async ({ city, temperature, condition }) => { if (!weatherDatabase[city]) { // 如果城市不存在,則創建新記錄 weatherDatabase[city] = { city, temperature, condition, forecast: [ { date: "2025-04-08", condition: condition, high: temperature + 2, low: temperature - 5 }, { date: "2025-04-09", condition: condition, high: temperature + 1, low: temperature - 6 }, { date: "2025-04-10", condition: condition, high: temperature, low: temperature - 7 } ], lastUpdated: new Date().toISOString() }; } else { // 更新現有記錄 weatherDatabase[city].temperature = temperature; weatherDatabase[city].condition = condition; weatherDatabase[city].lastUpdated = new Date().toISOString(); } return { content: [{ type: "text", text: 已成功更新${city}的天氣數據: 溫度 ${temperature}°C, 天氣狀況 ${condition} }] }; } ); // 定義提示詞:天氣查詢 server.prompt( "weatherQuery", { city: z.string().describe("城市名稱") }, ({ city }) => ({ messages: [{ role: "user", content: { type: "text", text: 請提供${city}的詳細天氣信息,包括溫度、天氣狀況和未來幾天的預報。 } }] }) ); // 定義提示詞:天氣建議 server.prompt( "weatherAdvice", { city: z.string().describe("城市名稱"), activity: z.string().describe("計劃的活動") }, ({ city, activity }) => ({ messages: [{ role: "user", content: { type: "text", text: 請根據${city}的天氣情況,給我關于${activity}的建議。考慮溫度、天氣狀況等因素。 } }] }) ); // 啟動服務器 async function main(){ const transport = new StdioServerTransport(); console.log("天氣服務啟動中..."); try { await server.connect(transport); console.log("天氣服務已連接"); } catch (error) { console.error("服務器連接失敗:", error); } } main().catch(console.error);

項目構建完成后,會在build目錄下生成一個index.js,下面通過命令的方式調用這個天氣mcp server。

Cursor添加MCP路徑在Cursor Settings->MCP->Add new global MCP Server:

{
"mcpServers": {
"weather-mcp-server": {
"type": "command",
"command": "node /Users/jayzx/CursorProjects/cursor-mcp-demo/quickstart-resources/weather-mcp-server/build/index.js"
}
}
}

讓我們測試一下:

2.2 cursor開發黃金價格預測項目

上面使用cursor開發MCP的練習似乎簡單了,懷著對cursor開發可用項目能力的質疑,下面動手做點復雜的東西。聯想到關注大黃近兩個月來的瘋狂漲勢(670到820),每次賣出都恨自己賣早了,如果能結合大模型分析未來幾天的黃金趨勢,是不是可以一定程度上提高短期投資收益。

黃金價格預測的數據來源基于三部分:黃金的日K線、當前黃金最新價格、有關黃金和美國的市場快訊。

2.2.1 開發

這個項目總共花費了十幾個小時,真正我做的內容其實就是上圖標黃的部分。其余耗時比較長的部分羅列如下:

2.2.2 優缺點

優點:

缺點:

三、使用Cursor開發MCP的一些感受和技巧

簡單幾天cursor體驗下來,感覺網傳的零基礎小白上手開發app上線或許多少有點夸大其詞,稍微復雜點的項目,比如涉及前后端、部署上線等,還是需要使用者具備一定的開發基礎的(常用的node、python、linux、git命令)。因為你只有能看懂前后端大概在干什么,才能在出現問題的時候輔助cursor修復優化。

但是cursor的作用也是不能讓人忽略的,尤其對于前端or后端開發走向全棧提供了一個簡單有效的捷徑。

文章前面已經提到些cursor使用技巧,最后簡單補充總結下:

最后給出Prajwal Tomar總結的10條cursor玩法:

文章轉載自: Cursor入門:MCP開發調用和項目實戰

上一篇:

深度長文 | 康奈爾最新論文:你真的了解AI Agent嗎?

下一篇:

輕松使用此API移除背景
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費