UNPKG

mcp-wtit

Version:

A Model Context Protocol (MCP) server that provides current time in ISO8601 format with timezone support

100 lines 3.7 kB
import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js'; export class TimeServer { dependencies; server; serverInfo = { name: 'mcp-wtit', version: '1.0.0', description: 'MCP server that provides current time in ISO8601 format', }; constructor(dependencies) { this.dependencies = dependencies; this.server = new Server(this.serverInfo, { capabilities: { tools: {}, }, }); this.setupHandlers(); } setupHandlers() { this.server.setRequestHandler(ListToolsRequestSchema, this.handleListTools.bind(this)); this.server.setRequestHandler(CallToolRequestSchema, async (request) => { return this.handleCallTool(request.params); }); } async handleListTools() { return { tools: [ { name: 'get_current_time', description: 'Get the current time with detailed information including ISO8601 format, timestamp, and timezone', inputSchema: { type: 'object', properties: { includeMilliseconds: { type: 'boolean', description: 'Include milliseconds in the ISO8601 format (default: true)', default: true, }, timezone: { type: 'string', description: 'Timezone for the time (default: UTC). Examples: "UTC", "America/New_York", "Asia/Tokyo"', default: 'UTC', }, }, }, }, ], }; } async handleCallTool(request) { const { name, arguments: args } = request; switch (name) { case 'get_current_time': return this.handleGetCurrentTime(args); default: throw new Error(`Unknown tool: ${name}`); } } async handleGetCurrentTime(args) { const input = this.parseTimeArguments(args); const result = await this.dependencies.getCurrentTimeUseCase.execute(input); if (!result.success || !result.data) { return { content: [ { type: 'text', text: `Error: ${result.error || 'Failed to get current time'}`, }, ], isError: true, }; } return { content: [ { type: 'text', text: JSON.stringify(result.data, null, 2), }, ], }; } parseTimeArguments(args) { if (!args || typeof args !== 'object') { return {}; } const { includeMilliseconds, timezone } = args; return { includeMilliseconds: typeof includeMilliseconds === 'boolean' ? includeMilliseconds : undefined, timezone: typeof timezone === 'string' ? timezone : undefined, }; } async start() { const transport = new StdioServerTransport(); await this.server.connect(transport); console.error('MCP Time Server (mcp-wtit) started successfully'); } } //# sourceMappingURL=time.server.js.map