@data_wise/localtime-mcp
Version:
MCP server for local time information
52 lines (51 loc) • 1.62 kB
JavaScript
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
class McpClient {
constructor() {
this.exitStack = [];
this.connectToServer = async () => {
const transport = new StdioClientTransport({
command: 'npm run start-mcp-server',
args: [],
});
await this.mcp.connect(transport);
};
this.getLocalTime = async () => {
try {
const result = await this.mcp.callTool({
name: 'getLocalTime',
arguments: {
input: {}
}
});
return JSON.parse(result.content[0].text);
}
catch (error) {
return { error: error.message };
}
};
this.cleanup = async () => {
for (const exit of this.exitStack) {
await exit();
}
};
this.mcp = new Client({ name: 'localtime-client', version: '1.0.1' });
}
}
const main = async () => {
const mcp = new McpClient();
try {
await mcp.connectToServer();
const timeInfo = await mcp.getLocalTime();
console.log('\n======================================');
console.log(JSON.stringify(timeInfo, null, 2));
console.log('======================================\n');
}
catch (error) {
console.error('Error:', error);
}
finally {
await mcp.cleanup();
}
};
main();