grok2api-mcp
Click on "Deploy 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., "@grok2api-mcp search X for recent posts about AI and summarize the main points"
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.
grok2api-mcp
A Node.js/TypeScript-based MCP (Model Context Protocol) Server that provides an interface to the Grok AI API (X/Twitter's AI model). This server runs over stdio and acts as a bridge between MCP clients (like Claude Desktop) and Grok's OpenAI-compatible API endpoints.
Features
X Search - Search and summarize X (Twitter) content using Grok
General Search - General web search and summarization
Image Generation - Generate images via Grok's image API
Video Generation - Generate videos via Grok's video API
Related MCP server: MiniMax MCP JS
Requirements
Node.js >= 18
Grok API access (API token from X.AI)
Installation
Quick Start with npx
npx grok2api-mcpGlobal Installation
npm install -g grok2api-mcp
grok2api-mcpFrom Source
git clone https://github.com/1WorldCapture/grok2api-mcp.git
cd grok2api-mcp
npm install
npm run buildConfiguration
Required Environment Variables
Variable | Description |
| Grok API base URL (e.g., |
| Your Grok API authentication token |
Optional Environment Variables
Variable | Default | Description |
|
| Global request timeout (ms) |
| (global) | Timeout for x_search tool |
| (global) | Timeout for general_search tool |
| (global) | Timeout for image_generation tool |
| (global) | Timeout for video_generation tool |
|
| Model for search tools |
|
| Model for image generation |
|
| Model for video generation |
Base URL Notes
The GROK_BASE_URL supports the following formats:
https://api.example.comhttps://api.example.com/v1https://api.example.com/proxyhttps://api.example.com/proxy/v1
The server automatically handles /v1 path normalization to avoid duplicate path segments.
Usage
Running the Server
GROK_BASE_URL=https://api.x.ai GROK_API_TOKEN=your-token npx grok2api-mcpDevelopment Mode
npm run devIntegration with Claude Desktop
Add the following to your Claude Desktop configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"grok": {
"command": "npx",
"args": ["-y", "grok2api-mcp"],
"env": {
"GROK_BASE_URL": "https://api.x.ai",
"GROK_API_TOKEN": "your-api-token"
}
}
}
}Or with global installation:
{
"mcpServers": {
"grok": {
"command": "grok2api-mcp",
"env": {
"GROK_BASE_URL": "https://api.x.ai",
"GROK_API_TOKEN": "your-api-token"
}
}
}
}MCP Tools
1. x_search
Search and summarize X (Twitter) content.
Model:
grok-4.20-beta(default)Endpoint:
POST /v1/chat/completionsStream: disabled (
stream=false)
2. general_search
Perform general web search and summarization.
Model:
grok-4.20-beta(default)Endpoint:
POST /v1/chat/completionsStream: disabled (
stream=false)
3. image_generation
Generate images using Grok's image generation API.
Model:
grok-imagine-1.0(default)Endpoint:
POST /v1/images/generationsStream: disabled (
stream=false)
4. video_generation
Generate videos using Grok's video generation API.
Model:
grok-imagine-1.0-video(default)Endpoint:
POST /v1/chat/completionsStream: disabled (
stream=false)
Project Structure
grok2api-mcp/
├── src/
│ ├── index.ts # Main entry point - MCP server setup
│ ├── config.ts # Environment variable loading and validation
│ ├── tools.ts # MCP tool definitions and handlers
│ ├── grokClient.ts # Grok API client wrapper
│ ├── httpClient.ts # HTTP client with timeout/abort handling
│ ├── errors.ts # Error types and normalization
│ ├── logger.ts # Simple stderr logger
│ └── utils.ts # Utility functions
├── dist/ # Compiled JavaScript output
├── package.json
├── tsconfig.json
└── README.mdArchitecture
MCP Client (stdio)
│
▼
┌─────────────────────────────────────────┐
│ index.ts - MCP Server │
│ - ListToolsRequestSchema handler │
│ - CallToolRequestSchema handler │
└─────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ tools.ts - Tool Definitions │
│ - Zod schema validation │
│ - x_search, general_search, etc. │
└─────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ grokClient.ts - Grok API Client │
│ - chatCompletions() │
│ - imageGenerations() │
└─────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ httpClient.ts - HTTP Client │
│ - Bearer token auth │
│ - Timeout & AbortController │
│ - URL path normalization │
└─────────────────────────────────────────┘Error Handling
Parameter Validation: All tool arguments are validated with Zod schemas
Unified HTTP Client: Single client handles timeouts, abort signals, and error normalization
Startup Validation: Required environment variables are checked at startup
Rich Error Context: Errors include timeout thresholds, URLs, elapsed time, and hints
SSE Detection: Explicit error if server returns SSE instead of JSON
Development
Scripts
Command | Description |
| Remove dist directory |
| Clean and compile TypeScript |
| Development mode with hot reload |
| Run compiled server |
Build
npm run buildDependencies
@modelcontextprotocol/sdk - Official MCP SDK
zod - Runtime type validation
License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Available Tools
4 toolsgeneral_searchC
Run general web-style search and summarization using grok-4.20-beta.
Inputs: query (required), max_tokens, timeout_ms, temperature.
image_generationC
Generate images via /v1/images/generations using grok-imagine-1.0.
Inputs: prompt (required), n, size, stream, +2 more.
video_generationC
Generate videos via /v1/chat/completions using grok-imagine-1.0-video.
Inputs: prompt (required), seed, size, stream, +4 more.
x_searchC
Search and summarize X (Twitter)-oriented information using grok-4.20-beta.
Inputs: query (required), max_tokens, timeout_ms, temperature.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 4 tools
The four tools cover distinct modalities: X search, general search, image generation, and video generation. The two search tools could be slightly confused when a query spans X and the broader web, but the descriptions make the intended source clear.
All tool names use consistent snake_case with a clear [scope/modality]_[action] pattern: x_search, general_search, image_generation, video_generation. The convention is predictable across the whole set.
Four tools is well-scoped for this API wrapper, with each tool representing a distinct capability. There is no obvious bloat or missing basic operation in the count itself.
The surface covers search plus image/video generation, but there are notable gaps: no general text/chat completion tool and no explicit status or retrieval operation for potentially asynchronous video generation. These gaps could force workarounds for some workflows.
Related MCP Connectors
Connect Grok by xAI to AI assistants via UniversalMCP stateless Streamable HTTP.
Use AI models for chat, image, and video generation from Claude Code and other MCP hosts.
X Ads (Twitter Ads) MCP server: run X campaigns, ad groups, ads and reports from AI.
Live AI-native web search with citations. One tool for every MCP client. Flat per-request pricing.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceEnables generative AI media tasks like image generation, editing, icon creation, and story generation using Google Gemini API through MCP.-
- AlicenseNot gradedqualityAmaintenanceEnables MCP-compatible clients to generate images and videos, synthesize speech, clone and design voices, and dynamically configure API interactions.431 npmMIT
- FlicenseNot gradedqualityAmaintenanceEnables MCP clients to perform AI-powered web searches and analyze images with text prompts for coding-development workflows.-
- FlicenseNot gradedqualityAmaintenanceEnables MCP clients to perform web searches and AI-powered image analysis, returning structured search results and extracting information from images or answering questions about them for development workflows.-