MCP服务器模板

一个用TypeScript编写的最小化Model Context Protocol (MCP)服务器模板,用于快速构建自定义MCP服务器,包含演示工具和集成测试套件。

  • by bitdozer
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 bitdozer/mcp-server-template

使用说明

MCP服务器模板

最低限度 模型上下文协议 (MCP)用TypeScript编写的服务器模板。

将此作为构建自己的MCP服务器的起点。它附带了一个 hello_world 演示工具和功能齐全的集成测试工具。

特性

  • 准备就绪的结构 –一个用于构建和测试的命令
  • 演示工具 – hello_world 显示完整的请求/响应周期
  • 类型安全 –TypeScript贯穿始终 佐德 输入验证
  • 集成测试 –Vitest+Execa测试套件,与真实服务器进程对话

可用工具

hello_world

致以友好的问候。用你自己的工具替换或扩展它。

输入 (全部可选):

字段 类型 描述
name string 问候语中应包含的姓名

示例调用:

{
  "tool": "hello_world",
  "arguments": {
    "name": "Alice"
  }
}

使用PowerShell

echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"hello_world","arguments":{"name":"Alice"}}}' | node build/server.js 

使用Git Bash(显式调用node.exe以避免节点抖动)

printf '%s\n%s\n%s\n' \
  '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' \
  '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' \
  '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"hello_world","arguments":{"name":"Alice"}}}' \
  | node.exe build/server.js
  
# or single command (note \n in string)
printf '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"hello_world","arguments":{"name":"Alice"}}}\n' | node.exe build/server.js

答复:

Hello, Alice! Welcome to the MCP Server Template.

入门指南

1.安装依赖项

npm install

2.建造

npm run build

这将TypeScript编译为 build/server.js.

3.运行测试

npm test

4.手动启动服务器

node build/server.js

服务器通过以下方式进行通信 stdio 使用MCP JSON-RPC协议。

添加您自己的工具

  1. 定义Zod模式 用于工具的输入 src/server.ts.
  2. 注册该工具 在 ListToolsRequestSchema 处理程序。
  3. 添加案例 在 CallToolRequestSchema switch语句。
  4. 添加集成测试 在 tests/integration/integration.test.ts.

连接到MCP客户端

GitHub Copilot聊天(VS代码)

添加到您的VS代码设置(.vscode/mcp.json):

{
  "servers": {
    "hello_world": {
      "command": "node",
      "args": [
        "absolute/path/to/build/server.js"
      ],
      "env": {
        "NODE_ENV": "production"
      }
    }
  }
}

临床扩展

添加到cline_mcp_settings.json作为STDIO传输本地服务器:

{
  "mcpServers": {
    "hello_world": {
      "command": "node",
      "args": ["/absolute/path/to/build/server.js"]
    }
  }
}

克劳德代码

# Project Scope (run in project folder, recommended)
claude mcp add --transport stdio hello_world "node.exe /absolute/path/to/build/server.js"
# or
# User Scope (always available, always consumes context)
claude mcp add --transport stdio --scope user hello_world "node.exe /absolute/path/to/build/server.js"

手册在.claude.json中

  "mcpServers": {
    "hello_world": {
      "type": "stdio",
      "command": "node.exe",
      "args": [
        "absolute\\path\\to\\build\\server.js"
      ],
      "env": {}
    }
  }

更改配置后重新启动客户端。

脚本

命令 描述
npm run build 编译TypeScript
npm run dev 构建并运行服务器
npm run watch 监视模式(保存后重建)
npm test 构建并运行所有测试
npm run test:integration 仅构建+运行集成测试
npm run test:watch 在监视模式下运行测试
npm run test:coverage 生成覆盖率报告

项目结构

src/
  server.ts              # MCP server – tools live here
tests/
  integration/
    integration.test.ts  # Integration tests
  helpers/
    mcp_client.ts        # JSON-RPC client for tests
    test_db.ts           # Temp-file utilities for tests
build/                   # Compiled output (git-ignored)

资源

许可证

麻省理工学院