spacePhoto.ts•1.12 kB
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
export function registerSpacePhoto(server: McpServer) {
server.tool(
"space-photo",
"Get NASA's Astronomy Picture of the Day",
z.object({}),
async () => {
const ctrl = new AbortController();
const id = setTimeout(() => ctrl.abort(), 8000);
try {
const res = await fetch("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY", {
signal: ctrl.signal,
});
const data = await res.json();
const result = {
title: data.title,
url: data.url,
explanation: data.explanation,
};
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
} catch (err: any) {
return {
content: [
{
type: "text",
text: JSON.stringify({ error: err.message }),
},
],
};
} finally {
clearTimeout(id);
}
}
);
}