nasa_apod
Fetch NASA's Astronomy Picture of the Day for specific dates, date ranges, or random selections to access space imagery and explanations.
Instructions
Fetch NASA's Astronomy Picture of the Day
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | Yes | The date of the APOD image to retrieve (YYYY-MM-DD) | |
| count | No | Count of random APODs to retrieve | |
| start_date | No | Start date for date range search (YYYY-MM-DD) | |
| end_date | No | End date for date range search (YYYY-MM-DD) | |
| thumbs | No | Return URL of thumbnail for video content |
Implementation Reference
- src/handlers/nasa/apod.ts:22-61 (handler)The core handler function for the 'nasa_apod' tool. Fetches data from NASA's APOD API using nasaApiRequest, processes the result with base64 image encoding, adds resources, and returns formatted content including text summary and embeddable images.export async function nasaApodHandler(params: ApodParams) { try { // Call the NASA APOD API const result = await nasaApiRequest('/planetary/apod', params); // Store results as resources const processedResult = await processApodResultWithBase64(result); return { content: [ { type: "text", text: processedResult.summary }, ...processedResult.images.map(img => ({ type: "text", text: `` })), ...processedResult.images.map(img => ({ type: "image", data: img.base64, mimeType: img.mimeType || "image/jpeg" })) ], isError: false }; } catch (error: any) { console.error('Error in APOD handler:', error); return { content: [ { type: "text", text: `Error retrieving APOD data: ${error.message}` } ], isError: true }; } }
- src/handlers/nasa/apod.ts:7-14 (schema)Zod schema defining the input parameters for the nasa_apod tool, including optional date, hd, count, date range, and thumbs.export const apodParamsSchema = z.object({ date: z.string().optional(), hd: z.boolean().optional(), count: z.number().int().positive().optional(), start_date: z.string().optional(), end_date: z.string().optional(), thumbs: z.boolean().optional() });
- src/index.ts:697-726 (registration)Tool registration in the tools/list handler response. Defines the 'nasa_apod' tool name, description, and input schema for MCP tool discovery.{ name: "nasa_apod", description: "Fetch NASA's Astronomy Picture of the Day", inputSchema: { type: "object", properties: { date: { type: "string", description: "The date of the APOD image to retrieve (YYYY-MM-DD)" }, count: { type: "number", description: "Count of random APODs to retrieve" }, start_date: { type: "string", description: "Start date for date range search (YYYY-MM-DD)" }, end_date: { type: "string", description: "End date for date range search (YYYY-MM-DD)" }, thumbs: { type: "boolean", description: "Return URL of thumbnail for video content" } }, required: ["date"] } },
- src/handlers/nasa/apod.ts:64-104 (helper)Supporting function that processes APOD API results: generates summary markdown, adds JSON resources, fetches images via axios, encodes to base64 for embedding, and detects MIME types.async function processApodResultWithBase64(result: any) { const results = Array.isArray(result) ? result : [result]; let summary = ''; const images: any[] = []; for (const apod of results) { const apodId = `nasa://apod/image?date=${apod.date}`; let mimeType = 'image/jpeg'; if (apod.url) { if (apod.url.endsWith('.png')) mimeType = 'image/png'; else if (apod.url.endsWith('.gif')) mimeType = 'image/gif'; else if (apod.url.endsWith('.jpg') || apod.url.endsWith('.jpeg')) mimeType = 'image/jpeg'; } addResource(apodId, { name: `Astronomy Picture of the Day - ${apod.title}`, mimeType: 'application/json', text: JSON.stringify(apod, null, 2) }); summary += `## ${apod.title} (${apod.date})\n\n${apod.explanation}\n\n`; if (apod.url && (!apod.media_type || apod.media_type === 'image')) { summary += `Image URL: ${apod.url}\n\n`; let base64 = null; try { const imageResponse = await axios.get(apod.url, { responseType: 'arraybuffer', timeout: 30000 }); base64 = Buffer.from(imageResponse.data).toString('base64'); } catch (err) { console.error('Failed to fetch APOD image for base64:', apod.url, err); } images.push({ url: apod.url, title: apod.title, resourceUri: apodId, mimeType, base64 }); } } return { summary, images }; }
- src/index.ts:1480-1494 (registration)Direct MCP request handler registration for the 'nasa/apod' method, which validates params with Zod schema matching apodParamsSchema and delegates execution to the general handleToolCall function.server.setRequestHandler( z.object({ method: z.literal("nasa/apod"), params: z.object({ date: z.string().optional(), start_date: z.string().optional(), end_date: z.string().optional(), count: z.number().optional(), thumbs: z.boolean().optional() }).optional() }), async (request) => { return await handleToolCall("nasa/apod", request.params || {}); } );