Token导航 LogoToken导航TokenDH.com
MCP Template New logo
AI代理未说明官方级别未说明来源级核验

MCP Template New

MCP Server

一个基于Node.js和TypeScript构建的模型上下文协议(MCP)服务器模板,支持标准I/O和流式HTTP传输,适用于客户端通信和工具调用。

工具数

2

提示词数

0

GitHub Stars

0

资源数

0
AI代理JavaScriptClaudeTypeScriptClaude DesktopClaude

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

lbovboe

提供方

lbovboe

最后核验

2026/5/17 20:22

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

MCP模板服务器

使用Node.js和TypeScript构建的模型上下文协议(MCP)服务器模板。

特性

  • 工具:

- get_time:获取任何时区的当前时间 - echo:回显消息

  • 资源:服务器信息资源
  • 双重运输:支持stdio和流式HTTP(单 /mcp 端点)

安装

npm install

构建

npm run build

用法

开发模式(含tsx)

标准模式 (适用于Claude Desktop等MCP客户):

npm run dev:mcp

HTTP模式 (对于基于HTTP的客户端,使用Streamable HTTP传输):

npm run dev:mcp:http
# Server runs on http://localhost:3001

# Custom port:
npm run dev:mcp:http:port 3002

生产模式

标准模式:

npm run start:mcp

HTTP模式:

npm run start:mcp:http
# Or with custom port:
npm run start:mcp:http:port 3002

Claude桌面配置

添加到您的Claude Desktop配置文件中:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "mcp-template": {
      "command": "node",
      "args": ["/path/to/mcp-template/build/index.js"]
    }
  }
}

或者使用tsx进行开发:

{
  "mcpServers": {
    "mcp-template": {
      "command": "npx",
      "args": ["-y", "tsx", "/path/to/mcp-template/src/index.ts"]
    }
  }
}

使用HTTP模式(流式HTTP)进行测试

新的Streamable HTTP传输使用 /mcp 端点 用于所有通信(POST、GET、DELETE方法)。

与旧SSE运输的主要区别

旧(已弃用)苏格兰和南方能源公司运输:

  • 两个端点: /sse (GET用于流媒体)和 /message (POST用于请求)
  • 会话ID作为查询参数传递

新的流式HTTP传输:

  • 单端点: /mcp
  • POST/mcp:客户端发送请求
  • GET/mcp:客户端为服务器发起的通知建立SSE流
  • DELETE/mcp:客户端终止会话
  • 传入会话ID Mcp-Session-Id 头球

测试步骤

  1. 启动服务器:
npm run dev:mcp:http
  1. 测试运行状况端点:
curl http://localhost:3001/health
  1. 初始化会话(必须初始化第一个请求):

选项A:使用 -i 标志(包括标头、更清晰的输出):

curl -i -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize",
    "params": {
      "protocolVersion": "2024-11-05",
      "capabilities": {},
      "clientInfo": {
        "name": "test-client",
        "version": "1.0.0"
      }
    }
  }'

选项B:使用 -v flag(详细,显示完整的请求和响应):

curl -v -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize",
    "params": {
      "protocolVersion": "2024-11-05",
      "capabilities": {},
      "clientInfo": {
        "name": "test-client",
        "version": "1.0.0"
      }
    }
  }'

重要提示:

  • -i 标志仅显示响应标头(更清晰)
  • -v 标志显示请求和响应标头(详细,用于调试)
  • Accept 标题必须同时包含这两个 application/jsontext/event-stream 正确支持Streamable HTTP协议
  • 寻找 mcp-session-id 响应中的报头(例如。, mcp-session-id: e75aa1e2-8327-47fe-b670-a49f6a694e64)
  • 复制此会话ID以在后续请求中使用
  1. 列出工具(替换 `` 使用步骤3中的值):
curl -i -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -H "Mcp-Session-Id: " \
  -d '{
    "jsonrpc": "2.0",
    "id": 2,
    "method": "tools/list"
  }'

实际会话ID示例:

curl -i -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -H "Mcp-Session-Id: e75aa1e2-8327-47fe-b670-a49f6a694e64" \
  -d '{
    "jsonrpc": "2.0",
    "id": 2,
    "method": "tools/list"
  }'
  1. 调用工具:
