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.
- 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
- Node.js >= 18
- Grok API access (API token from X.AI)
npx grok2api-mcpnpm install -g grok2api-mcp
grok2api-mcpgit clone https://github.com/1WorldCapture/grok2api-mcp.git
cd grok2api-mcp
npm install
npm run build| Variable | Description |
|---|---|
GROK_BASE_URL |
Grok API base URL (e.g., https://api.x.ai) |
GROK_API_TOKEN |
Your Grok API authentication token |
| Variable | Default | Description |
|---|---|---|
GROK_REQUEST_TIMEOUT_MS |
120000 |
Global request timeout (ms) |
GROK_X_SEARCH_TIMEOUT_MS |
(global) | Timeout for x_search tool |
GROK_GENERAL_SEARCH_TIMEOUT_MS |
(global) | Timeout for general_search tool |
GROK_IMAGE_TIMEOUT_MS |
(global) | Timeout for image_generation tool |
GROK_VIDEO_TIMEOUT_MS |
(global) | Timeout for video_generation tool |
GROK_SEARCH_MODEL |
grok-4.20-beta |
Model for search tools |
GROK_IMAGE_MODEL |
grok-imagine-1.0 |
Model for image generation |
GROK_VIDEO_MODEL |
grok-imagine-1.0-video |
Model for video generation |
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.
GROK_BASE_URL=https://api.x.ai GROK_API_TOKEN=your-token npx grok2api-mcpnpm run devAdd 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"
}
}
}
}Search and summarize X (Twitter) content.
- Model:
grok-4.20-beta(default) - Endpoint:
POST /v1/chat/completions - Stream: disabled (
stream=false)
Perform general web search and summarization.
- Model:
grok-4.20-beta(default) - Endpoint:
POST /v1/chat/completions - Stream: disabled (
stream=false)
Generate images using Grok's image generation API.
- Model:
grok-imagine-1.0(default) - Endpoint:
POST /v1/images/generations - Stream: disabled (
stream=false)
Generate videos using Grok's video generation API.
- Model:
grok-imagine-1.0-video(default) - Endpoint:
POST /v1/chat/completions - Stream: disabled (
stream=false)
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.md
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 │
└─────────────────────────────────────────┘
- 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
| Command | Description |
|---|---|
npm run clean |
Remove dist directory |
npm run build |
Clean and compile TypeScript |
npm run dev |
Development mode with hot reload |
npm start |
Run compiled server |
npm run build- @modelcontextprotocol/sdk - Official MCP SDK
- zod - Runtime type validation
Contributions are welcome! Please feel free to submit a Pull Request.