article
README
🚀 @m 服务器端口
这是一个使用 TypeScript 和 FastMCP 搭建的服务器项目,旨在实现模型上下文协议 (Model Context Protocol, MCP)。该服务器支持多种功能模块,可通过 CLI 工具进行测试和 inspect 操作。
🚀 快速开始
安装依赖
npm install fastmcp @types/zod
启动服务器
npm run dev
✨ 主要特性
- 使用 FastMCP 实现 MCP 协议。
- 支持标准的 CLI 工具 (
fastmcp和fastmcp-inspect) 进行测试和 inspect 操作。 - 提供灵活的配置选项,支持通过环境变量自定义服务器行为。
📦 安装指南
初始化项目
npx @m/server init
或者使用 npm:
npm create @m/server
💻 使用示例
基础用法
# 使用 fastmcp 启动测试服务器
npx fastmcp dev server.js
# 使用 fastmcp-inspect 检查服务器状态
npx fastmcp inspect server.ts
高级用法
import { FastMCP } from "fastmcp";
const server = new FastMCP();
// 添加自定义工具
server.addTool({
name: "hello_world",
description: "A simple hello world tool",
parameters: z.object({
name: z.string().describe("Name to greet")
}),
execute: async (params) => {
return `Hello, ${params.name}!`;
}
});
// 添加自定义资源模板
server.addResourceTemplate({
uriTemplate: "example://{id}",
name: "Example Resource",
mimeType: "text/plain",
arguments: [
{
name: "id",
description: "Resource ID",
required: true,
},
],
async load({ id }) {
return {
text: `This is an example resource with ID: ${id}`
};
}
});
// 添加自定义提示
server.addPrompt({
name: "greeting",
description: "A simple greeting prompt",
arguments: [
{
name: "name",
description: "Name to greet",
required: true,
},
],
load: async ({ name }) => {
return `Hello, ${name}! How can I help you today?`;
}
});
// 启动服务器
server.start();
环境变量配置
# 修改 HTTP 端口(默认为 3001)
PORT=8080 npm run start
# 修改绑定地址(默认为 0.0.0.0)
HOST=127.0.0.1 npm run start
📚 详细文档
项目内容
- 基础设置:实现了 MCP 协议的基本功能,包括工具、资源和提示的注册与管理。
- 工具模块:提供了自定义工具的接口,可扩展实现多种服务器功能。
- 资源模板:支持基于 URI 模板的资源加载,便于动态生成内容。
- 提示系统:实现了交互式提示功能,可与客户端进行自然语言对话。
文档资料
- FastMCP 官方文档:FastMCP GitHub 仓库
- 模型上下文协议 (MCP):MCP 官方文档
📄 许可证
本项目在 MIT 许可证下发布 - 有关详细信息,请参阅 LICENSE 文件。
微信扫一扫