#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
const server = new Server(
{
name: "DateTime MCP Server",
version: "0.1.0",
},
{
capabilities: {
tools: {},
},
}
);
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "get_current_datetime",
description: "Get the current date and time",
inputSchema: {
type: "object",
properties: {
format: {
type: "string",
description: "Output format: 'iso' (ISO 8601), 'unix' (timestamp), 'human' (readable), or a custom format string",
default: "iso"
},
timezone: {
type: "string",
description: "Timezone (e.g., 'UTC', 'America/New_York'). Defaults to system timezone",
default: "system"
}
}
}
},
{
name: "get_current_date",
description: "Get only the current date",
inputSchema: {
type: "object",
properties: {
format: {
type: "string",
description: "Output format: 'iso' (YYYY-MM-DD), 'us' (MM/DD/YYYY), 'eu' (DD/MM/YYYY), or custom format",
default: "iso"
}
}
}
},
{
name: "get_current_time",
description: "Get only the current time",
inputSchema: {
type: "object",
properties: {
format: {
type: "string",
description: "Output format: '24h' (HH:MM:SS), '12h' (hh:MM:SS AM/PM), or custom format",
default: "24h"
},
include_seconds: {
type: "boolean",
description: "Include seconds in the output",
default: true
}
}
}
}
]
};
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const now = new Date();
switch (request.params.name) {
case "get_current_datetime": {
const format = request.params.arguments?.format || "iso";
const timezone = request.params.arguments?.timezone || "system";
let result: string;
if (format === "iso") {
result = now.toISOString();
} else if (format === "unix") {
result = Math.floor(now.getTime() / 1000).toString();
} else if (format === "human") {
result = now.toString();
} else {
result = formatDate(now, format as string);
}
return {
content: [{
type: "text",
text: result
}]
};
}
case "get_current_date": {
const format = request.params.arguments?.format || "iso";
let result: string;
if (format === "iso") {
result = now.toISOString().split('T')[0];
} else if (format === "us") {
result = `${(now.getMonth() + 1).toString().padStart(2, '0')}/${now.getDate().toString().padStart(2, '0')}/${now.getFullYear()}`;
} else if (format === "eu") {
result = `${now.getDate().toString().padStart(2, '0')}/${(now.getMonth() + 1).toString().padStart(2, '0')}/${now.getFullYear()}`;
} else {
result = formatDate(now, format as string);
}
return {
content: [{
type: "text",
text: result
}]
};
}
case "get_current_time": {
const format = request.params.arguments?.format || "24h";
const includeSeconds = request.params.arguments?.include_seconds !== false;
let result: string;
if (format === "24h") {
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
result = includeSeconds ? `${hours}:${minutes}:${seconds}` : `${hours}:${minutes}`;
} else if (format === "12h") {
let hours = now.getHours();
const ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12 || 12;
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
result = includeSeconds ? `${hours}:${minutes}:${seconds} ${ampm}` : `${hours}:${minutes} ${ampm}`;
} else {
result = formatDate(now, format as string);
}
return {
content: [{
type: "text",
text: result
}]
};
}
default:
throw new Error("Unknown tool");
}
});
function formatDate(date: Date, format: string): string {
const replacements: { [key: string]: string } = {
'YYYY': date.getFullYear().toString(),
'MM': (date.getMonth() + 1).toString().padStart(2, '0'),
'DD': date.getDate().toString().padStart(2, '0'),
'HH': date.getHours().toString().padStart(2, '0'),
'mm': date.getMinutes().toString().padStart(2, '0'),
'ss': date.getSeconds().toString().padStart(2, '0'),
};
let result = format;
for (const [key, value] of Object.entries(replacements)) {
result = result.replace(new RegExp(key, 'g'), value);
}
return result;
}
/**
* Start the server using stdio transport.
* This allows the server to communicate via standard input/output streams.
*/
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
main().catch((error) => {
console.error("Server error:", error);
process.exit(1);
});