@data_wise/localtime-mcp
Version:
MCP server for local time information
94 lines (83 loc) • 2.33 kB
text/typescript
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { z } from 'zod';
import moment from 'moment-timezone';
// 添加进程错误处理
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
process.exit(1);
});
const server = new McpServer({
name: 'localtime',
version: '1.0.6',
capabilities: {
resources: {},
tools: {},
},
});
interface TimeState {
input: {
format?: string;
};
}
// 获取系统时间信息工具
server.tool('getLocalTime', 'Get comprehensive system time information', {
input: z.object({
format: z.string().optional()
})
}, async (state: TimeState) => {
const { format = 'YYYY-MM-DD HH:mm:ss' } = state.input;
const systemTimezone = moment.tz.guess();
try {
const now = moment();
const response = {
unixTimestamp: Math.floor(now.valueOf() / 1000),
formattedTime: now.format(format),
utc: now.utc().format('YYYY-MM-DD HH:mm:ss [UTC]'),
timezone: {
name: systemTimezone,
offset: now.tz(systemTimezone).format('Z'),
abbreviation: now.tz(systemTimezone).format('z')
}
};
return {
content: [
{
type: 'text',
text: JSON.stringify(response, null, 2)
}
]
};
} catch (error: any) {
return {
content: [
{
type: 'text',
text: `Error getting system time information: ${error.message}`
}
]
};
}
});
async function main() {
try {
console.error('Starting Local Time MCP Server...');
const transport = new StdioServerTransport();
await server.connect(transport);
console.error('Local Time MCP Server running on stdio');
// 保持进程运行
process.stdin.resume();
} catch (error) {
console.error('Error in main():', error);
process.exit(1);
}
}
main().catch((error) => {
console.error('Fatal error in main():', error);
process.exit(1);
});