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 Microsoft.Extensions.Hosting@10.0.11
#:package ModelContextProtocol@2.2.0
#: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

Claude Code and Codex read different files, in different formats. Pick yours, add the snippet to your project root, and commit it. Everyone who clones the project then gets the server. Don't paste one client's format into the other's file.

Claude Code — .mcp.json

Note: This example also covers 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"]
    }
  }
}

claude mcp add date-times-mcp --scope project -- dotnet run ./.mcp-servers/date-times-mcp.cs -v q writes the same entry for you. Full details: /install/claude-code.md.

Codex — .codex/config.toml

[mcp_servers.date-times-mcp]
command = "dotnet"
args = [
    "run",
    "./.mcp-servers/date-times-mcp.cs",
    "-v",
    "q",
]
startup_timeout_sec = 60

Codex needs the project trusted. It ignores a project .codex/config.toml unless your personal ~/.codex/config.toml holds [projects.'<absolute path>'] with trust_level = "trusted" — and there is no error message when it doesn't. On Windows use the plain C:\Users\... path, never the extended \\?\C:\... form. Don't commit that entry. See /install/codex.md.

Both paths are relative to the project, so they work for everyone who clones it, on every operating system. -v q is required in both: standard output carries the JSON-RPC stream, and without it the build output can reach that stream and break the connection.

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. Codex reads its configuration at startup only, so restart it and open a new chat.

Statistics

Version 1.0.0
License MIT

Author

XAKPC Dev Labs

Maintained by the AnyMCP community