getCurrentTime.ts•1.06 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
export function registerGetCurrentTime(server: McpServer) {
server.tool(
"get-current-time",
"Fetch the current time on server",
{},
async () => {
const ctrl = new AbortController();
const id = setTimeout(() => ctrl.abort(), 8000);
try {
const response = new Date().toLocaleString();
const data = response;
if (!data) {
return {
content: [
{
type: "text",
text: `No results image found.`,
},
],
};
}
return {
content: [
{
type: "text",
text: response,
},
],
};
} catch (err: any) {
return {
content: [
{
type: "text",
text: JSON.stringify({ error: err.message }),
},
],
};
} finally {
clearTimeout(id);
}
}
);
}