A Model Context Protocol (MCP) server that provides AI assistants with access to Grok 4's capabilities including chat completions, live search, and model management.
- Chat Completions: Interact with Grok 4 for conversational AI tasks
- Live Search: Real-time web search with structured results
- Multi-Model Support: Supports grok-4-1-fast-reasoning, grok-4-1-fast-non-reasoning, and grok-code-fast-1
- 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 | TPM | RPM | Input Price | Output Price | Use Case |
|---|---|---|---|---|---|---|
| grok-4-1-fast-reasoning | 2,000,000 | 4M | 480 | $0.20/M tokens | $0.50/M tokens | General reasoning tasks |
| grok-4-1-fast-non-reasoning | 2,000,000 | 4M | 480 | $0.20/M tokens | $0.50/M tokens | Fast, direct responses |
| grok-code-fast-1 | N/A | N/A | N/A | N/A | N/A | Code generation and analysis |
- 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 GROK_MODEL="grok-4-1-fast-reasoning" # or grok-4-1-fast-non-reasoning, grok-code-fast-1
export GROK_BASE_URL="https://api.x.ai/v1" # Optional, defaults provided
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 searchgrok_chat: Multi-turn conversations with Grokgrok_search: Live web search functionalitygrok_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"
}
}
}
}| Variable | Default | Description |
|---|---|---|
XAI_API_KEY |
Required | Your xAI API key |
GROK_MODEL |
grok-4-1-fast-reasoning |
Default model to use |
GROK_BASE_URL |
https://api.x.ai/v1 |
API endpoint URL |
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 |
The server includes built-in resilience features:
- Rate Limiting: 2 concurrent requests, 500ms minimum interval
- Circuit Breaker: Automatic failure handling with fallback
- 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": Check network connectivity and 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
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
- MCP protocol implementation
- Multi-model support
- Comprehensive error handling and monitoring
Built with ❤️ for the AI community