space_fact
Retrieve NASA's Astronomy Picture of the Day with title, date, description, and image URL to explore daily space imagery and astronomical content.
Instructions
Get NASA's Astronomy Picture of the Day (APOD) with its title, date, description, and image URL.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- server.py:43-67 (handler)The main handler function for the 'space_fact' tool, decorated with @mcp.tool() for registration. It fetches the NASA Astronomy Picture of the Day (APOD), extracts title, date, explanation, and image URL, and formats a response message.@mcp.tool() def space_fact() -> str: """Get NASA's Astronomy Picture of the Day (APOD) with its title, date, description, and image URL.""" try: api_key = os.getenv("NASA_API_KEY", "DEMO_KEY") # fallback if not set url = f"https://api.nasa.gov/planetary/apod?api_key={api_key}" response = requests.get(url) if response.status_code == 200: data = response.json() title = data.get("title", "Unknown Title") explanation = data.get("explanation", "No description available.") date = data.get("date", "") image_url = data.get("hdurl") or data.get("url") # sometimes only 'url' exists message = ( f"🪐 **{title}** ({date})\n\n" f"{explanation}\n\n" f"📸 Image URL: {image_url}" ) return message else: return f"⚠️ NASA API error: {response.status_code}" except Exception as e: return f"❌ Something went wrong: {e}"