spacexLaunches.ts•1.58 kB
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
export function registerSpacexLaunches(server: McpServer) {
server.tool(
"spacex-launches",
"Get recent SpaceX launches",
{
limit: z.string().optional().describe("Number of launches to return (default 5)"),
},
async ({ limit }) => {
const lim = Math.max(1, Math.min(Number(limit) || 5, 50));
const ctrl = new AbortController();
const id = setTimeout(() => ctrl.abort(), 8000);
try {
const res = await fetch("https://api.spacexdata.com/v5/launches/query", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: {},
options: { sort: { date_utc: "desc" }, limit: lim },
}),
signal: ctrl.signal,
});
const data = await res.json();
const results = data.docs?.map((d: any) => ({
name: d.name,
date_utc: d.date_utc,
success: d.success,
details: d.details,
webcast: d.links?.webcast,
}));
return {
content: [
{
type: "text",
text: JSON.stringify(results, null, 2),
},
],
};
} catch (err: any) {
return {
content: [
{
type: "text",
text: JSON.stringify({ error: err.message }),
},
],
};
} finally {
clearTimeout(id);
}
}
);
}