Skip to main content
Connect DSPy’s native MCP tools to Magic Hour, check API authorization, and recover metadata for an image project you already created. This example makes no model, creation, or media-download request.

Install and configure

Requires Python 3.12. Download recover-image.py and requirements.txt into the same directory, then install in a virtual environment:
The example pins dspy[mcp] to 3.4.1, mcp to 2.3.0, and httpx2 to 2.16.0. Provide a Magic Hour API key through your server-side secret manager as MAGIC_HOUR_API_KEY. Set MAGIC_HOUR_IMAGE_PROJECT_ID to the saved ID returned by your original image-creation request. Keep keys, account details, and temporary download URLs private.

Check the saved project

DSPy’s Tool.from_mcp_tool converts discovered tools into native DSPy tools. Calling .acall() directly invokes the selected tool without an LM or agent choosing what to call. Discovery alone does not validate your key; the example checks account field types before reporting authorization.
The example prints the discovered tool count, an authorization marker, and the requested project’s ID and status. It does not print the account balance, credentials, raw responses, or signed media URLs. Each HTTP client and MCP session closes through its native async context. DSPy caches are disabled, callbacks are empty, and no model or tracer is configured.

Results and recovery

result_mode="structured" uses MCP structured content when available. DSPy’s fallback may return a JSON string; the example accepts a dictionary or that JSON-string form. It checks that the returned project ID matches the saved ID and that status is a string. The wait tool may poll the same saved project for up to 30 seconds. A timeout does not mean generation failed or stopped. Retain the saved ID and run recovery later. On failure, the example reports only a static phase label and exits with status 1. It never creates another project or automatically retries a creation request. This example recovers metadata only. Inline downloads are disabled. DSPy 3.4.1’s MCP conversion can prefer text over non-text content when both are present; this example does not preserve inline images or save a media file. Model reasoning and image creation require separate application logic and are outside its scope.

Scope and verification

Native validation with Python 3.12, DSPy 3.4.1, and MCP 2.3.0 converted 48 tools, verified account field types, and recovered the saved image project with status complete. The exact runnable example exited successfully with no stderr; its client contexts closed and the process ended. No model, creation, or media-download call was made. Discovery does not mean all 48 tools were executed. See Magic Hour MCP, our privacy policy, and DSPy’s official MCP tools documentation.