Content
# X (Twitter) MCP Server
A full-featured MCP (Model Context Protocol) server that gives Claude (or any MCP client) complete access to X/Twitter. No paid API needed. Uses your browser session cookies to authenticate as you.
**24 tools** covering: read tweets, search, post, reply, like, retweet, follow/unfollow, DMs (read + send), bookmarks, user lookup, threads, and more.
## How it works
X's web app (x.com) uses an internal GraphQL API for everything. When you're logged in, your browser sends two cookies (`auth_token` and `ct0`) with every request. This tool does the same thing — it sends those cookies with GraphQL requests to read and write on your behalf.
For a few anti-bot-protected endpoints (search, followers, following), raw HTTP doesn't work because Twitter's JS adds computed headers. For those, the tool uses a headless Chromium browser (via Playwright) that loads the real page with your cookies injected and intercepts the GraphQL response.
No OAuth app registration. No API key application. No paid tier. Just two cookies from your browser.
## Setup
### 1. Install dependencies
```bash
pip install mcp httpx python-dotenv playwright
playwright install chromium
```
### 2. Extract your cookies
This is the key step. You need two values from your browser.
1. Open **x.com** in Chrome/Firefox/Edge and make sure you're **logged in**
2. Open **DevTools** (F12 or right-click -> Inspect)
3. Go to **Application** tab (Chrome) or **Storage** tab (Firefox)
4. In the left sidebar, expand **Cookies** -> click **https://x.com**
5. Find and copy these two cookies:
| Cookie | What it looks like | Example |
|--------|-------------------|---------|
| `auth_token` | 40-character hex string | `a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2` |
| `ct0` | Long hex string (~96+ chars) | `abcdef0123456789abcdef0123456789abcdef01...` |
**Alternative method** (if you prefer the Network tab):
1. Open DevTools -> **Network** tab
2. Browse around on x.com (click on a tweet, etc.)
3. Click any request to `x.com/i/api/graphql/...`
4. Look at the **Request Headers**:
- `Cookie:` header contains `auth_token=YOUR_TOKEN; ct0=YOUR_CT0`
- `X-CSRF-Token:` header contains the same value as `ct0`
### 3. Create your .env file
```bash
cp .env.example .env
```
Edit `.env` and paste your values:
```
X_AUTH_TOKEN=your_auth_token_here
X_CT0=your_ct0_here
MY_USER_ID=your_numeric_user_id_here
```
**Finding your user ID** (needed for DMs and "my timeline"):
- Open x.com, open DevTools console, run: `document.cookie.match(/twid=u%3D(\d+)/)?.[1]`
- Or just use the tool first without it, run `x_user_lookup` on your own handle, and grab the `id` field from the result.
### 4. Add to Claude Code
Add this to your Claude Code settings (`~/.claude/settings.json` under `mcpServers`):
```json
{
"mcpServers": {
"x-api": {
"command": "python3",
"args": ["/full/path/to/x_mcp.py"],
"type": "stdio"
}
}
}
```
Replace `/full/path/to/x_mcp.py` with the actual absolute path to the file.
Then restart Claude Code. You should see the x-api tools available.
### 5. Test it
Ask Claude: "Look up the user @elonmusk" or "Search for tweets about AI agents".
## Available tools
### Read
| Tool | Description |
|------|-------------|
| `x_me` | Get your own profile info |
| `x_get_tweet` | Get a single tweet by ID |
| `x_user_lookup` | Look up user by @handle |
| `x_user_lookup_by_id` | Look up user by numeric ID |
| `x_timeline` | Get a user's recent tweets |
| `x_search` | Search tweets (supports X search operators) |
| `x_mentions` | Get recent mentions |
| `x_followers` | Get a user's followers |
| `x_following` | Get who a user follows |
| `x_conversation` | Get replies in a thread |
### Write
| Tool | Description |
|------|-------------|
| `x_post_tweet` | Post a new tweet |
| `x_reply` | Reply to a tweet |
| `x_quote_tweet` | Quote-tweet with your comment |
| `x_delete_tweet` | Delete one of your tweets |
| `x_post_thread` | Post a multi-tweet thread |
### Engagement
| Tool | Description |
|------|-------------|
| `x_like` / `x_unlike` | Like or unlike a tweet |
| `x_retweet` / `x_unretweet` | Retweet or undo |
| `x_follow` / `x_unfollow` | Follow or unfollow a user |
### DMs
| Tool | Description |
|------|-------------|
| `x_get_dms` | Get recent DM conversations |
| `x_dm_inbox_all` | Paginate entire inbox (trusted + requests) |
| `x_dm_search` | Search DM contacts by handle/name |
| `x_get_dm_conversation` | Full message history for a conversation |
| `x_send_dm` | Send a DM to a user |
### Bookmarks
| Tool | Description |
|------|-------------|
| `x_bookmarks` | Get your bookmarks |
| `x_bookmark` / `x_remove_bookmark` | Add/remove bookmark |
## Maintenance
### Cookie expiration
Your cookies will expire periodically (usually every few weeks to months). When they do, you'll see 403 errors or "Cookies may be expired" messages. Just re-extract them from your browser and update `.env`.
### GraphQL Query ID rotation
Twitter changes their internal GraphQL query IDs every 2-4 weeks. When they go stale, you'll get 400 or 403 errors mentioning "Query ID may be stale."
To get fresh IDs:
1. Open x.com in your browser with DevTools Network tab open
2. Filter network requests by `graphql`
3. Browse around (view tweets, search, view profiles) to trigger the operations you need
4. Each request URL looks like: `x.com/i/api/graphql/QUERY_ID/OperationName`
5. Copy the new query IDs and update the `QUERY_IDS` dict in `x_mcp.py`
You can also check https://github.com/fa0311/TwitterInternalAPIDocument for community-tracked IDs.
### Rate limiting
The tool has built-in 1-second throttling between requests to avoid Cloudflare blocks. If you're getting blocked anyway, increase the sleep time in `_throttle()`.
## How this was built
The approach for building an MCP server for any website:
1. **Open the site in your browser with DevTools Network tab open**
2. **Use the site normally** — click around, search, post, etc.
3. **Watch the network requests** — filter by XHR/Fetch to see the API calls
4. **Identify the auth mechanism** — look at cookies, headers, and tokens in requests
5. **Replay the requests** with the same headers/cookies using httpx or requests
6. **For anti-bot endpoints** that need browser JS — use Playwright to load the page with injected cookies and intercept the responses
7. **Parse the responses** — the JSON structure is usually nested but consistent
8. **Wrap it in FastMCP** — each API operation becomes a `@mcp.tool()` function
This same pattern works for basically any website. The key insight: every site's frontend is just making API calls. You can make the same calls.