news 2026/6/10 16:00:17

入门】使用Node.js开发一个MCP服务器(STDIO方式)介绍

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
入门】使用Node.js开发一个MCP服务器(STDIO方式)介绍

CP(Model Control Protocol)是一个标准化接口协议,用于定义AI工具的功能和参数格式。它允许AI以标准方式调用各种工具,例如通过定义参数格式(如城市名称)来获取城市天气信息。当用户请求查询北京天气时,AI会按照MCP标准参数格式组装参数,调用MCP服务器执行相应功能,并处理返回结果。

开发MCP服务器(Node.js方式)

提示:首先需要下载安装nodeJs

下边我将带你开发一个简单的MCP服务器:

文件结构

3bc543b87ec00df5f2d5f2d2c47c7411

package.json 文件:

{

"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 进行安装相关依赖:

8061b24924ec9ab2707c51d6cd8146b2

使用node ./dist/demo-service.js 测试是否可以启动成功

e161f2bcd7aed04583db05305710a9fd

使用官方工具测试

使用 mcp-inspector 进行测试,打开新终端输入以下命令:

npx @modelcontextprotocol/inspector

50876d5489520ea3396c72853a143867

按图中步骤输入各项参数:

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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 12:50:30

Laravel 新项目避坑指南10 大基础设置让代码半年不崩

有没有遇到过这种 Laravel 项目:刚上线那会儿干干净净,过三个月就变成无法收拾的灾难?Controller 动不动就 500 多行、慢得要命的数据库查询随处可见,甚至有人把 .env 推上 GitHub,所有密钥一夜之间全线暴露。 别以为只…

作者头像 李华
网站建设 2026/6/10 12:52:29

20款必备Lens插件:彻底改变你的Kubernetes管理体验

20款必备Lens插件:彻底改变你的Kubernetes管理体验 【免费下载链接】lens Lens - The way the world runs Kubernetes 项目地址: https://gitcode.com/gh_mirrors/le/lens Lens插件生态系统为Kubernetes集群管理带来了革命性的效率提升,通过丰富的…

作者头像 李华
网站建设 2026/6/10 12:52:19

Advanced Charging Controller:终极电池保养指南

Advanced Charging Controller:终极电池保养指南 【免费下载链接】acc Advanced Charging Controller 项目地址: https://gitcode.com/gh_mirrors/ac/acc 想要延长手机电池寿命却不知从何下手?Advanced Charging Controller (ACC) 正是你需要的解…

作者头像 李华
网站建设 2026/6/10 12:52:09

python爬取简书首页前10文章

爬虫代码——爬取简书首页前10文章 from bs4 import BeautifulSoup from urllib.request import urlopen, Request # 配置 User-Agent 并创建 Request 对象 headers {User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0} req Request(url…

作者头像 李华
网站建设 2026/6/10 14:32:33

超实用文本转拼音工具推荐:88box 拼音转换神器

超实用文本转拼音工具推荐:88box 拼音转换神器 如果你经常需要将中文文本转为拼音,无论是日常学习、工作文档处理,还是内容创作中的拼音标注需求,这款 https://88box.top/text-tools/pinyin 文本转拼音工具绝对值得一试&#xff…

作者头像 李华