@data_wise/localtime-mcp
Version:
MCP server for local time information
64 lines (55 loc) • 1.72 kB
text/typescript
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
interface ToolResponse {
content: Array<{
type: string;
text: string;
}>;
}
class McpClient {
mcp: Client;
exitStack = [] as Array<() => Promise<void>>;
constructor() {
this.mcp = new Client({ name: 'localtime-client', version: '1.0.1' });
}
connectToServer = async () => {
const transport = new StdioClientTransport({
command: 'npm run start-mcp-server',
args: [],
});
await this.mcp.connect(transport);
};
getLocalTime = async () => {
try {
const result = await this.mcp.callTool({
name: 'getLocalTime',
arguments: {
input: {}
}
}) as ToolResponse;
return JSON.parse(result.content[0].text);
} catch (error: any) {
return { error: error.message };
}
};
cleanup = async () => {
for (const exit of this.exitStack) {
await exit();
}
};
}
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();