The xAI MCP Server integrates xAI's Grok APIs into Claude Code, enabling AI-powered content generation and analysis through natural language prompts.
Generate Images (
generate_image): Create images from text descriptions using Grok Imagine, with options for number of images (1-10), aspect ratio, and output format (URL or base64)Chat with Grok (
chat): Interact with Grok language models (grok-3, grok-4, grok-3-mini) with configurable system prompts, temperature, and token limitsAnalyze Images (
analyze_image): Use Grok's vision capabilities to describe images, extract text, or answer questions via URL or base64 dataLive Web Search (
live_search): Perform real-time searches across the web, news, and X/Twitter with optional date range filtering and configurable result countGenerate Videos (
generate_video): Create videos from text prompts, animate existing images, or edit videos, with control over duration (1-15s) and aspect ratio
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@xAI MCP ServerSearch for the latest news about SpaceX launches"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
xAI MCP Server
A Model Context Protocol (MCP) server that brings xAI's Grok APIs to Claude Code. Generate images, chat with Grok, analyze images, search the web, and create videos—all from natural language prompts in your Claude Code session.
Features
Tool | Description |
| Generate images using Grok Imagine |
| Chat with Grok models (grok-3, grok-4, grok-3-mini) |
| Analyze and describe images with Grok Vision |
| Real-time web, news, and X/Twitter search |
| Generate videos from text prompts |
Prerequisites
Node.js 18.0.0 or higher
xAI API Key from x.ai/api
Claude Code installed
Installation
Option 1: Quick Install (Recommended)
curl -fsSL https://raw.githubusercontent.com/joemccann/xai-mcp-server/main/install.sh | bashThis installs the server and automatically configures Claude Code. You'll be prompted for your xAI API key.
Option 2: npx from GitHub
No installation needed - runs directly from GitHub:
npx github:joemccann/xai-mcp-serverOption 3: npm Global Install
npm install -g @joemccann/xai-mcp-serverOption 4: Clone and Build
git clone https://github.com/joemccann/xai-mcp-server.git
cd xai-mcp-server
npm installConfiguration for Claude Code
Step 1: Get Your xAI API Key
Go to x.ai/api
Sign up or log in
Create an API key
Copy the key (starts with
xai-)
Step 2: Configure Claude Code
Note: If you used the Quick Install (Option 1), this is already done for you.
Add the MCP server using the Claude CLI:
claude mcp add xai -e XAI_API_KEY=xai-your-key-here -- node ~/.xai-mcp-server/dist/index.jsFor nvm users, use the absolute path to node:
claude mcp add xai -e XAI_API_KEY=xai-your-key-here -- $(which node) ~/.xai-mcp-server/dist/index.jsVerify it's configured:
claude mcp listYou should see:
xai: ... - ✓ ConnectedStep 3: Restart Claude Code
Restart Claude Code to load the new MCP server. You should see the xAI tools available.
Usage
Once configured, you can use natural language to invoke xAI capabilities:
Image Generation
Generate an image of a cyberpunk cityscape at night with neon lights.Using grok imagine, create a watercolor painting of a mountain landscape.Generate 3 variations of a logo for a coffee shop called "Bean There".Chat with Grok
Ask Grok to explain the theory of relativity in simple terms.Have Grok write a haiku about programming.Image Analysis
Analyze this image and describe what you see: https://example.com/photo.jpgWhat text is visible in this screenshot: [image URL]Live Search
Search for the latest news about SpaceX launches.Find recent tweets about the new iPhone release.Search the web for Python best practices 2024.Video Generation
Generate a 5-second video of clouds moving across a blue sky.Create a video animation of a bouncing ball.Tool Reference
generate_image
Generate images from text descriptions using Grok Imagine.
Parameter | Type | Required | Default | Description |
| string | Yes | - | Text description of the image |
| number | No | 1 | Number of images (1-10) |
| string | No |
| Image generation model |
| string | No | - | Aspect ratio (e.g., "16:9", "1:1", "4:3") |
| string | No |
| Output format: "url" or "b64_json" |
Example Response:
{
"success": true,
"images": [
{
"index": 1,
"url": "https://api.x.ai/images/generated/abc123.png",
"revised_prompt": "A detailed cyberpunk cityscape..."
}
]
}chat
Chat with Grok language models.
Parameter | Type | Required | Default | Description |
| string | Yes | - | Message to send to Grok |
| string | No |
| Model: grok-3, grok-4, grok-3-mini |
| string | No | - | System context/instructions |
| number | No | 0.7 | Sampling temperature (0-2) |
| number | No | - | Maximum response tokens |
analyze_image
Analyze images using Grok's vision capabilities.
Parameter | Type | Required | Default | Description |
| string | Yes | - | Image URL or base64 data URL |
| string | No | "Describe this image" | Question or instruction |
| string | No |
| Detail level: "low", "high", "auto" |
| string | No |
| Vision model |
live_search
Perform real-time web searches using Grok.
Parameter | Type | Required | Default | Description |
| string | Yes | - | Search query |
| array | No |
| Sources: "web", "news", "x" |
| object | No | - | Date filter: |
| number | No | 10 | Maximum results (1-20) |
generate_video
Generate videos from text descriptions.
Parameter | Type | Required | Default | Description |
| string | Yes | - | Video description |
| string | No |
| Video generation model |
| number | No | 5 | Duration in seconds (1-15) |
| string | No | - | Input image URL to animate |
| string | No | - | Input video URL to edit |
| string | No | - | Aspect ratio (e.g., "16:9") |
| boolean | No | true | Wait for video to finish |
Development
# Install dependencies
npm install
# Build TypeScript
npm run build
# Watch mode (rebuild on changes)
npm run dev
# Run the server directly
npm startTesting
This project includes comprehensive unit tests with pretty table output:
# Run unit tests (mocked, no API calls)
npm run test
# Run tests with detailed output (shows every individual test)
npm run test:detailed
# Run tests in watch mode
npm run test:watch
# Run tests with coverage report
npm run test:coverage
# Run integration tests (requires XAI_API_KEY, makes real API calls)
npm run test:integrationTest Output Features:
Color-coded results with status indicators (✓ PASS, ✗ FAIL)
Performance metrics showing slowest/fastest test files
Summary table with total stats
Detailed mode shows individual test durations and suite hierarchy
All tests use mocked APIs by default (no cost, no API key needed)
Project Structure
xai-mcp-server/
├── src/
│ ├── index.ts # MCP server entry point
│ ├── xai-client.ts # xAI API client with types
│ └── tools/
│ ├── generate-image.ts
│ ├── chat.ts
│ ├── vision.ts
│ ├── live-search.ts
│ └── generate-video.ts
├── dist/ # Compiled JavaScript
├── package.json
├── tsconfig.json
└── README.mdTroubleshooting
"XAI_API_KEY environment variable is required"
Re-add the MCP server with your API key:
claude mcp remove xai
claude mcp add xai -e XAI_API_KEY=xai-your-key-here -- $(which node) ~/.xai-mcp-server/dist/index.jsTools not appearing in Claude Code
Run
claude mcp listto check server statusIf not listed, add it with
claude mcp add(see Step 2 above)For nvm users, use absolute node path:
$(which node)Ensure the project is built (
npm run build)Restart Claude Code completely
API errors
Verify your API key is valid at x.ai
Check you have sufficient API credits
Some features may require specific API tier access
API Reference
This server uses the xAI API. For full API documentation, see:
License
MIT
Contributing
Contributions welcome! Please open an issue or submit a pull request.
Acknowledgments
xAI for the Grok API
Model Context Protocol for the MCP specification
Anthropic for Claude Code