Image generation in OpenCode: MCP server setup
OpenCode nests servers a level deeper than most clients: add the Image MCP Server under mcp.servers in opencode.json as "type": "remote", with url, headers and "oauth": false. OpenCode is terminal-native and configuration-first, so the fastest route in is the config file or opencode mcp add. Its current format moved servers under mcp.servers, so even a config copied from an older OpenCode guide misses.
Add the server
- Where the config lives
- ~/.config/opencode/opencode.json(c) (global) · opencode.json(c) or .opencode/opencode.json(c) (project)
- The key it nests under
- mcp.servers
- 1
Create an API key. Create a key in the Image MCP Server dashboard, and export it as IMAGEMCP_API_KEY.
- 2
Open opencode.json. Edit ~/.config/opencode/opencode.json for every project, or opencode.json in the project root.
- 3
Add it under mcp.servers. Use "type": "remote", the endpoint as url, the key as a header, and "oauth": false.
- 4
Check with opencode mcp list. A working server is listed as connected.
The server reads the key from an x-api-key header, an Authorization: Bearer header or a ?apikey= query parameter — see authentication. What each tool costs is on the credits page.
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"servers": {
"imagemcp": {
"type": "remote",
"url": "https://mcp.imagemcpserver.com/mcp",
"oauth": false,
"headers": { "x-api-key": "{env:IMAGEMCP_API_KEY}" }
}
}
}
}$ opencode mcp add imagemcp --global \
--url https://mcp.imagemcpserver.com/mcp \
--header 'x-api-key={env:IMAGEMCP_API_KEY}'
# then add "oauth": false to the entry it writes
$ opencode mcp listmcp.servers, "type": "remote", and OAuth off
Three things differ from the configs you will have copied from elsewhere. Servers sit under mcp.servers — not mcpServers, and in the current format not directly under mcp either. The connection is "type": "remote" rather than a transport name. And OpenCode tries OAuth on every remote server unless you set "oauth": false, which its docs reserve for servers that authenticate only with an API key or header, as this one does.
Check it connected in OpenCode
Run opencode mcp list: a working entry shows ✓ imagemcp connected. Then ask for your image credit balance: get_user_info is free, and it is the first call that actually checks the key, because the server lists its tools without one.
Example workflow in OpenCode
A social card for every docs page
- 1
Reference the key, do not paste it
Write the header as {env:IMAGEMCP_API_KEY}, so opencode.json can be committed.
- 2
Check it connected
opencode mcp list should show ✓ imagemcp connected.
- 3
Generate and shrink
Ask OpenCode for one card per page with generate_image (15 credits on the default model), then compress each one (1 credit).
When OpenCode will not connect
| Symptom | Usual cause | Fix |
|---|---|---|
| imagemcp shows needs authentication | oauth is still on; OpenCode enables it for every remote server by default. | Set "oauth": false on the entry; this server authenticates by header only. |
| The header reaches the server as literal text | The key was written as $IMAGEMCP_API_KEY. | Use {env:IMAGEMCP_API_KEY}; OpenCode does not expand $NAME inside JSON strings. |
| The server is configured but never connects | It sits directly under mcp, the older V1 layout. | Move it under mcp.servers. |
Once OpenCode is connected
OpenCode gets the server’s 8 image tools, plus 3 utility tools for the credit balance, the model list and batching: 11 MCP tools in all. The ones most setups reach for first are the AI image generation API, Transparent PNG generator API and Image compression API; each page covers what the tool does and what a call costs. If the protocol itself is new to you, what is MCP explains how a client like OpenCode finds these tools and calls them.
- generate_image · 8–30 creditsAI image generation APIText-to-image and image-to-image, any live model
- generate_transparent_image · 16–38 creditsTransparent PNG generator APIGenerate and cut out in one call
- compress_image · 1 creditImage compression APIRe-encode smaller with a quality dial
- Use caseAI Open Graph imagesThe complete workflow, with the calls in order and what each one costs.
Setting up another client too? The Kilo Code MCP setup and Codex CLI MCP setup guides use the same endpoint and key in a different config shape.
OpenCode questions
How do I add an MCP server to OpenCode?
Run opencode mcp add with --url and --header, or add an entry under mcp.servers in opencode.json yourself. opencode mcp add writes to the project config unless you pass --global.
Why is my mcpServers block ignored?
Because OpenCode does not use that key. Its current format nests servers under mcp.servers, so a block copied from Cursor, Cline or Claude Desktop parses as valid JSON and connects nothing.
My server sits directly under mcp. Is that wrong?
That is the older V1 layout, along with an enabled field. OpenCode’s current docs say V2 does not place server names directly under mcp, and use disabled rather than enabled to switch a server off.
Why set "oauth": false?
OpenCode enables OAuth for remote servers unless oauth is false, and its docs say to turn it off only for servers that use an API key or another header credential exclusively. This server is one of those: the key in the header is the whole of its authentication.
How do I keep the key out of the file?
Write it as {env:IMAGEMCP_API_KEY}. OpenCode substitutes {env:NAME} references; shell-style $NAME is not expanded inside JSON strings.
Sources & references