npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

grok2api-mcp

v1.0.0

Published

MCP server for Grok AI API integration - supports X search, web search, image and video generation

Readme

grok2api-mcp

npm version License: ISC Node.js Version

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

Requirements

  • Node.js >= 18
  • Grok API access (API token from X.AI)

Installation

Quick Start with npx

npx grok2api-mcp

Global Installation

npm install -g grok2api-mcp
grok2api-mcp

From Source

git clone https://github.com/1WorldCapture/grok2api-mcp.git
cd grok2api-mcp
npm install
npm run build

Configuration

Required Environment Variables

| Variable | Description | |----------|-------------| | GROK_BASE_URL | Grok API base URL (e.g., https://api.x.ai) | | GROK_API_TOKEN | Your Grok API authentication token |

Optional Environment Variables

| 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 |

Base URL Notes

The GROK_BASE_URL supports the following formats:

  • https://api.example.com
  • https://api.example.com/v1
  • https://api.example.com/proxy
  • https://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-mcp

Development Mode

npm run dev

Integration 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/completions
  • Stream: disabled (stream=false)

2. general_search

Perform general web search and summarization.

  • Model: grok-4.20-beta (default)
  • Endpoint: POST /v1/chat/completions
  • Stream: disabled (stream=false)

3. image_generation

Generate images using Grok's image generation API.

  • Model: grok-imagine-1.0 (default)
  • Endpoint: POST /v1/images/generations
  • Stream: 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/completions
  • Stream: 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.md

Architecture

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 | |---------|-------------| | 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 |

Build

npm run build

Dependencies

License

ISC

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.