MCP日期时间服务器
一个简单、清晰的MCP(模型上下文协议)服务器,提供准确的日期和时间信息,帮助LLM避免生成不正确的日期和时刻。
问题陈述
大型语言模型(LLM)通常会生成不正确或过时的日期和时间,因为它们:
- 有一个固定的知识截止日期
- 推理过程中不知道当前日期/时间
- 可能计算日期差异或日期算术错误
这个MCP服务器通过提供LLM可以用来获取准确、实时日期和时间信息的工具来解决这个问题。
特性
此服务器提供 5个简单、记录良好的工具:
- 获取当前日期时间 -获取当前日期和时间(当你需要知道“现在”时,一定要使用它)
- 解析日期 -解析并验证任何日期字符串
- 格式化日期 -在不同格式之间转换日期
- 计算日期差异 -计算两个日期之间的差异
- 为日期添加时间 -在日期中增减时间
每个工具都返回结构化、一致的JSON,便于LLM理解和使用。
安装
快速入门(预构建捆绑包)
使用此服务器的最简单方法是使用预构建的Node.js包:
# 1. Clone and build
git clone
cd mcp-date-time
npm install
npm run build
# 2. Add to Claude Code (using bundled version)
claude mcp add --transport stdio mcp-date-time -- node /path/to/mcp-date-time/dist/index.js
# Or use the standalone bundle (no dependencies needed)
claude mcp add --transport stdio mcp-date-time -- node /path/to/mcp-date-time/dist/index.standalone.js与Claude Desktop一起使用
添加到您的 claude_desktop_config.json:
选项1:常规捆绑包(需要npm安装)
{
"mcpServers": {
"date-time": {
"command": "node",
"args": ["/path/to/mcp-date-time/dist/index.js"]
}
}
}选项2:独立捆绑包(无依赖关系)
{
"mcpServers": {
"date-time": {
"command": "node",
"args": ["/path/to/mcp-date-time/dist/index.standalone.js"]
}
}
}发展模式
# Install dependencies
bun install # or npm install
# Run the server directly with Bun
bun run index.ts
# Or with Node (after building)
npm run build
node dist/index.js从源头构建
# Install dependencies
npm install
# Build both bundles
npm run build # Creates dist/index.js (with external deps)
npm run build:standalone # Creates dist/index.standalone.js (all deps included)
# Test with Node.js
npm run test:node工具文档
1.获取当前日期时间
目的: 获取当前日期和时间。每当你需要知道“现在”时,就使用这个。
输入:
{
"timezone": "America/New_York" // Optional, defaults to UTC
}输出:
{
"iso8601": "2024-11-12T15:30:45.123Z",
"unix_timestamp": 1699803045,
"unix_milliseconds": 1699803045123,
"human_readable": "11/12/2024, 3:30:45 PM",
"date_only": "11/12/2024",
"time_only": "3:30:45 PM",
"timezone": "America/New_York",
"year": 2024,
"month": 11,
"day": 12,
"hour": 15,
"minute": 30,
"second": 45,
"day_of_week": "Tuesday"
}示例用法:
LLM: "What's the current date and time?"
Tool: get-current-datetime with no arguments
Result: Shows current date/time in multiple formats2.解析日期
目的: 解析并验证日期字符串。使用此选项检查日期是否有效,并以标准格式获取。
输入:
{
"dateString": "March 15, 2024",
"timezone": "UTC" // Optional
}输出(有效日期):
{
"iso8601": "2024-03-15T00:00:00.000Z",
"unix_timestamp": 1710460800,
"unix_milliseconds": 1710460800000,
"human_readable": "3/15/2024, 12:00:00 AM",
"date_only": "3/15/2024",
"time_only": "12:00:00 AM",
"timezone": "UTC",
"is_valid": true
}输出(无效日期):
{
"is_valid": false,
"error": "Could not parse date: Invalid date string"
}示例用法:
LLM: "Is 'February 30, 2024' a valid date?"
Tool: parse-date with dateString="February 30, 2024"
Result: { "is_valid": false, "error": "..." }3.格式化日期
目的: 将日期转换为特定格式。
输入:
{
"dateString": "2024-03-15T10:30:00Z",
"format": "relative", // "iso", "unix", "date-only", "time-only", "datetime", "relative"
"timezone": "America/Los_Angeles" // Optional
}输出示例:
- 国际标准化组织:
{ "formatted": "2024-03-15T10:30:00.000Z", "format": "ISO 8601" } - Unix:
{ "formatted": "1710499800", "format": "Unix Timestamp" } - 仅限日期:
{ "formatted": "2024-03-15", "format": "YYYY-MM-DD" } - 仅限时间:
{ "formatted": "10:30:00", "format": "HH:MM:SS" } - 日期时间:
{ "formatted": "3/15/2024, 3:30:00 AM", "format": "Human Readable", "timezone": "America/Los_Angeles" } - 相对的:
{ "formatted": "5 days ago", "format": "Relative", "reference_time": "2024-03-20T10:30:00.000Z" }
示例用法:
LLM: "Show me that date in relative terms"
Tool: format-date with format="relative"
Result: "5 days ago" (or "3 hours from now", etc.)4.计算日期差异
目的: 计算两个日期之间的时差。
输入:
{
"startDate": "2024-03-15T10:30:00Z",
"endDate": "2024-03-20T15:45:00Z",
"unit": "all" // "days", "hours", "minutes", "seconds", "all"
}输出(单位=“全部”):
{
"milliseconds": 450900000,
"seconds": 450900,
"minutes": 7515,
"hours": 125,
"days": 5,
"human_readable": "5 days, 5 hours, 15 minutes, 0 seconds",
"is_past": false,
"start_date": "2024-03-15T10:30:00.000Z",
"end_date": "2024-03-20T15:45:00.000Z"
}输出(具体单位):
{
"difference": 5,
"unit": "days",
"is_past": false,
"start_date": "2024-03-15T10:30:00.000Z",
"end_date": "2024-03-20T15:45:00.000Z"
}示例用法:
LLM: "How many days until Christmas 2024?"
Tool 1: get-current-datetime to get today's date
Tool 2: calculate-date-difference with startDate=today, endDate="2024-12-25", unit="days"
Result: Shows number of days5.添加日期
目的: 在日期中增减时间。用正数加,负数减。
输入:
{
"dateString": "2024-03-15T10:30:00Z",
"amount": 5, // Use negative for subtraction
"unit": "days", // "years", "months", "days", "hours", "minutes", "seconds"
"timezone": "UTC" // Optional
}输出:
{
"original_date": "2024-03-15T10:30:00.000Z",
"result_date": "2024-03-20T10:30:00.000Z",
"operation": "+5 days",
"human_readable": "3/20/2024, 10:30:00 AM",
"timezone": "UTC"
}示例用法:
LLM: "What will the date be 3 weeks from today?"
Tool 1: get-current-datetime to get today's date
Tool 2: add-time-to-date with amount=21, unit="days"
Result: Shows date 21 days in the future设计原则
此服务器的设计考虑了LLM的可用性:
- 简单明了的名称 -工具名称是描述性的和显而易见的(例如。,
get-current-datetime不gtd) - 明确的描述 -每个工具都有详细的说明,解释何时以及如何使用它
- 结构化输出 -所有响应都是一致的JSON,具有多种格式选项
- 错误处理 -无效输入返回明确的错误消息,而不是异常
- 时区支持 -全局应用程序的可选时区参数
- 无歧义 -工具返回多种格式,以便LLM可以选择最合适的格式
常见的LLM使用模式
模式1:“今天几号?”
Tool: get-current-datetime
Output: Full date/time info including year, month, day, etc.模式2:“距离\[未来日期\]还有多少天?”
Tool 1: get-current-datetime
Tool 2: calculate-date-difference (startDate=now, endDate=future, unit="days")
Output: Number of days模式3:“两周前的日期是什么?”
Tool 1: get-current-datetime
Tool 2: add-time-to-date (amount=-14, unit="days")
Output: Date from 2 weeks ago模式4:“这个日期有效吗?”
Tool: parse-date
Output: { "is_valid": true/false, ... }为什么这种方法有效
在此服务器之前:
- 法学硕士:“今天是2023年11月15日”(错误,使用培训截止日期)
- 用户:“不,实际日期是几号?”
- LLM *猜一猜* (仍然错误)
使用此服务器:
- 用户:“今天几号?”
- LLM *调用获取当前日期时间工具*
- 工具:返回准确的当前日期
- 法学硕士:“今天是2024年11月12日”(正确!)
捆绑选项
此服务器提供两个捆绑版本:
常规捆绑包(dist/index.js -13KB)
- 在以下情况下使用: 你有
node_modules可用的 - 依赖关系: 需要
@modelcontextprotocol/sdk和zod待安装 - 尺寸: ~13KB(最小捆绑包)
- 安装命令:
npm install运行前需要
独立捆绑包(dist/index.standalone.js -214KB)
- 在以下情况下使用: 您想要一个没有外部依赖关系的单个文件
- 依赖关系: 所有依赖项都捆绑在里面
- 尺寸: ~214KB(完全独立)
- 安装命令: 没有!只需复制并运行
你应该使用哪一种?
- 常规捆绑 如果您正在开发或安装了依赖项
- 独立捆绑包 适用于分发、Docker容器或无服务器环境
两个包在功能上完全相同,并通过了所有测试。
技术细节
- 运输: stdio(与Claude Code和Claude Desktop配合使用)
- 协议: MCP(模型上下文协议)
- 运行时间: Node.js v18+或Bun
- Bundler: esbuild(用于创建Node.js包)
- 依赖关系: 最小(@modelcontextprotocol/sdk+zod)
- 日期库: 原生JavaScript日期(不需要外部日期库)
贡献
问题和PR欢迎!此服务器应保持简单,并专注于其核心任务:为LLM提供准确的日期和时间。
许可证
麻省理工学院
作者
旨在解决LLM生成错误日期和时间的实际问题。
