servers icon indicating copy to clipboard operation
servers copied to clipboard

Time/date operations

Open bartolli opened this issue 7 months ago • 0 comments

Adds date calculation capabilities to the MCP time server with two new tools for performing date arithmetic and determining days of the week.

Description

This PR enhances the time server with date operation functionality through two new tools:

  • calculate_date: Performs date arithmetic by adding or subtracting days from a given date
  • get_day_of_week: Determines the day of the week for any given date

Both tools support multiple date formats (YYYY-MM-DD, Month DD YYYY, MM/DD/YYYY, etc.) and provide clear error messages with format hints when parsing fails.

Server Details

  • Server: time
  • Changes to: tools (added 2 new tools)

Motivation and Context

LLMs often struggle with date arithmetic, leading to calculation errors and hallucinations when asked, "What date is 5 days from today?" This PR addresses this by providing deterministic, mathematically accurate date calculations.

Previously, the time server only handled current time and timezone conversions. These new tools enable:

  • Accurate date arithmetic: LLMs can now reliably calculate future/past dates without hallucination risks
  • Agentic workflows: Agents can schedule tasks, set reminders, and manage deadlines with confidence
  • Memory operations: When storing dated information, agents can calculate relative dates (e.g., "remind me in 10 days")
  • Calendar operations: Verify days of the week for scheduling meetings or events
  • Data processing: Handle date-based queries in datasets with mathematical precision

Example: Instead of an LLM guessing that "today + 5 days = May 30, 2025", it now receives a verified calculation with day-of-week confirmation (Friday), eliminating common errors like incorrect month transitions or leap year mistakes.

Also fixes a bug where McpError was incorrectly initialized with a string instead of an ErrorData object.

How Has This Been Tested?

  • Added 17 comprehensive unit tests covering:
    • Basic date addition/subtraction
    • Month and year boundary crossing
    • Leap year handling
    • Multiple date format support
    • Invalid date error handling
  • All existing tests continue to pass (42 tests total)
  • Manually tested with various date formats and edge cases

Breaking Changes

No breaking changes. This is a non-breaking addition of new functionality.

Types of changes

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)
  • [x] Documentation update

Checklist

  • [x] I have read the MCP Protocol Documentation
  • [x] My changes follow MCP security best practices
  • [x] I have updated the server's README accordingly
  • [x] I have tested this with an LLM client
  • [x] My code follows the repository's style guidelines
  • [x] New and existing tests pass locally
  • [x] I have added appropriate error handling
  • [x] I have documented all environment variables and configuration options

Additional context

  • Date calculations are timezone-aware to ensure accuracy across different time zones
  • The DateCalculationResult includes a calculation_details field that provides human-readable feedback about what operation was performed
  • Error messages include all supported date formats to help LLMS correct input errors

bartolli avatar May 25 '25 22:05 bartolli