
使用liteMCP构建一个Hello MCP!
liteMCP
和之前步骤几乎一模一样,只有语法和依赖上的区别,因此将简化说明
src/index.ts
- MCP服务器实现,提供两个工具:say_hello
和add
package.json
- 项目依赖和脚本tsconfig.json
- TypeScript配置
MCP服务器搭建步骤
1. 初始化项目
# 创建项目目录
mkdir litemcp_demo
cd litemcp_demo
# 初始化npm项目
npm init -y
2.安装依赖
# 安装主要依赖
npm install litemcp zod --save
# 安装开发依赖
npm install typescript @types/node --save-dev
依赖说明:
litemcp
: LiteMCP框架,用于简化MCP服务器的开发zod
: 数据验证库,用于参数验证typescript
: TypeScript编译器@types/node
: Node.js的TypeScript类型定义
2. 配置TypeScript
创建tsconfig.json
文件:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "build",
"rootDir": "src",
"sourceMap": true,
"declaration": true,
"resolveJsonModule": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "build"]
}
target
: 目标ECMAScript版本module
和moduleResolution
: 使用Node.js的ESM模块系统outDir
: 编译输出目录rootDir
: 源代码根目录其他选项设置类型检查和代码生成行为
3. 编写MCP服务器代码
创建src/index.ts
文件:
下方展示同样的功能,双方代码区别并不是很大,litemcp稍微简约一些,但官方版本教程相对更多一些
//litemcp代码
#!/usr/bin/env node
import { LiteMCP } from "litemcp";
import { z } from "zod";
// 创建LiteMCP服务器
const server = new LiteMCP("hello-world", "1.0.0");
// 添加一个简单的打招呼工具
server.addTool({
name: "say_hello",
description: "问候指定的名字",
parameters: z.object({
name: z.string().describe("要问候的名字")
}),
execute: async (args) => {
server.logger.debug("正在问候", args);
return \`你好,\${args.name}!欢迎使用MCP!\`;
}
});
// 添加一个加法工具
server.addTool({
name: "add",
description: "计算两个数字的和",
parameters: z.object({
a: z.number().describe("第一个数字"),
b: z.number().describe("第二个数字")
}),
execute: async (args) => {
server.logger.debug("正在计算两个数字的和", args);
return \`\${args.a} + \${args.b} = \${args.a + args.b}\`;
}
});
// 启动服务器
server.start();
console.error("MCP Hello World 服务器已启动");
process.on("uncaughtException", (error) => {
console.error("服务器启动失败", error);
process.exit(1);
});
//官方sdk代码
#!/usr/bin/env node
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
// 创建MCP服务器
const server = new McpServer({
name: "hello-world",
version: "1.0.0"
});
// 添加一个简单的打招呼工具
server.tool(
"say_hello",
{ name: z.string().describe("要问候的名字") },
async (params: { name: string }) => ({
content: [{ type: "text", text: `你好,${params.name}!欢迎使用MCP!` }]
})
);
// 添加一个加法工具
server.tool(
"add",
{
a: z.number().describe("第一个数字"),
b: z.number().describe("第二个数字")
},
async (params: { a: number, b: number }) => ({
content: [{ type: "text", text: `${params.a} + ${params.b} = ${params.a + params.b}` }]
})
);
// 启动服务器
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("MCP Hello World 服务器已启动");
}
main().catch((error) => {
console.error("服务器启动失败:", error);
process.exit(1);
});
导入需要的模块
创建LiteMCP服务器实例
添加两个工具:
say_hello
: 问候工具add
: 加法计算工具
启动服务器
添加错误处理
4. 更新package.json
确保package.json包含以下内容:
{
"name": "litemcp_demo",
"version": "1.0.0",
"description": "MCP Hello World Demo with LiteMCP",
"main": "dist/index.js",
"type": "module",
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "npx litemcp dev dist/index.js"
},
"keywords": [
"mcp",
"demo",
"litemcp"
],
"author": "",
"license": "ISC",
"dependencies": {
"litemcp": "^0.0.2",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/node": "^22.13.10",
"typescript": "^5.8.2"
}
}
添加
"type": "module"
启用ES模块添加npm脚本命令
确保依赖版本正确
5. 编译
编译后,dist
目录应包含以下文件:
index.js
: 编译后的JavaScript代码index.js.map
: 源码映射文件index.d.ts
: TypeScript类型声明文件
# 编译项目
npm run build
6.运行
使用
使用方式
//注意目录位置,你也可以使用绝对地址
npx @wong2/mcp-cli node ./dist/index.js args...
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 sky博客
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果