MCP月相服务器
一个简单的MCP(模型上下文协议)服务器,根据日期和时间计算月相。
关于
特性
- 计算月球年龄(自上次新月以来的天数)
- 计算光照百分比(0-100%)
- 接受特定日期时间或使用当前时间
- 使用JSON-RPC格式通过stdio进行通信
- 提供用于发现的MCP工具定义
用法
运行服务器(开发模式):
go run .然后将服务器添加到您最喜欢的AI编码工具中。如果发生挤压,请参阅 crush.json 文件。
MCP工具发现
列出可用工具:
{"method": "tools/list", "id": 1}答复:
{
"result": {
"tools": [
{
"name": "moon_phase",
"description": "Calculate the phase of the moon for a given date and time, or current time if not specified. Returns moon age in days and illumination percentage.",
"inputSchema": {
"type": "object",
"properties": {
"datetime": {
"type": "string",
"description": "ISO 8601 datetime string (RFC3339 format). If not provided, uses current time.",
"format": "date-time"
}
},
"additionalProperties": false
}
}
]
},
"id": 1
}(注意:规范要求每个请求都携带一个唯一的ID。)
呼叫工具
调用moon_phase工具:
{"method": "tools/call", "params": {"name": "moon_phase", "arguments": {"datetime": "2024-01-01T12:00:00Z"}}, "id": 2}直接方法调用(传统)
获取当前月相:
{"method": "moon_phase", "id": 1}获取特定日期的月相:
{"method": "moon_phase", "params": {"datetime": "2024-01-01T12:00:00Z"}, "id": 2}示例响应
{"result": {"moon_age": 15.5, "illumination": 75}, "id": 1}测试
运行测试:
go test -v ./...实现细节
- 使用Julian day计算进行精确的月相计算
- 月球周期:29.53059天
- 日期时间格式:RFC3339(ISO 8601)
- 错误代码遵循JSON-RPC 2.0规范
- 支持MCP工具协议和直接方法调用
