MCP服务器模板

一个基于TypeScript的MCP服务器模板,用于构建可扩展的AI助手通信服务。

  • by neil-ac
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 neil-ac/node-email-mcp

使用说明

Alpic MCP 模板

一个使用Streamable HTTP传输构建MCP服务器的TypeScript模板。

概述

此模板为创建能够与AI助手和其他MCP客户端通信的MCP服务器提供了基础。它包含了一个简单的HTTP服务器实现,以及示例工具、资源和提示,帮助您开始构建自己的MCP集成。

先决条件

  • Node.js 22+(参见 .nvmrc (用于确切版本)

安装

  1. 克隆仓库:
git clone <repository-url>
cd mcp-server-template
  1. 安装依赖项:
npm install
  1. 创建环境文件:
cp .env.example .env

用法

发展

启动开发服务器并启用热重载:

npm run dev

服务器将在 http://localhost:3000 当你对源代码进行修改时,它会自动重启。

生产构建(或生产版本)

为生产构建项目:

npm run build

编译后的JavaScript将输出到 dist/ 目录。

运行检查器

使用MCP检查器工具来测试您的服务器:

npm run inspector

API终端点

  • POST /mcp - 主MCP通信端点
  • GET /mcp - 返回“方法不允许”(405)
  • DELETE /mcp - 返回“方法不允许”(405)

发展

添加新工具

要添加新工具,请修改 src/server.ts:

server.tool(
  "tool-name",
  "Tool description",
  {
    // Define your parameters using Zod schemas
    param: z.string().describe("Parameter description"),
  },
  async ({ param }): Promise<CallToolResult> => {
    // Your tool implementation
    return {
      content: [
        {
          type: "text",
          text: `Result: ${param}`,
        },
      ],
    };
  },
);

添加新提示

要添加一个新的提示模板,请修改 src/server.ts:

server.prompt(
  "prompt-name",
  "Prompt description",
  {
    // Define your parameters using Zod schemas
    param: z.string().describe("Parameter description"),
  },
  async ({ param }): Promise<GetPromptResult> => {
    return {
      messages: [
        {
          role: "user",
          content: {
            type: "text",
            text: `Your prompt content with ${param}`,
          },
        },
      ],
    };
  },
);

资源