date-times-mcp.cs

Provides date and time utilities including current date/time, date calculations, and formatting

datetime utilities formatting calculations
View on GitHub

Available Tools

GetCurrentDateTime

Gets current date and time in various formats

DateDifference

Calculates the difference between two dates

AddTime

Adds or subtracts time from a date

Source Code

#:package [email protected]
#:package [email protected]
#:property PublishAot=false
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;

var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{
    // Configure all logs to go to stderr
    consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});

// Register the MCP server
builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();

// Build and run the MCP Server Application
await builder.Build().RunAsync();

//====== TOOLS ======

public record CurrentDateTimeResult(string FormattedDateTime);
public record DateDifferenceResult(int Days, int Hours, int Minutes);
public record AddTimeResult(DateTime ResultDate);

[McpServerToolType] 
public static class DateTimeTools
{
    [McpServerTool, Description("Gets current date and time in various formats")]
    public static CurrentDateTimeResult GetCurrentDateTime(
        [Description("Date format string (default: yyyy-MM-dd HH:mm:ss)")] string format = "yyyy-MM-dd HH:mm:ss")
    {
        return new CurrentDateTimeResult(DateTime.Now.ToString(format));
    }

    [McpServerTool, Description("Calculates the difference between two dates")]
    public static DateDifferenceResult DateDifference(
        [Description("Start date")] DateTime startDate, 
        [Description("End date")] DateTime endDate)
    {
        var diff = endDate - startDate;
        return new DateDifferenceResult(diff.Days, diff.Hours, diff.Minutes);
    }

    [McpServerTool, Description("Adds or subtracts time from a date")]
    public static AddTimeResult AddTime(
        [Description("Base date")] DateTime date, 
        [Description("Days to add")] int days = 0, 
        [Description("Hours to add")] int hours = 0, 
        [Description("Minutes to add")] int minutes = 0)
    {
        return new AddTimeResult(date.AddDays(days).AddHours(hours).AddMinutes(minutes));
    }
}

How to Run

1. Save the server code

Put it in your project, in a .mcp-servers/ directory. Copy the code above, or download it from /servers/date-times-mcp/date-times-mcp.cs. Commit it: the file is the whole server.

2. Build it once

The first build restores the packages. It takes longer than the startup timeout of most clients, so run it by hand before you go on:

dotnet build .mcp-servers/date-times-mcp.cs -v q

This adds no bin or obj directory to your project. A file-based app is not a project, so the SDK caches the build under your temp directory instead. Your .gitignore needs no new line.

3. Configure your MCP client

Add this to .mcp.json in your project root, and commit it. Everyone who clones the project then gets the server.

Note: This example is for Claude Code, Claude Desktop, Cursor, and Windsurf. Visual Studio and VS Code use servers instead of mcpServers.

{
  "mcpServers": {
    "date-times-mcp": {
      "type": "stdio",
      "command": "dotnet",
      "args": ["run", "./.mcp-servers/date-times-mcp.cs", "-v", "q"]
    }
  }
}

The path is relative to the project, so it works for everyone who clones it, on every operating system. -v q is required: standard output carries the JSON-RPC stream, and without it the build output can reach that stream and break the connection.

With Claude Code, claude mcp add date-times-mcp --scope project -- dotnet run ./.mcp-servers/date-times-mcp.cs -v q writes the same entry for you. Add --scope user instead, with an absolute path, to install it for every project.

4. Restart your LLM client

Restart your client in the project directory, then check that date-times-mcp shows as connected. Claude Code asks you to approve a project server the first time it reads .mcp.json.

Statistics

Version 1.0.0
License MIT

Author

XAKPC Dev Labs

Maintained by the AnyMCP community