The Grok2 Image MCP Server allows chat assistants to generate images using the Grok-2 model through the Model Context Protocol (MCP). This server acts as a bridge between chat applications and xAI's image generation capabilities.
You can quickly start the server using npx:
npx -y grok2-image-mcp-server
To configure your MCP configuration file, add the following JSON:
{
"mcpServers": {
"grok2_image": {
"command": "npx",
"args": [
"grok2-image-mcp-server"
],
"env": {
"XAIAPI_KEY": "xAI Key"
}
}
}
}
The server supports several environment variables for configuration:
https://api.x.ai/v1
). Must end with /v1
imgen.x.ai
Using an API proxy:
XAIAPI_BASE_URL=https://api-proxy.me/xai/v1
Using an image proxy domain:
IMAGE_PROXY_DOMAIN=https://image.proxy.workers.dev
Using an HTTP proxy:
HTTP_PROXY=http://127.0.0.1:7890
# or with authentication
HTTP_PROXY=https://user:[email protected]:8080
If you're experiencing issues accessing images, you can set up a Cloudflare Workers proxy:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const TARGET_DOMAIN = 'imgen.x.ai'
async function handleRequest(request) {
const url = new URL(request.url)
const targetUrl = `https://${TARGET_DOMAIN}${url.pathname}${url.search}`
const init = {
method: request.method,
headers: request.headers,
body: request.method === 'GET' || request.method === 'HEAD' ? undefined : request.body,
redirect: 'follow'
}
const response = await fetch(targetUrl, init)
const newHeaders = new Headers(response.headers)
newHeaders.set('Access-Control-Allow-Origin', '*')
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders
})
}
IMAGE_PROXY_DOMAIN
environment variable with your custom domain (e.g., https://image.proxy.workers.dev
)To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "grok2_image" '{"command":"npx","args":["grok2-image-mcp-server"],"env":{"XAIAPI_KEY":"xAI Key"}}'
See the official Claude Code MCP documentation for more details.
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"grok2_image": {
"command": "npx",
"args": [
"grok2-image-mcp-server"
],
"env": {
"XAIAPI_KEY": "xAI Key"
}
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"grok2_image": {
"command": "npx",
"args": [
"grok2-image-mcp-server"
],
"env": {
"XAIAPI_KEY": "xAI Key"
}
}
}
}
3. Restart Claude Desktop for the changes to take effect