使用说明
MCP TypeScript模板
一个TypeScript模板,用于使用现代工具和最佳实践构建远程模型上下文协议(MCP)服务器,同时利用 MCP TypeScript SDK.
特性
此模板提供:
- TypeScript -完全支持TypeScript,配置严格
- 维特 -具有ES模块输出的快速构建系统
- 快速 -用于HTTP服务器的快速、无异议的web框架
- ESLint+Pretier -代码质量和格式
- 码头工人 -集装箱化支持
- 示例工具 -演示MCP工具实现的简单回声工具
入门指南
最简单的入门方法是使用 degit:
-
基于此模板创建新项目
npx degit nickytonline/mcp-typescript-template my-mcp-server cd my-mcp-server -
安装依赖项
npm install -
构建项目
npm run build -
启动服务器
npm start
服务器将在以下时间可用 http://localhost:3000 用于MCP连接。
替代方案:使用GitHub模板
您还可以单击GitHub上的“使用此模板”按钮创建新的存储库,然后克隆它:
git clone <your-repo-url>
cd my-mcp-server
npm install
发展
开发监视模式(带热重新加载)
npm run dev
构建项目
npm run build
代码检查
- 完成项目
npm run lint
- 修复所有可自动修复的棉绒错误
npm run lint:fix
格式化
- 设置项目中的文件格式
npm run format
- 检查格式
npm run format:check
测试您的MCP服务器
您可以使用以下工具测试您的MCP服务器 MCP检查员:
npx @modelcontextprotocol/inspector
这将启动一个web界面,允许您:
- 连接到您的MCP服务器
- 以交互方式测试您的工具
- 查看请求/响应消息
- 调试您的MCP实现
确保您的服务器正在运行(使用 npm start 或 npm run dev)在与检查员联系之前。
可用工具
该模板包括一个示例工具:
回声
返回提供的消息——一个演示MCP工具实现的简单示例。
参数:
message(string)-要回显的消息
自定义您的MCP服务器
- 更新package.json -更改名称、描述和关键字
- 修改src/index.ts -用自定义工具替换回显工具
- 添加您的逻辑 -为您的业务逻辑创建其他TypeScript文件
- 更新README -记录您的特定MCP服务器功能
码头工人
使用Docker构建和运行:
- 构建Docker镜像
docker build -t my-mcp-server .
- 运行容器
docker run -p 3000:3000 my-mcp-server
Docker Compose
# docker-compose.yml
version: "3.8"
services:
mcp-server:
build: .
ports:
- "3000:3000"
environment:
- PORT=3000
docker-compose up --build
项目结构
mcp-typescript-template/
├── src/
│ └── index.ts # Main MCP server entry point
├── dist/ # Built output (generated)
├── .eslintrc.js # ESLint configuration
├── .prettierrc # Prettier configuration
├── tsconfig.json # TypeScript configuration
├── vite.config.ts # Vite build configuration
├── Dockerfile # Docker configuration
└── package.json # Dependencies and scripts
建筑
此模板遵循一个简单的架构:
- HTTP传输 -使用Express和StreamableHTTPServerTransport进行远程MCP连接
- 工具注册 -使用JSON模式注册工具以进行输入验证
- 错误处理 -正确的MCP格式错误响应
- 会话管理 -处理MCP会话初始化和管理
示例:添加新工具
import { createTextResult } from "./lib/utils.js";
server.registerTool(
"my_tool",
{
title: "My Custom Tool",
description: "Description of what this tool does",
inputSchema: {
param1: z.string().describe("Description of param1"),
param2: z.number().optional().describe("Optional parameter"),
},
},
async (args) => {
// Your tool logic here
const result = await myCustomLogic(args.param1, args.param2);
return createTextResult(result);
},
);
为什么选择Express?
此模板使用Express作为HTTP服务器,它提供:
- MCP SDK兼容性 -与MCP TypeScript SDK的StreamableHTTPServerTransport完全兼容
- 成熟稳定 -久经考验的HTTP服务器,拥有广泛的生态系统
- TypeScript支持 -出色的TypeScript支持,具有全面的类型定义
- 中间件生态系统 -丰富的通用任务中间件生态系统
- 文档 -全面的文档和社区支持
- 可靠性 -经过验证的生产应用可靠性
存储库指南
贡献者应审查 AGENTS.md 在打开更改之前,了解项目结构、编码标准和拉取请求期望。