Skip to main content
Glama
README.md
# grok-x-search-mcp

A free, open-source [Model Context Protocol (MCP)](https://modelcontextprotocol.io) server that gives Claude real-time access to X (Twitter) through the [xAI Grok API](https://x.ai/api). Search posts, analyze sentiment, track accounts, and cross-reference X with web sources — all from within Claude.

> **Free to use.** This MCP server is open-source with no fees. You only pay [xAI directly](https://x.ai/api) for API usage — roughly **$0.005–$0.01 per search**.

---

## Why not just use the X API?

The traditional X (Twitter) API has become prohibitively expensive for most developers:

| Approach              | Monthly Cost     | What you get                                  |
| --------------------- | ---------------- | --------------------------------------------- |
| X API Free            | $0               | Write-only. No read access.                   |
| X API Basic           | **$200/month**   | 15,000 post reads                             |
| X API Pro             | **$5,000/month** | 1M post reads                                 |
| X API Enterprise      | **$42,000+/mo**  | Custom limits                                 |
| **xAI Grok x_search** | **$0 minimum**   | Pay $0.005 per search. No subscription floor. |

On top of the cost, the X API requires a developer application approval process, OAuth setup, and careful rate-limit management.

**With this MCP server**, you skip all of that. Grok's `x_search` tool has privileged, real-time access to X posts and threads built in. One xAI API key, one `npm install`, and you're querying X through Claude in minutes.

---

## Tools

| Tool            | Description                                                                                                                                |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `search_x`      | Search X for posts, threads, and media on any topic. Returns summary, key themes, verbatim notable posts, and optional sentiment analysis. |
| `web_plus_x`    | Cross-reference what X is saying with web sources. Returns both sides plus a synthesis of where they agree or contradict.                  |
| `track_account` | Deep-dive a specific X account — dominant topics, tone, and verbatim recent posts.                                                         |
| `monitor_x`     | Stateful monitoring — call repeatedly to track only _new_ posts and sentiment shifts since last check.                                     |

---

## Prerequisites

- **Node.js** >= 22
- **Claude Desktop** or any MCP-compatible client
- **xAI API key** — get one at [console.x.ai](https://console.x.ai)

---

## Setup

### 1. Clone and install

```bash
git clone https://github.com/0xnicholasy/grok-x-search-mcp.git
cd grok-x-search-mcp
npm install
```

### 2. Configure environment

```bash
cp .env.example .env
```

Edit `.env` and set your API key:

```env
XAI_API_KEY=xai-your-api-key-here
GROK_MODEL=grok-4-1-fast
MAX_RETRIES=3
REQUEST_TIMEOUT_MS=45000
LOG_LEVEL=info
```

### 3. Build

```bash
npm run build
```

### 4. Register with Claude

**Claude Desktop** — add to your `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "grok-x-search": {
      "command": "node",
      "args": ["/absolute/path/to/grok-x-search-mcp/dist/server.js"]
    }
  }
}
```

**Claude Code (CLI)** — run once:

```bash
claude mcp add --scope user grok-x-search node /absolute/path/to/grok-x-search-mcp/dist/server.js
```

**Cursor / Windsurf / other MCP clients** — add to your MCP config:

```json
{
  "mcpServers": {
    "grok-x-search": {
      "command": "node",
      "args": ["/absolute/path/to/grok-x-search-mcp/dist/server.js"]
    }
  }
}
```

Restart your client after registering. Verify with `/mcp` (Claude Desktop) or `claude mcp list` (Claude Code CLI).

---

## Usage Examples

Once registered, just talk to Claude:

```
Search X for what people are saying about NVIDIA earnings this week
```

```
What is the X sentiment on Bitcoin today? Show me verbatim posts.
```

```
Track @sama's posts this week — what topics is he focused on?
```

```
Fact-check: is the claim that "inflation is rising" on X supported by web sources?
```

```
Monitor X for "GPT-5" — I'll call this again later to see new posts
```

---

## API Cost

This MCP server is **free and open-source**. The only costs are xAI API charges billed directly to your account:

| Component                     | Cost                           |
| ----------------------------- | ------------------------------ |
| `x_search` invocation         | $5.00 / 1k calls ($0.005 each) |
| `web_search` invocation       | $5.00 / 1k calls ($0.005 each) |
| Input tokens (grok-4-1-fast)  | $0.20 / 1M                     |
| Output tokens (grok-4-1-fast) | $0.50 / 1M                     |

A typical `search_x` call costs roughly **$0.005–$0.01** total. New xAI accounts get **$25 in free credits** to start.

Compare that to the X API Basic tier at $200/month minimum — just to get read access.

---

## Configuration

| Variable             | Default         | Description                                              |
| -------------------- | --------------- | -------------------------------------------------------- |
| `XAI_API_KEY`        | _(required)_    | Your xAI API key                                         |
| `GROK_MODEL`         | `grok-4-1-fast` | Model to use — see [xAI model pricing](https://x.ai/api) |
| `MAX_RETRIES`        | `3`             | Retry attempts on 429/5xx errors (max 5)                 |
| `REQUEST_TIMEOUT_MS` | `45000`         | Request timeout in ms (5000–120000)                      |
| `LOG_LEVEL`          | `info`          | Log verbosity: `debug`, `info`, `warn`, `error`          |

### Choosing a model

`grok-4-1-fast` is the recommended default — it is both the cheapest text model and delivers grok-4 quality. The dominant cost is the `x_search` tool invocation ($0.005/call), not tokens.

For maximum accuracy use `grok-4-0709`, but expect 30x higher token costs.

---

## Development

```bash
# Run with hot-reload (no build needed)
npm run dev

# Interactive tool inspector
npm run inspect

# Run unit tests
npm run test:unit

# Run all tests (requires XAI_API_KEY in .env for integration tests)
npm test

# Type-check without building
npm run lint
```

The `monitor_x` tool persists state to `.monitor-state.json` in the project root (excluded from git). Delete this file to reset all monitoring history.

---

## Project Structure

```
src/
  server.ts             # MCP server — tool definitions and request handlers
  grokClient.ts         # xAI Responses API client with retry + timeout
  sentimentAnalyzer.ts  # Prompt builder and response parser for search_x
  monitorState.ts       # State persistence and post deduplication for monitor_x
  config.ts             # Environment config with Zod validation
  types.ts              # Zod schemas and TypeScript types
  utils.ts              # Date range helpers
  __tests__/
    unit/               # Unit tests (no API key needed)
    integration/        # Integration tests (requires XAI_API_KEY)
```

---

## License

MIT

TDQS

A3.7/5.0

Scored across 4 tools

Disambiguation4/5

Each tool has a distinguishable angle: search_x is a one-off topical search, monitor_x is stateful delta tracking, track_account targets a single account, and web_plus_x adds cross-referencing with web sources. search_x and monitor_x overlap on 'query X for posts,' but the descriptions make the stateful/one-shot distinction reasonably clear.

Naming Consistency4/5

All names are snake_case and mostly verb-led (search_x, monitor_x, track_account). The only deviation is web_plus_x, which encodes a combination rather than an action, though it remains readable in context.

Tool Count4/5

Four tools is lean but defensible for a focused X research server, and each covers a genuinely different workflow (search, cross-reference, account analysis, ongoing monitoring). It sits at the low end without feeling padded or arbitrary.

Completeness3/5

Core discovery and monitoring are covered, but there is no way to retrieve a specific post or thread by ID/URL, list an account's raw timeline, or stop/clear an existing monitor. For a research workflow those are notable gaps an agent could hit when a user pastes a link or asks to end tracking.

Maintenance

ActivityInactive
ResponsivenessNo recent community issues