This MCP server enables chat assistants to generate images using the Grok-2 model through the Model Context Protocol (MCP). It 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 client application, add the following to your MCP configuration:
{
"mcpServers": {
"grok2_image": {
"command": "npx",
"args": [
"grok2-image-mcp-server"
],
"env": {
"XAIAPI_KEY": "xAI Key"
}
}
}
}
The server can be configured using the following environment variables:
XAIAPI_KEY
(required): Your xAI API key
XAIAPI_BASE_URL
(optional): API proxy URL if the default endpoint (https://api.x.ai/v1
) is inaccessible. Must end with /v1
Example:
XAIAPI_BASE_URL=https://api-proxy.me/xai/v1
IMAGE_PROXY_DOMAIN
(optional): Image proxy domain if the default image server (imgen.x.ai
) is inaccessible
Example:
IMAGE_PROXY_DOMAIN=https://image.proxy.workers.dev
If you encounter issues accessing the generated 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
})
}
https://image.proxy.workers.dev
)IMAGE_PROXY_DOMAIN
environment variable with your custom domainThere 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 > MCP and click "Add new global MCP server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"cursor-rules-mcp": {
"command": "npx",
"args": [
"-y",
"cursor-rules-mcp"
]
}
}
}
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 explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.