MCP协议服务器模板

一个基于Node.js和Fastify框架的MCP协议服务器模板,提供TypeScript支持、错误处理和会话管理功能,适用于构建符合MCP规范的服务器。

  • by karankraina
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 karankraina/mcp-fastify

使用说明

MCP快速服务器模板

一个强大的建筑模板 模型上下文协议(MCP) 使用Node.js和Fastify框架的服务器。此模板为创建具有TypeScript支持、正确错误处理和会话管理的MCP兼容服务器提供了坚实的基础。

🚀 特性

  • MCP协议合规性:完全支持模型上下文协议规范
  • Fastify框架:高性能、低开销的web框架
  • TypeScript支持:具有全面类型定义的类型安全开发

📋 先决条件

  • Node.js 22+
  • npm或yarn包管理器
  • TypeScript知识(推荐)

🛠️ 安装

  1. 克隆存储库

    git clone https://github.com/karankraina/mcp-fastify.git
    cd mcp-fastify
    
  2. 安装依赖项

    npm install
    
  3. 设置环境变量

    cp sample.env .env
    # Edit .env with your configuration
    
  4. 构建项目

    npm run build
    
  5. 启动服务器

    npm start
    

🏗️ 项目结构

mcp-fastify/
├── src/                    # Source TypeScript files
│   ├── server.ts          # Main server implementation
│   └── tools/             # MCP tools directory
│       ├── index.ts       # Tool registration hub
│       └── divide-numbers.ts # Example divide tool with error handling
├── build/                 # Compiled JavaScript output
├── tests/                 # Test files
├── package.json          # Project configuration
├── tsconfig.json         # TypeScript configuration
├── sample.env            # Environment variables template
└── README.md             # This file

🔧 配置

环境变量

变量 描述 默认值 必填
PORT 服务器端口号 3001 没有
ENVIRONMENT 运行时环境(development, production, test) production 没有

服务器配置

服务器根据环境支持不同的日志记录配置:

  • 发展:带有时间戳的漂亮打印日志
  • 生产:JSON结构化日志
  • 测试:已禁用日志记录

🛠️ 添加新工具

要添加新的MCP工具:

  1. 创建新的工具文件 在……里面 src/tools/:

    // src/tools/your-new-tool.ts
    import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
    import { z } from "zod";
    
    export function registerYourNewTool(server: McpServer) {
        server.registerTool(
            "your_tool_name",
            {
                title: "Your Tool Title",
                description: "Description of what your tool does",
                annotations: {
                    destructiveHint: false,
                    openWorldHint: false,
                },
                inputSchema: {
                    param1: z.string().describe("First parameter"),
                    param2: z.number().describe("Second parameter"),
                },
            },
            async ({ param1, param2 }) => {
                try {
                    // Your tool logic here
                    const result = `Processed: ${param1} with ${param2}`;
                    
                    return {
                        content: [{
                            type: "text",
                            text: result,
                        }]
                    };
                } catch (error) {
                    console.error('Error in your tool:', error);
                    return {
                        content: [{
                            type: "text",
                            text: `Error: ${error instanceof Error ? error.message : String(error)}`,
                        }]
                    };
                }
            }
        );
    }
    
  2. 注册该工具 在……里面 src/tools/index.ts:

    import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
    import { registerMultiplyNumbersTool } from "./multiply-numbers.js";
    import { registerYourNewTool } from "./your-new-tool.js";
    
    export async function registerTools(server: McpServer) {
        await registerMultiplyNumbersTool(server);
        await registerYourNewTool(server);
    }
    
  3. 重建和测试:

    npm run build
    npm start
    

🧪 测试

运行测试套件:

npm test

在监视模式下运行测试:

npm run test:watch

运行覆盖率测试:

npm run test:coverage

🤝 贡献

  1. 分叉存储库
  2. 创建要素分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

访问存储库:https://github.com/karankraina/mcp-fastify

📄 许可证

此项目根据ISC许可证获得许可-请参阅 许可证 文件以获取详细信息。

🔗 链接

🙏 致谢