Alpic MCP 模板
一个使用Streamable HTTP传输构建MCP服务器的TypeScript模板。
概述
此模板为创建能够与AI助手和其他MCP客户端通信的MCP服务器提供了基础。它包含了一个简单的HTTP服务器实现,以及示例工具、资源和提示,帮助您开始构建自己的MCP集成。
先决条件
- Node.js 22+(参见
.nvmrc(用于确切版本)
安装
- 克隆仓库:
git clone
cd mcp-server-template- 安装依赖项:
npm install- 创建环境文件:
cp .env.example .env用法
发展
启动开发服务器并启用热重载:
npm run dev服务器将在 http://localhost:3000 当你对源代码进行修改时,它会自动重启。
生产构建(或生产版本)
为生产构建项目:
npm run build编译后的JavaScript将输出到 dist/ 目录。
运行检查器
使用MCP检查器工具来测试您的服务器:
npm run inspectorAPI终端点
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 => {
// 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 => {
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Your prompt content with ${param}`,
},
},
],
};
},
);