TypeScript中的MCP时间服务器
MCP(模型上下文协议)服务器的TypeScript实现,提供与时间相关的工具。
特性
- 获取当前时间:获取指定时区的当前时间
- 转换时间:使用时差计算在不同时区之间转换时间
- 模块化架构:将服务器创建和传输绑定分开,以实现灵活性
- 多种运输方式:支持stdio和HTTP传输
- Docker支持:易于集装箱化部署
需求
- Node.js 18+
- npm或纱线
- Docker(可选,用于容器化部署)
安装
# Clone the repository
git clone
cd mcp-time-server
# Install dependencies
npm install运行服务器
使用stdio传输(默认)
# Build the project
npm run build
# Start with stdio transport
npm start使用HTTP传输
# Build the project
npm run build
# Start with HTTP transport on default port 3000
node dist/index.js --transport=http
# Start with HTTP transport on custom port
node dist/index.js --transport=http --port=8080发展模式
npm run devDocker部署
使用Docker构建和运行
# Build the Docker image
docker build -t mcp-time-server .
# Run with stdio transport (default)
docker run --name mcp-time-stdio mcp-time-server
# Run with HTTP transport
docker run --name mcp-time-http -p 3000:3000 mcp-time-server node dist/index.js --transport=http使用Docker Compose
# Start the HTTP version
docker-compose up mcp-time-http
# Start the stdio version
docker-compose up mcp-time-stdio
# Start both services
docker-compose upHTTP传输端点
使用HTTP传输时,以下端点可用:
- POST/mcp:主MCP协议端点
- GET/状态:返回服务器状态信息
要测试HTTP传输:
- 使用启动服务器
--transport=http - 使用配置为连接到的MCP客户端
http://localhost:3000/mcp
项目结构
该项目遵循模块化架构,关注点明确分离:
src/
├── index.ts # Entry point and command-line handling
├── server.ts # MCP server creation and configuration
├── timeService.ts # Time-related utilities and calculations
├── types.ts # Shared types and interfaces
└── transports/
├── index.ts # Transport exports and utilities
├── stdio.ts # Standard I/O transport implementation
└── http.ts # HTTP transport implementation模块
- timeService.ts:时间操作的核心业务逻辑
- server.ts:使用工具创建和配置MCP服务器
- 运输工具/:包含不同的传输实现
- types.ts:共享类型定义
- index.ts:主要应用程序入口点
这种模块化设计使代码库更易于维护,并有助于:
- 添加新的运输类型
- 对单个组件进行单元测试
- 使用新的时间相关工具扩展功能
工具
1. get_current_time
获取指定时区的当前时间。
输入架构:
{
"timezone": "IANA timezone name (e.g., 'America/New_York', 'Europe/London')"
}结果:
{
"timezone": "America/New_York",
"datetime": "2023-06-01T12:34:56-04:00",
"is_dst": true
}2. convert_time
在不同时区之间转换时间。
输入架构:
{
"source_timezone": "IANA timezone name (e.g., 'America/New_York')",
"time": "14:30",
"target_timezone": "IANA timezone name (e.g., 'Asia/Tokyo')"
}结果:
{
"source": {
"timezone": "America/New_York",
"datetime": "2023-06-01T14:30:00-04:00",
"is_dst": true
},
"target": {
"timezone": "Asia/Tokyo",
"datetime": "2023-06-02T03:30:00+09:00",
"is_dst": false
},
"time_difference": "+13.0h"
}实现细节
此服务器是使用以下方式构建的:
- TypeScript
- MCP TypeScript SDK
- 用于HTTP传输的Express
- Luxon用于时区处理
- Zod用于模式验证
许可证
国际协调委员会
