MCP服务器实现

一个使用TypeScript和官方MCP SDK实现的简单MCP服务器,提供工具和资源管理功能,适用于AI助手集成和开发测试。

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

使用说明

简单MCP服务器

使用TypeScript和官方MCP SDK的简单模型上下文协议(MCP)服务器实现。

特性

此MCP服务器提供:

工具

  • 回声:回显输入文本
  • add_numbers:将两个数字相加
  • get_current时间:返回当前日期和时间
  • get_light_status:使用航班号获取实时航班状态(支持模拟和真实API数据)

资源

  • info://server:服务器信息和功能
  • greeting://hello:一条简单的问候语

安装

  1. 安装依赖项:
npm install
  1. 构建项目:
npm run build

用法

发展模式

以热重新加载的开发模式运行服务器:

npm run dev

生产模式

构建并运行编译后的服务器:

npm run build
npm start

观看模式

构建并观察变化:

npm run watch

MCP客户端集成

要将此服务器与MCP客户端一起使用,您需要配置客户端以连接到此服务器。服务器使用stdio传输,因此可以作为子进程启动。

MCP客户端的示例配置:

{
  "mcpServers": {
    "simple-server": {
      "command": "node",
      "args": ["path/to/dist/index.js"]
    }
  }
}

项目结构

├── examples/                   # Example implementations
│   ├── resources/
│   │   └── example-resource.ts # Example resource implementations
│   └── tools/
│       └── example-tool.ts     # Example tool implementations
├── src/                        # Source code
│   ├── handlers/
│   │   └── index.ts            # MCP request handlers
│   ├── resources/
│   │   ├── index.ts            # Resource implementations
│   │   └── registry.ts         # Resource registry
│   ├── tools/
│   │   ├── index.ts            # Tool implementations
│   │   └── registry.ts         # Tool registry
│   ├── types/
│   │   └── index.ts            # TypeScript type definitions
│   ├── utils/
│   │   └── config.ts           # Configuration and utilities
│   ├── index.ts                # Main entry point
│   └── server.ts               # Core server implementation
├── dist/                       # Compiled JavaScript (generated)
├── package.json                # Node.js dependencies and scripts
├── tsconfig.json               # TypeScript configuration
├── test.js                     # Basic test script
├── test-comprehensive.js       # Comprehensive test script
└── README.md                   # This file

模块化架构

服务器采用模块化架构构建,易于扩展:

  • 工具:每个工具都是一个单独的类,实现 ToolHandler 接口
  • 资源:每个资源都是一个单独的类,实现 ResourceHandler 接口
  • 注册处:中央登记处管理所有工具和资源
  • 处理器:请求处理程序管理MCP协议通信
  • 类型:用于类型安全的共享TypeScript接口

发展

添加新工具

  1. 创建一个新的工具类来实现 ToolHandler 在……里面 src/tools/
  2. 将工具添加到 ToolRegistry 在……里面 src/tools/registry.ts
  3. 看 examples/tools/example-tool.ts 完整示例

添加新资源

  1. 创建一个新的资源类来实现 ResourceHandler 在……里面 src/resources/
  2. 将资源添加到 ResourceRegistry 在……里面 src/resources/registry.ts
  3. 看 examples/resources/example-resource.ts 完整示例

航班状态工具

航班状态工具提供实时航班信息:

特征:

  • 实时航班状态(计划、活动、降落、取消、延误)
  • 出发和到达机场信息
  • 航空公司详细信息
  • 预定时间
  • 国际航班支援

用途:

# Test the flight status tool
node test-flight-status.js

API集成: 默认情况下,该工具使用模拟数据。要获取真实的飞行数据:

  1. 在上注册免费API密钥 AviationStack
  2. 设置环境变量: AVIATIONSTACK_API_KEY=your_api_key
  3. 重建并重新启动服务器

测试

运行测试脚本以验证功能:

# Basic test
node test.js

# Comprehensive test with all features
node test-comprehensive.js

# Test flight status tool specifically
node test-flight-status.js

Git配置

该项目包括一个全面的 .gitignore 文件不包括:

  • 环境文件(.env*)包含API密钥
  • 编译输出(dist/)
  • 依赖关系(node_modules/)
  • IDE/编辑器文件
  • 操作系统特定文件
  • 日志和临时文件

重要安全提示:API密钥和机密自动从git跟踪中排除。

看 GITIGNORE_INFO.md 有关被忽略文件的详细信息。

MCP协议

该服务器实现了模型上下文协议(MCP),允许AI助手安全地访问外部工具和数据源。服务器通过stdio上的JSON-RPC进行通信。

有关MCP的更多信息,请访问:https://modelcontextprotocol.io/