欧美精产国品一二三区,国产成人一区二区三区A片免费,特级毛片www免费版,成人做爰A片免费看黄冈宾馆,日韩精品人妻中文字幕有码

【入(ru)門(men)】使用Node.js開發一個MCP服務器

介紹

一(yi)(yi)個(ge)小小后端碼(ma)農,研究了(le)一(yi)(yi)下午,終于(yu)搞明白怎么開(kai)發一(yi)(yi)個(ge)nodeJs的(de)MCP服務器,特(te)寫成(cheng)一(yi)(yi)篇粗略的(de)博客,供大家參考。

MCP 是什么?

MCP(Model Control Protocol)是一個標(biao)準(zhun)化(hua)接(jie)口協議,用(yong)于(yu)定義AI工具(ju)(ju)的功能(neng)和參(can)數格(ge)式(shi)(shi)。它允(yun)許AI以標(biao)準(zhun)方式(shi)(shi)調用(yong)各種工具(ju)(ju),例如通過定義參(can)數格(ge)式(shi)(shi)(如城市(shi)名稱)來獲取城市(shi)天(tian)氣信(xin)息。當用(yong)戶(hu)請求查詢(xun)北(bei)京(jing)天(tian)氣時,AI會按照MCP標(biao)準(zhun)參(can)數格(ge)式(shi)(shi)組裝參(can)數,調用(yong)MCP服(fu)務器(qi)執行(xing)相應功能(neng),并處(chu)理返回結果。

開發MCP服務器(Node.js方式)

提示:首先需要下載安裝nodeJs

下邊(bian)我(wo)將帶你開發一個(ge)簡單(dan)的MCP服務器:

文件結構

3bc543b87ec00df5f2d5f2d2c47c7411

package.json 文件(jian):

{
  "name": "mcp-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@modelcontextprotocol/sdk": "^1.20.2",
    "zod": "^3.23.8"
  },
  "type": "module"
}

demo-server.js:

#!/usr/bin/env node

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

const server = new McpServer({
    name: "demo_service",
    version: "1.0.0"
});

server.tool(
    "say_hello",
    {
        needShowMeText: z.string().describe("想要展示的話")
    },
    async ({ needShowMeText }) => {
        try {
            // 返回成功響應
            return {
                content: [{ type: "text", text: 'Hello =>' + needShowMeText }]
            };
        } catch (error) {
            // 錯誤處理
            return {
                content: [{ type: "text", text: `失敗: ${error.message}` }],
                isError: true
            };
        }
    }
);

async function main() {
    try {
        console.log("MCP服務器啟動中...");
        const transport = new StdioServerTransport();
        await server.connect(transport);
        console.log("MCP服務器已啟動并等待連接");
    } catch (error) {
        console.error("啟動服務器時出錯:", error);
        process.exit(1);
    }
}

main();

在根目錄下,cmd 命令框輸入 npm install 進行安裝相(xiang)關依(yi)賴(lai):

8061b24924ec9ab2707c51d6cd8146b2

使用node ./dist/demo-service.js 測(ce)試是否可以啟(qi)動(dong)成功

e161f2bcd7aed04583db05305710a9fd

使用官方工具測試

使用 mcp-inspector 進行測(ce)試,打開新終端(duan)輸入以(yi)下(xia)命令:

npx @modelcontextprotocol/inspector

50876d5489520ea3396c72853a143867

按圖中步驟輸(shu)入(ru)各(ge)項(xiang)參數:

41882acefcfebe505af194abc453f2ce

527a46109cfed5c0da4244b3148f9eb1

看到Tool Result: Success便成功了

Qoder、IDEA等代碼編輯器集成MCP服務器

打開 文件 -> 首選項 -> Qoder 設置 -> MCP服務 -> +添加


{
    "mcpServers": {
        "mcp本地測試3": {
            "name": "mcp本地測試3",
            "command": "node",
            "args": [
                "G:\\Dev\\Code\\TestSource\\mcp-test\\dist\\demo-server.js"
            ]
        }
    }
}

1a5a4fd90635488744ac0d4b7a45d094

這樣MCP安裝便成功了!

使用

新建會話窗口,輸入 使用 say_hello 工具告訴我world

bb9ff338aa603cbe76fe60b0467e62b0

總結

MCP 有(you)什(shen)么作用(yong)呢?我(wo)個人理解的是(shi)它可以更加精(jing)準控制(zhi)程(cheng)序的中間(jian)執行過程(cheng),準確(que)(que)地獲(huo)取數據(ju),然(ran)后(hou)(hou)使用(yong)AI能力進行分(fen)析(xi)。比如,我(wo)們需要獲(huo)取北京今天的天氣,如果沒(mei)有(you)MCP服務器(qi),服務器(qi)可能會(hui)從海量(liang)的網頁(ye)中尋(xun)找(zhao)數據(ju)/接口,我(wo)們無法控制(zhi)最終獲(huo)取的結果。如果我(wo)們不(bu)能控制(zhi)數據(ju)源的準確(que)(que)性,那么后(hou)(hou)續的分(fen)析(xi)過程(cheng)也沒(mei)有(you)任何意義。

MCP服(fu)務(wu)就是封裝了(le)一個(ge)個(ge)接(jie)口的(de)(de)(de)服(fu)務(wu)器,我們可(ke)以從接(jie)口獲(huo)取(qu)我們想要的(de)(de)(de)準確數據,然(ran)后通過(guo)AI的(de)(de)(de)理解能(neng)力,幻化(hua)出各種各樣的(de)(de)(de)效果,MCP服(fu)務(wu)器開(kai)發過(guo)程,就是按MCP標準封裝一個(ge)個(ge)接(jie)口。

posted @ 2025-11-03 22:24  帥氣的濤啊  閱讀(53)  評論(1)    收藏  舉報