【推荐优先调用】将自然语言日期表达式解析为标准日期范围 **为什么需要这个工具?** 用户经常使用"本周"、"最近7天"等自然语言表达日期,但 AI 模型自己计算日期 可能导致不一致的结果。此工具在服务器端使用精确的当前时间计算,确保所有 AI 模型获得一致的日期范围。 **推荐使用流程:** 1. 用户说"分析AI本周的情感倾向" 2. AI 调用 resolve_date_range("本周") → 获取精确日期范围 3. AI 调用 analyze_sentiment(topic="ai", date_rang
(
expression: str
)
| 117 | |
| 118 | @mcp.tool |
| 119 | async def resolve_date_range( |
| 120 | expression: str |
| 121 | ) -> str: |
| 122 | """ |
| 123 | 【推荐优先调用】将自然语言日期表达式解析为标准日期范围 |
| 124 | |
| 125 | **为什么需要这个工具?** |
| 126 | 用户经常使用"本周"、"最近7天"等自然语言表达日期,但 AI 模型自己计算日期 |
| 127 | 可能导致不一致的结果。此工具在服务器端使用精确的当前时间计算,确保所有 |
| 128 | AI 模型获得一致的日期范围。 |
| 129 | |
| 130 | **推荐使用流程:** |
| 131 | 1. 用户说"分析AI本周的情感倾向" |
| 132 | 2. AI 调用 resolve_date_range("本周") → 获取精确日期范围 |
| 133 | 3. AI 调用 analyze_sentiment(topic="ai", date_range=上一步返回的date_range) |
| 134 | |
| 135 | Args: |
| 136 | expression: 自然语言日期表达式,支持: |
| 137 | - 单日: "今天", "昨天", "today", "yesterday" |
| 138 | - 周: "本周", "上周", "this week", "last week" |
| 139 | - 月: "本月", "上月", "this month", "last month" |
| 140 | - 最近N天: "最近7天", "最近30天", "last 7 days", "last 30 days" |
| 141 | - 动态: "最近5天", "last 10 days"(任意天数) |
| 142 | |
| 143 | Returns: |
| 144 | JSON格式的日期范围,可直接用于其他工具的 date_range 参数: |
| 145 | { |
| 146 | "success": true, |
| 147 | "expression": "本周", |
| 148 | "date_range": { |
| 149 | "start": "2025-11-18", |
| 150 | "end": "2025-11-26" |
| 151 | }, |
| 152 | "current_date": "2025-11-26", |
| 153 | "description": "本周(周一到周日,11-18 至 11-26)" |
| 154 | } |
| 155 | |
| 156 | Examples: |
| 157 | 用户:"分析AI本周的情感倾向" |
| 158 | AI调用步骤: |
| 159 | 1. resolve_date_range("本周") |
| 160 | → {"date_range": {"start": "2025-11-18", "end": "2025-11-26"}, ...} |
| 161 | 2. analyze_sentiment(topic="ai", date_range={"start": "2025-11-18", "end": "2025-11-26"}) |
| 162 | |
| 163 | 用户:"看看最近7天的特斯拉新闻" |
| 164 | AI调用步骤: |
| 165 | 1. resolve_date_range("最近7天") |
| 166 | → {"date_range": {"start": "2025-11-20", "end": "2025-11-26"}, ...} |
| 167 | 2. search_news(query="特斯拉", date_range={"start": "2025-11-20", "end": "2025-11-26"}) |
| 168 | """ |
| 169 | try: |
| 170 | result = await asyncio.to_thread(DateParser.resolve_date_range_expression, expression) |
| 171 | return json.dumps(result, ensure_ascii=False, indent=2) |
| 172 | except MCPError as e: |
| 173 | return json.dumps({ |
| 174 | "success": False, |
| 175 | "error": e.to_dict() |
| 176 | }, ensure_ascii=False, indent=2) |