MCP服务器模板

一个用于构建基于TypeScript的Model Context Protocol (MCP)服务器的全面模板,提供类型安全、示例实现、测试配置和文档支持。

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

使用说明

MCP服务器模板

一个全面的建筑模板 模型上下文协议(MCP) 使用TypeScript的服务器。

特性

  • ✅ TypeScript -完全类型安全和现代JavaScript功能
  • ✅ 示例实现 -工具、资源和提示示例
  • ✅ 综合文档 -CLAUDE代码的详细CLAUDE.md
  • ✅ 测试设置 -Vitest配置和示例测试
  • ✅ ESLint配置 -代码质量和一致性
  • ✅ 生产就绪 -内置错误处理和日志记录

快速开始

# 1. Use this template
Click "Use this template" button on GitHub

# 2. Clone your new repository
git clone https://github.com/yourusername/your-mcp-server.git
cd your-mcp-server

# 3. Install dependencies
npm install

# 4. Build the project
npm run build

# 5. Test with MCP Inspector
npx @modelcontextprotocol/inspector dist/index.js

包含什么

示例实现

此模板包括以下工作示例:

  1. 工具 -Claude可以调用的函数

    • example_tool -处理和转换文本
  2. 资源 -克劳德可以访问的数据

    • example://resource -JSON数据资源
  3. 提示 -可重复使用的提示模板

    • example_prompt -基于主题的提示生成

项目结构

mcp-server-template/
├── src/
│   ├── index.ts              # Main server entry point
│   ├── tools/                # Tool implementations
│   │   └── example.ts
│   ├── resources/            # Resource implementations
│   │   └── example.ts
│   └── prompts/              # Prompt implementations
│       └── example.ts
├── dist/                     # Compiled output (generated)
├── package.json
├── tsconfig.json
├── CLAUDE.md                 # Comprehensive Claude Code documentation
└── README.md                 # This file

安装

作为本地开发服务器

npm run build
claude mcp add my-server node /absolute/path/to/dist/index.js

发布到npm后

npm publish
claude mcp add my-server npx -y your-package-name

发展

npm install          # Install dependencies
npm run build        # Build TypeScript
npm run dev          # Watch mode for development
npm test             # Run tests
npm run lint         # Lint code
npm run lint:fix     # Fix linting issues

使用Claude代码

安装后,您的MCP服务器将在Claude Code中可用:

# Claude can now use your tools
User: "Use the example_tool to process 'hello world'"

# Claude can access your resources
User: "Read the example resource"

# Claude can use your prompts
User: "Use the example_prompt for 'MCP servers'"

定制

1.更新包元数据

编辑 package.json:

{
  "name": "your-mcp-server",
  "version": "1.0.0",
  "description": "Your server description",
  "author": "Your Name"
}

2.添加您的工具

在中创建新的工具文件 src/tools/:

// src/tools/myTool.ts
export async function myTool(args: { input: string }) {
  // Your implementation
  return {
    content: [
      {
        type: "text",
        text: `Result: ${args.input}`,
      },
    ],
  };
}

注册 src/index.ts:

import { myTool } from "./tools/myTool.js";

// Add to tools list and call handler

3.添加您的资源

在中创建资源文件 src/resources/ 遵循相同的模式。

4.添加您的提示

在中创建提示文件 src/prompts/ 遵循相同的模式。

文档

使用Claude Code的所有文档都在 CLAUDE.md:

  • 核心概念(工具、资源、提示)
  • 带有分步说明的开发指南
  • 测试策略
  • 部署程序
  • 故障排除指南
  • 最佳实践

用例

此模板非常适合构建:

  • 🔧 API集成 -将Claude连接到外部API
  • 📁 文件系统工具 -自定义文件操作
  • 🗄️ 数据库访问 -查询和管理数据库
  • 🌐 网络爬虫 -获取和处理web内容
  • 📊 数据处理器 -转换和分析数据
  • 🤖 自动化工具 -自动化工作流程

测试

MCP检查员进行本地测试

npm run build
npx @modelcontextprotocol/inspector dist/index.js

检查器为测试工具、资源和提示提供了一个交互式UI。

单元测试

npm test                    # Run all tests
npm test -- --watch        # Watch mode
npm test -- --coverage     # Coverage report

技术

  • TypeScript 5.3.3
  • MCP-SDK 1.0.4
  • Vitest 1.1.0(测试)
  • 埃斯林特 8.56.0(棉绒)

资源

许可证

麻省理工学院

贡献

  1. 分叉存储库
  2. 创建功能分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

模板信息

版本: 1.0.0 最后更新时间: 2025 模板用途: 构建模型上下文协议(MCP)服务器


⭐ 如果你觉得这个模板有用,就把它标上!

📝 使用CLAUDE Code时,请查看CLAUDE.md以获取全面的文档