Grok MCP Server: xAI Image & Web Search
Explore the MCP server for xAI's Grok models with image analysis, generation, and web search.
npx -y @merterbak/Grok-MCPOverview
This MCP (Model Context Protocol) server provides a lightweight bridge between xAI’s Grok family of models and external tools for image analysis, image generation, and live web search. It implements a simple API that follows MCP conventions so agent frameworks and developer tools can call Grok models with structured context and tool invocations — for example, asking a model to analyze an image, generate a new image, or augment responses with web search results.
For developers building multimodal assistants, research prototypes, or content pipelines, the Grok MCP server centralizes model I/O, tool orchestration, and configuration. It abstracts the low-level details of calling the xAI endpoint (keys, payload formats) and exposes consistent endpoints and payload shapes that are easy to integrate with agents, UIs, or automation scripts.
Features
- MCP-compliant API surface for invoking Grok models with context and tools
- Image analysis: object/scene description, captioning, and structured annotations
- Image generation: instruct-driven image synthesis (prompt → image)
- Integrated web search tool to retrieve recent web content and citations
- Simple configuration via environment variables or .env files
- Docker-friendly — run locally or in cloud environments
- Example request/response patterns suitable for agent integration
Installation / Configuration
Prerequisites:
- Python 3.10+ (recommended)
- Git
- An API key for xAI Grok (set as XAI_API_KEY). If using web search, set a SEARCH_API_KEY for your search provider.
Typical setup:
# Clone the repository
# Create and activate a virtual environment
# Install dependencies
Environment variables (example .env):
# .env
XAI_API_KEY=sk-xxxxx
MCP_HOST=0.0.0.0
MCP_PORT=8080
ENABLE_WEB_SEARCH=true
SEARCH_API_KEY=search-xxxx
Run the server (example using uvicorn):
# If the repo exposes an ASGI app in app.main:app
Docker example:
# Build and run with env-file
Available Resources
The server exposes a set of REST endpoints that map to MCP-style tool calls. Typical endpoints include:
| Endpoint | Purpose |
|---|---|
| POST /mcp/v1/invoke | Generic MCP invocation: pass model name, context, and requested tools |
| POST /mcp/v1/image/analyze | Send an image (URL or base64) for analysis/captioning |
| POST /mcp/v1/image/generate | Request image generation from prompt and parameters |
| POST /mcp/v1/search | Perform a web search and return top results & snippets |
Payloads follow JSON conventions. Images can be provided as public URLs or as base64-encoded data within the request body depending on the endpoint’s configuration.
Use Cases
- Image content analysis for moderation
- Scenario: A platform needs to classify uploaded images for policy compliance.
- Example curl:
Expected: A structured JSON response with captions, detected objects and confidence scores.
- Image-generation assistant
- Scenario: A chatbot asks Grok to generate an image from a user prompt.
- Example payload:
Call POST /mcp/v1/image/generate and receive a base64 image or a temporary URL.
- Augmenting answers with web search
- Scenario: The assistant needs up-to-date facts or citations.
- Example curl:
Response includes top snippets and source URLs that the Grok model can use when constructing answers.
Integration Tips
- Treat the MCP server as a middleware: let your agent or application send a single MCP invocation that describes model input + requested tools and then handle the structured response.
- For large volumes or production usage, run behind a process manager (systemd, Kubernetes) and enable TLS; keep API keys secure via secret stores.
- Cache web-search results when feasible to reduce external API costs and latency.
For code samples, refer to the repository README and example clients in the examples/ folder (GitHub: https://github.com/merterbak/Grok-MCP).