A Model Context Protocol (MCP) server that provides AI assistants with access to Grok 4.5 capabilities including chat completions, web search (via Responses API), and model management.
- Chat Completions: Interact with Grok 4.5 for conversational AI tasks
- Web Search: Real-time web (and X) search with Responses API
web_search/x_searchtools, modern options (image understanding/search), plus legacysearch_parameterscompatibility for thegrok_searchtool - Multi-Model Support: Defaults to Grok 4.5 and supports Grok 4.3/grok-latest legacy aliases, Grok Build 0.1, and Grok Imagine/Voice APIs
- Rate Limiting: Built-in request throttling and circuit breaker patterns
- Caching: Intelligent response caching for improved performance
- Metrics: Prometheus metrics for monitoring and observability
- Security: Input validation, sanitization, and secure configuration
| Model | Context Window | Tier 0 TPM | Tier 0 RPS | Input Price | Output Price | Use Case |
|---|---|---|---|---|---|---|
| grok-4.5 | 500,000 | 50M | 150 | $2.00/M | $6.00/M | Default for code, chat, reasoning, and tool use |
| grok-4.5-latest | 500,000 | 50M | 150 | $2.00/M | $6.00/M | Latest Grok 4.5 alias |
| grok-4.3 | 1,000,000 | 10M | 37 | $1.25/M | $2.50/M | Legacy redirect target for retired text slugs |
| grok-latest | 1,000,000 | 10M | 37 | $1.25/M | $2.50/M | Legacy alias for Grok 4.3 |
| grok-4.20 | 1,000,000 | 10M | 37 | N/A | N/A | Legacy reasoning and general chat |
| grok-build-0.1 | 256K | 10M | 37 | $1.00/M | $2.00/M | Code generation and agentic workflows |
| grok-imagine-image | N/A | N/A | N/A | N/A | N/A | Image generation |
| grok-voice-think-fast-1.0 | N/A | N/A | N/A | N/A | N/A | Voice workflows |
- Node.js >= 18.0.0
- npm or yarn
- xAI API key
- Clone the repository:
git clone <repository-url>
cd grok-4-mcp-server- Install dependencies:
npm install- Configure environment variables:
cp .env.example .envrcEdit .envrc with your configuration:
export XAI_API_KEY="your-xai-api-key-here"
export SHARED_SECRET="your-shared-secret-here"
export GROK_MODEL="grok-4.5"
export GROK_BASE_URL="https://api.x.ai/v1" # Optional; accepts https://api.x.ai and appends /v1 (XAI_BASE_URL also accepted)
export GROK_TEMPERATURE="0.7" # Optional, 0.0-1.0
export GROK_MAX_TOKENS="4000" # Optional
export MCP_SERVER_NAME="grok-4-mcp-server" # Optional
export MCP_SERVER_VERSION="1.0.0" # Optional- Load environment variables:
direnv allow # If using direnv
# or
source .envrc- Build the project:
npm run buildnpm startnpm run devThe server implements the Model Context Protocol and can be integrated with any MCP-compatible client. It exposes the following tools:
grok_ask: Ask Grok a question with optional context and web search contextgrok_chat: Multi-turn conversations with Grokgrok_search: Web (and optional X) search powered by Responses APIweb_search/x_searchtools, with image understanding/search support and legacy compatgrok_x_search: Dedicated X/Twitter search via the x_search toolgrok_ask/grok_chat: Supportinclude_search+ modern flags for injecting search context (with images/X)grok_models: List available Grok modelsgrok_test_connection: Test API connectivitygrok_health: Server health check and metrics
{
"mcpServers": {
"grok": {
"command": "node",
"args": ["dist/index.js"],
"env": {
"XAI_API_KEY": "your-key-here",
"SHARED_SECRET": "your-shared-secret-here"
}
}
}
}| Variable | Default | Description |
|---|---|---|
XAI_API_KEY |
Required | Your xAI API key |
SHARED_SECRET |
Required | Shared secret used to authenticate MCP requests |
GROK_MODEL |
grok-4.5 |
Default model to use |
GROK_BASE_URL |
https://api.x.ai/v1 |
API endpoint URL (accepts https://api.x.ai and appends /v1; XAI_BASE_URL also accepted) |
GROK_TEMPERATURE |
0.7 |
Response creativity (0.0-1.0) |
GROK_MAX_TOKENS |
4000 |
Maximum response tokens |
LOG_LEVEL |
info |
Logging verbosity |
NODE_ENV |
production |
Environment mode |
GROK_TIMEOUT_MS |
60000 |
Request timeout for chat completions (milliseconds) |
GROK_SEARCH_TIMEOUT_MS |
120000 |
Request timeout for live search / Responses API calls (milliseconds) |
GROK_RETRIES |
2 |
Number of retries for transient failures (network errors, 5xx, 429) |
GROK_RETRY_DELAY_MS |
1000 |
Base delay between retries; doubles with each attempt (milliseconds) |
GROK_MAX_CONCURRENT |
2 |
Maximum concurrent requests to the xAI API |
GROK_MIN_TIME_MS |
500 |
Minimum interval between requests to the xAI API (milliseconds) |
The server includes built-in resilience features:
- Configurable Timeouts: Separate timeouts for chat and search/Responses API calls.
- Retries with Backoff: Transient failures (network errors, 5xx, 429) are retried up to
GROK_RETRIEStimes with exponential backoff. - Rate Limiting: Configurable concurrency and minimum interval (
GROK_MAX_CONCURRENT,GROK_MIN_TIME_MS). - Caching: 5-minute TTL LRU cache for responses.
- Connection Pooling: HTTP agent with keep-alive connections.
npm test
npm run test:watchnpm run lint
npm run lint:fixnpm run type-checknpm run build
npm run clean- API Key Protection: Never commit API keys to version control
- Input Validation: All inputs are validated and sanitized
- Rate Limiting: Prevents abuse and ensures fair usage
- Error Handling: Sensitive information is not exposed in error messages
- Logging: Configurable log levels prevent sensitive data leakage
The server exposes Prometheus metrics at /metrics (when health endpoint is called):
- Request latency histograms
- Request counters by tool
- Error counters
- Cache hit/miss ratios
- "API key not found": Ensure
XAI_API_KEYis set in your environment - "Connection timeout" / request timeouts: Increase
GROK_TIMEOUT_MS(default 60s) for long reasoning requests andGROK_SEARCH_TIMEOUT_MS(default 120s) for live search. Also check network connectivity and the API endpoint URL. - "Rate limit exceeded": Implement client-side rate limiting or increase intervals
- "Model not available": Verify the model name is correct and supported; retired slugs now alias to current models
Enable verbose logging:
export LOG_LEVEL=debug
export NODE_ENV=developmentTest server connectivity:
curl -X POST localhost:3000/health- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
- Follow TypeScript best practices
- Add comprehensive tests
- Update documentation for API changes
- Use conventional commit messages
- Ensure compatibility with Node.js >= 18
MIT License - see LICENSE file for details.
- Issues: GitHub Issues
- Documentation: Full Docs
- Community: [Discord/Slack]
- Initial release with Grok 4 support
- Updated default model to Grok 4.5
- MCP protocol implementation
- Multi-model support
- Comprehensive error handling and monitoring
Built with ❤️ for the AI community