Gemini Interactions API vs. Stateless Image Gen
gemini-3.1-flash-lite-image model (NB2Lite) wrapped in a Model Context Protocol (MCP) server for Codex.The core advantage here is the Interactions API. Instead of treating every request as a fresh start, it uses an interaction_id to maintain visual context server-side. When you request an edit, you pass the previous ID, and the model modifies the existing canvas rather than hallucinating a new version of the scene. This turns a "re-describe everything" process into a simple "add a neon sign here" command.
Technical Implementation: MCP and Codex
To get this working as an AI workflow, the setup uses a FastMCP server that exposes four specific tools to the agent. This allows the coding agent to handle image generation and iterative refinement as native skills.
The logic follows a specific state loop:
1. Initial call via client.interactions.create with store=True.
2. The API returns an interaction_id.
3. Subsequent edits reference the latest interaction_id to maintain pixel continuity.
Deployment Details & Gotchas
From a real-world deployment perspective, there are a few technical constraints that are easy to miss if you're building from scratch:
- ID Chaining: Every turn generates a new interaction ID. You must chain the most recent one; using an older ID effectively "forks" the image state, which can lead to inconsistent results.
- Aspect Ratio Lock: The ratio (1:1, 16:9, etc.) is set at the first generation. Trying to change this during a stateful edit typically degrades the image quality, so the toolset locks the ratio after the first call.
- Thinking Levels: The API supports
low(fast) andhigh(complex). While the documentation mentionsminimalandmedium, these often trigger HTTP 400 errors with this specific model, so the server is configured to ignore them.
By packaging this as an MCP server, the integration into the development environment is seamless. The agent knows exactly when to call the image tool and how to manage the session IDs without the user manually tracking strings.