An MCP server that lets Claude read public tweets from X.com by URL or tweet ID. Designed for occasional research use with X's pay-as-you-go billing — no monthly subscription required.
- Python 3.14+
- uv
- An X API Bearer Token (see below)
- Go to https://developer.x.com and sign in with your X account
- Accept the Developer Agreement and Policy
- You should see a default app already available on your dashboard
- Go to your app's Keys and Tokens section
- Generate a Bearer Token (App-only token) — this is all you need for reading public tweets
- Copy it — you'll only see it once
- In the Developer Console (https://console.x.com/), go to Billing
- Add a payment method and purchase initial credits ($5 is plenty)
- Set a spending cap of $1–2/month to prevent surprises
Each tweet read costs roughly $0.005, so $5 in credits lasts a very long time at casual usage.
mkdir -p ~/.config/x-api
echo "X_BEARER_TOKEN=your-bearer-token-here" > ~/.config/x-api/.env
chmod 600 ~/.config/x-api/.envReplace your-bearer-token-here with your actual token.
Any agent that can read ~/.config/x-api/.env can read the token. To keep it out of reach, store it in a broker that adds the Authorization header itself (for example the secretbroker in rafazaya/dotfiles), then point the app-only calls at it instead of setting a token:
echo "X_API_BASE=http://cachyminisforum:7480/x" >> ~/.config/x-api/.envWith X_API_BASE set and X_BEARER_TOKEN absent, reads and searches go through the broker and this server never holds the token. Bookmarks still use the per-account OAuth tokens below and go to X directly.
git clone https://github.com/rafazaya/x-api.git
cd x-api
uv syncAdd the following to your .mcp.json (in your project root or ~/.claude/.mcp.json for global access):
{
"mcpServers": {
"x-api": {
"type": "stdio",
"command": "uv",
"args": [
"run",
"--directory",
"/path/to/x-api",
"python",
"-m",
"x_api.server"
]
}
}
}Replace /path/to/x-api with the absolute path to your cloned repo.
Once configured, Claude can:
Read tweets by URL or ID:
https://x.com/user/status/123456789123456789
Search recent tweets (last 7 days) by topic or keyword:
"Python asyncio""from:karpathy -is:retweet"
Search the full tweet archive by topic or keyword:
"from:kepano Obsidian -is:retweet"- Use optional
start_timeandend_timeISO 8601 bounds for narrower searches - Use sparingly; this calls X's full-archive search endpoint and may be more expensive than recent search
These tools return the tweet's author, text, date, and engagement stats (likes, retweets, replies, quotes).
List bookmarks for an authorized account (see below):
list_bookmarks_tool(account="<username>", max_results=20)accountcan be omitted when only one account is authorized
The bearer token can't read bookmarks; X requires a token issued for the account itself. Any number of accounts can authorize the same app.
In the developer console, open your app → User authentication settings → Set up:
- App permissions: Read
- Type of App: Native App (public client, PKCE, no client secret)
- Callback URI:
http://127.0.0.1:8723/callback - Website URL: anything, e.g. this repo
Save, then add the Client ID to the env file:
echo "X_CLIENT_ID=your-client-id" >> ~/.config/x-api/.envuv run python -m x_api.auth login # prints a URL; open it signed into the account
uv run python -m x_api.auth list # show authorized accountsThe browser must be signed into the account you want to authorize. Repeat per account, switching accounts (or using a private window) in between. Tokens are saved to ~/.config/x-api/tokens/<username>.json (mode 600).
The callback goes to 127.0.0.1 on the machine running the browser, so if the browser is on another machine, tunnel the port first:
ssh -L 8723:127.0.0.1:8723 user@serverAccess tokens last 2 hours and are refreshed automatically. X rotates the refresh token on every refresh; refreshes hold a per-account lock and save the new token atomically, so the MCP server and the sync job can run side by side. If a refresh ever fails, run login again for that account.
x_api.sync_bookmarks appends new bookmarks from every authorized account to a markdown file, one entry per bookmark with the author, date, link, and full text. It only fetches and dedupes, with no classification, so it is safe to run unattended.
echo "X_BOOKMARKS_FILE=/path/to/Social bookmarks.md" >> ~/.config/x-api/.env
uv run python -m x_api.sync_bookmarks # or --dry-run to only count new itemsSeen IDs are kept in ~/.local/state/x-api/bookmarks.json. Each run reads 10 bookmarks per account and pages further only when all of them are new, up to X_BOOKMARKS_MAX_PAGES pages (default 3). X only exposes an account's 800 most recent bookmarks.
uv run python -m x_api.timer installThis writes x-bookmarks-sync.service and x-bookmarks-sync.timer into ~/.config/systemd/user/, with this checkout's path and uv's location filled in, then enables the timer (daily at about 07:00, catching up after downtime). It is safe to re-run, for example after moving the repo. Logs: journalctl --user -u x-bookmarks-sync.