curl -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -H "Mcp-Session-Id: " \
  -d '{
    "jsonrpc": "2.0",
    "id": 3,
    "method": "tools/call",
    "params": {
      "name": "echo",
      "arguments": {
        "message": "Hello from Streamable HTTP!"
      }
    }
  }'
  1. 建立SSE流(可选,用于服务器发起的通知):
curl -X GET http://localhost:3001/mcp \
  -H "Mcp-Session-Id: " \
  -H "Accept: text/event-stream"
  1. 终止会话:
curl -X DELETE http://localhost:3001/mcp \
  -H "Mcp-Session-Id: "

从SSE运输迁移

此模板已更新为使用新 可流式HTTP传输 而不是弃用 SSEServerTransport.

发生了什么变化?

之前(弃用的SSE运输):

  • 用过的 SSEServerTransport@modelcontextprotocol/sdk/server/sse.js
  • 需要两个端点:

- GET /sse -用于服务器到客户端流式传输 - POST /message?sessionId= -用于客户端到服务器的消息

  • 会话ID作为查询参数传递

之后(新的流式HTTP传输):

  • 用途 StreamableHTTPServerTransport@modelcontextprotocol/sdk/server/streamableHttp.js
  • 单端点 /mcp 有三种HTTP方法:

- POST /mcp -对于所有客户端到服务器的消息 - GET /mcp -用于服务器到客户端的SSE流(可选) - DELETE /mcp -用于会话终止

  • 传入会话ID Mcp-Session-Id 头球
  • 需要 Accept: application/json, text/event-stream 头球

流式HTTP的好处

  1. 简化架构:单个端点而不是两个端点
  2. 更好的可扩展性:更易于负载平衡和管理
  3. 标准符合性:遵循HTTP最佳实践
  4. 面向未来:新的MCP规范标准(截至2025年3月26日)

项目结构

该项目遵循模块化架构,以实现更好的可维护性和可扩展性。

备注:原始单片版本(重构前)保留在 reference/index.ts 以供参考。
mcp-template/
├── src/
│   ├── index.ts              # Main entry point
│   ├── config/               # Configuration
│   │   └── index.ts         # Server config and argument parsing
│   ├── types/               # TypeScript type definitions
│   │   └── index.ts         # Shared types
│   ├── schemas/             # Zod validation schemas
│   │   └── tool-schemas.ts  # Tool input validation
│   ├── tools/               # Tool implementations
│   │   ├── index.ts         # Tool registry and exports
│   │   ├── get-time.ts      # Get time tool
│   │   └── echo.ts          # Echo tool
│   ├── resources/           # Resource implementations
│   │   ├── index.ts         # Resource registry
│   │   └── info.ts          # Server info resource
│   ├── server/              # MCP server setup
│   │   └── mcp-server.ts    # Server creation and handlers
│   └── transports/          # Transport layer
│       ├── stdio.ts         # Standard I/O transport
│       └── http.ts          # HTTP/SSE transport
├── build/                    # Compiled JavaScript (after build)
├── package.json
├── tsconfig.json
├── README.md
└── ARCHITECTURE.md           # Detailed architecture documentation

定制

添加新工具

  1. 创建架构src/schemas/tool-schemas.ts:
export const MyToolSchema = z.object({
  param: z.string().describe("Parameter description"),
});
  1. 创建工具src/tools/my-tool.ts:
import { Tool } from "./index.js";
import { MyToolSchema } from "../schemas/tool-schemas.js";

export const myTool: Tool = {
  name: "my_tool",
  description: "Tool description",
  inputSchema: { /* ... */ },
  handler: async (args: any) => {
    const parsed = MyToolSchema.safeParse(args);
    // ... implement logic
    return { content: [{ type: "text", text: result }] };
  },
};
  1. 出口src/tools/index.ts:
export { myTool } from "./my-tool.js";
  1. 注册src/index.ts:
const server = createMCPServer(
  serverConfig,
  [getTimeTool, echoTool, myTool], // Add here
  [infoResource]
);

添加新资源

遵循与工具相同的模式,但 src/resources/ 目录。

有关详细的体系结构信息和扩展模式,请参见 建筑.md.

许可证

国际学生中心

目录标签

目录标签

AI代理JavaScriptClaudeTypeScript协议服务器本地部署Node.js流式传输工具调用

支持客户端

Claude DesktopClaude

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

session

工具数量(toolCount,工具数)

2

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明session部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP