Skip to main content
Glama

template-mcp

MCP (Model Context Protocol) 服务器模板,支持 TypeScript、Zod 验证和双重传输(stdio/HTTP)。兼容任何 MCP 客户端:Claude Code、Claude Desktop、Cursor、VS Code Copilot、Windsurf、Cline 等。

特性

  • 双重传输:stdio(本地)和 Streamable HTTP(远程)

  • TypeScript 严格模式,使用 ESM 模块

  • Zod 验证:用于工具输入模式

  • Joi 环境变量验证(启动时快速失败)

  • Pino 日志记录:输出到 stderr(stdio 安全)

  • 模块化架构:工具、资源和提示词作为独立模块

  • 工厂模式createServer() 以支持可测试性

  • 完整测试套件:使用 MCP SDK 内存传输

  • 高质量工具链:ESLint + Prettier + Husky + lint-staged

  • Docker 就绪:多阶段构建

  • CI/CD:GitHub Actions 流水线

Related MCP server: xmcp Application

快速开始

pnpm install
pnpm dev

脚本

脚本

描述

pnpm dev

启动并热重载 (tsx watch)

pnpm build

编译 TypeScript + 解析别名

pnpm start

运行编译后的服务器

pnpm test

运行测试

pnpm lint

代码检查

pnpm type-check

类型检查(不生成文件)

配置

.env.example 复制为 .env 并进行调整:

变量

默认值

描述

MCP_TRANSPORT

stdio

传输方式:stdiohttp

PORT

3000

HTTP 端口(仅用于 http 传输)

LOG_LEVEL

info

Pino 日志级别

NODE_ENV

development

环境

项目结构

src/
├── main.ts              # Entrypoint: transport selection
├── server.ts            # createServer() factory
├── config/              # Env validation + constants
├── common/              # Logger, error helpers, types
├── tools/               # MCP tools (callable by LLMs)
├── resources/           # MCP resources (read-only data)
└── prompts/             # MCP prompts (reusable templates)

添加新工具

  1. 创建 src/tools/my-tool.tool.ts

import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';

export function registerMyTool(server: McpServer): void {
  server.registerTool(
    'my_tool',
    {
      title: 'My Tool',
      description: 'What this tool does',
      inputSchema: {
        param: z.string().describe('Parameter description'),
      },
      annotations: {
        readOnlyHint: true,
        destructiveHint: false,
        idempotentHint: true,
        openWorldHint: false,
      },
    },
    async ({ param }) => ({
      content: [{ type: 'text', text: `Result: ${param}` }],
    }),
  );
}
  1. src/tools/index.ts 中注册:

import { registerMyTool } from './my-tool.tool.js';

export function registerTools(server: McpServer): void {
  registerGreetTool(server);
  registerMyTool(server); // add here
}
  1. src/tools/__tests__/my-tool.tool.spec.ts 中添加测试

客户端配置

Claude Code

添加到 .claude/settings.json

{
  "mcpServers": {
    "template-mcp": {
      "command": "node",
      "args": ["/absolute/path/to/template-mcp/dist/main.js"]
    }
  }
}

Claude Desktop

添加到 claude_desktop_config.json

{
  "mcpServers": {
    "template-mcp": {
      "command": "node",
      "args": ["/absolute/path/to/template-mcp/dist/main.js"]
    }
  }
}

Cursor

添加到 Cursor 设置 > MCP Servers:

{
  "mcpServers": {
    "template-mcp": {
      "command": "node",
      "args": ["/absolute/path/to/template-mcp/dist/main.js"]
    }
  }
}

VS Code (Copilot)

添加到 .vscode/settings.json

{
  "mcp": {
    "servers": {
      "template-mcp": {
        "command": "node",
        "args": ["/absolute/path/to/template-mcp/dist/main.js"]
      }
    }
  }
}

Docker

# Build
docker build -t template-mcp .

# Run (HTTP mode, used for remote access)
docker run -p 3000:3000 template-mcp

技术栈

  • Node.js 22 + TypeScript (严格模式, ESM)

  • MCP SDK v1 (@modelcontextprotocol/sdk)

  • Zod (工具输入验证)

  • Joi (环境变量验证)

  • Pino (stderr 日志)

  • Vitest (测试)

  • ESLint + Prettier + Husky


验证

以下所有内容均已验证并 100% 可用。

代码质量

检查

命令

Lint + 格式化

pnpm lint

严格类型检查

pnpm type-check

构建 (tsc + alias)

pnpm build

单元测试 (11/11)

pnpm test

套件

覆盖范围

greet.tool.spec.ts (5)

列表、休闲/正式/热情风格、拒绝空名称

server-info.resource.spec.ts (2)

列表、JSON 字段 (name, version, uptime, timestamp)

summarize.prompt.spec.ts (4)

列表、简要/要点风格、数字强制转换、默认值

无需网络或端口 — 使用 SDK 的 InMemoryTransport

运行时 — stdio 传输(默认模式)

echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0.0"}}}' \
  | MCP_TRANSPORT=stdio node dist/main.js

JSON-RPC 响应在 stdout,日志在 stderr。

运行时 — HTTP 传输

MCP_TRANSPORT=http PORT=3100 node dist/main.js &
# Initialize → capturar Mcp-Session-Id del header
# tools/list, resources/list, prompts/list, tools/call greet, resources/read info://server

已验证端点

预期结果

tools/call greet {"name":"Freddy","style":"casual"}

"Hey Freddy! How's it going?"

resources/read info://server

包含 name, version, uptime, nodeVersion, timestamp 的 JSON

Docker

docker build -t template-mcp .  # multi-stage: base → deps → build → production
docker run -p 3000:3000 template-mcp  # arranca en HTTP mode

CI (GitHub Actions)

pnpm installpnpm lintpnpm buildpnpm test 在每次推送到 main/master 分支或提交 PR 时运行。

提交流水线 (本地)

git commit → husky → lint-staged → eslint --fix + prettier --write (仅限暂存文件)

已知差距

  • HTTP 传输无自动化测试 (中):单元测试使用 InMemoryTransport;HTTP 传输 (StreamableHTTPServerTransport) 仅通过 curl 手动验证。对于远程生产环境,需添加真实会话的集成测试

  • MCP 客户端集成 (中):通过添加到 .claude/settings.json 或 Cursor 手动验证,并确认工具/资源/提示词出现在客户端中

  • 极端工具输入 (低):超长字符串、格式错误的 unicode — Zod 会拒绝它们,但错误响应未通过 HTTP 进行测试

  • 并发会话 (低):超出模板范围

Install Server
F
license - not found
A
quality
D
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Tools

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    A TypeScript-based template for rapidly developing MCP servers with modular tool architecture, built-in validation using Zod schemas, and comprehensive error handling.
    9
    MIT
  • F
    license
    Not graded
    quality
    D
    maintenance
    A Model Context Protocol (MCP) server template designed for building structured tools, prompts, and resources with built-in support for HTTP and STDIO transports. It provides a standardized framework for developers to create and deploy AI-driven services using TypeScript and Zod schema validation.
    9
  • A
    license
    Not graded
    quality
    D
    maintenance
    A minimal TypeScript MCP server template with example tool, Zod validation, stdio transport, and dotenv setup.
    78
    MIT

View all related MCP servers

Related MCP Connectors

  • A TypeScript MCP server for Home Assistant, enabling programmatic management of entities, automati…

  • A comprehensive Model Context Protocol (MCP) server that enables AI assistants to interact with yo…

  • MCP server for AI agents to plan, verify, and deploy Cloudflare-native apps.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Freddymhs/template-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server