Skip to content

Repository files navigation

Twitter/X Research for Claude Code

Search Twitter/X directly from Claude Code CLI using two methods:

  1. Browser MCP (Recommended) - Uses your logged-in Chrome session, no API needed
  2. Twitter API (Backup) - Uses official Twitter API with rate limits

Architecture Overview

┌──────────────────────────────────────────────────────────────────────────────┐
│                           CLAUDE CODE CLI                                     │
│                                                                               │
│   User: "Search Twitter for $NVDA sentiment"                                 │
│                              │                                                │
│              ┌───────────────┴───────────────┐                               │
│              ▼                               ▼                                │
│   ┌─────────────────────┐       ┌─────────────────────────┐                  │
│   │   BROWSER MCP       │       │   TWITTER API MCP       │                  │
│   │   (Recommended)     │       │   (Backup/Offline)      │                  │
│   └─────────────────────┘       └─────────────────────────┘                  │
│              │                               │                                │
│              ▼                               ▼                                │
│   ┌─────────────────────┐       ┌─────────────────────────┐                  │
│   │  Chrome Extension   │       │  twitter_mcp_server.py  │                  │
│   │  + Your Login       │       │  + Tweepy + Anthropic   │                  │
│   └─────────────────────┘       └─────────────────────────┘                  │
│              │                               │                                │
│              ▼                               ▼                                │
│   ┌─────────────────────┐       ┌─────────────────────────┐                  │
│   │  twitter.com/x.com  │       │   Twitter API v2        │                  │
│   │  (Full Access)      │       │   (Rate Limited)        │                  │
│   └─────────────────────┘       └─────────────────────────┘                  │
└──────────────────────────────────────────────────────────────────────────────┘

Method Comparison

Feature Browser MCP Twitter API
Cost Free Free tier limited, $200/mo for more
Rate Limits None 1,500 tweets/month (free)
Setup Chrome extension API keys + Python
Auth Your Twitter login Bearer token
Access Full Twitter experience API endpoints only
Works offline No (needs Chrome) Yes

Method 1: Browser MCP (Recommended)

This method uses your Chrome browser with the Browser MCP extension. Claude controls your browser to search Twitter using your logged-in session.

How It Works

┌─────────────────┐     ┌──────────────────┐     ┌─────────────────┐
│  Claude Code    │────▶│  Browser MCP     │────▶│  Chrome Browser │
│  CLI            │     │  Extension       │     │  (Your Login)   │
└─────────────────┘     └──────────────────┘     └─────────────────┘
                                                          │
                                                          ▼
                                                 ┌─────────────────┐
                                                 │  twitter.com    │
                                                 │  (Full Access)  │
                                                 └─────────────────┘

Setup Steps

Step 1: Install Chrome Extension

  1. Go to Browser MCP on Chrome Web Store
  2. Click "Add to Chrome"

Step 2: Add to Claude Code Config

Add to C:\Users\<username>\.claude.json:

{
  "mcpServers": {
    "browsermcp": {
      "command": "npx",
      "args": ["@browsermcp/mcp@latest"]
    }
  }
}

Step 3: Connect Extension

  1. Open Chrome and go to twitter.com
  2. Log into Twitter/X if not already
  3. Click the Browser MCP extension icon in toolbar
  4. Click "Connect"

Step 4: Restart Claude Code

Close and reopen Claude Code CLI, then verify with /mcp

Usage Examples

Search Twitter for $NVDA sentiment

Find latest tweets about AI stocks

Go to twitter.com and search for $TSLA options flow

Browser MCP Tools Available

Tool Description
browser_navigate Go to a URL
browser_snapshot Read page content
browser_click Click elements
browser_type Type into fields
browser_screenshot Take screenshot

Method 2: Twitter API (Backup)

This method uses the official Twitter API via a custom MCP server. Useful when you need offline access or want an autonomous sub-agent.

How It Works

┌─────────────────┐     ┌──────────────────────┐     ┌─────────────────┐
│  Claude Code    │────▶│  twitter_mcp_server  │────▶│  Twitter API    │
│  CLI            │     │  (Python + MCP)      │     │  (v2 Endpoints) │
└─────────────────┘     └──────────────────────┘     └─────────────────┘
                                  │
                                  ▼
                        ┌──────────────────────┐
                        │  Sub-Agent (Claude)  │
                        │  Autonomous Research │
                        └──────────────────────┘

Setup Steps

Step 1: Get API Credentials

Twitter Bearer Token:

  1. Go to developer.twitter.com
  2. Create Developer Account → Project → App
  3. Get Bearer Token from "Keys and Tokens"

Anthropic API Key:

  1. Go to console.anthropic.com
  2. Create API key

Step 2: Install Dependencies

cd "d:\Claude Agent Twitter"
pip install -r requirements.txt

Step 3: Configure Credentials

Create .env file:

TWITTER_BEARER_TOKEN=your_bearer_token_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here

Step 4: Test Connection

python test_connection.py

Step 5: Add to Claude Code Config

Add to C:\Users\<username>\.claude.json:

{
  "mcpServers": {
    "twitter-research": {
      "type": "stdio",
      "command": "python",
      "args": ["d:\\Claude Agent Twitter\\twitter_mcp_server.py"]
    }
  }
}

Twitter API Tools Available

Tool Description
twitter_search Direct search query
twitter_research_agent Autonomous multi-search sub-agent

Sub-Agent Architecture

The twitter_research_agent spawns a specialized Claude instance:

def run_twitter_sub_agent(task: str) -> str:
    # 1. Create Claude client with specialized system prompt
    client = anthropic.Anthropic()

    # 2. Define search tool for sub-agent
    tools = [{"name": "search_twitter", ...}]

    # 3. Agentic loop - makes multiple searches autonomously
    while response.stop_reason == "tool_use":
        result = search_twitter(**tool_use.input)
        messages.append(tool_result)

    # 4. Return comprehensive summary
    return response.text

Rate Limits

Tier Price Tweets/Month
Free $0 1,500
Basic $200/mo 10,000
Pro $5,000/mo 1,000,000

Project Structure

d:\Claude Agent Twitter\
├── twitter_mcp_server.py   # Twitter API MCP server
├── run_agent.py            # CLI interface for sub-agent
├── test_connection.py      # API connection tester
├── setup.py                # Interactive setup helper
├── requirements.txt        # Python dependencies
├── .env                    # API credentials (DO NOT COMMIT)
├── .gitignore              # Git ignore rules
├── CLAUDE.md               # Claude Code context
└── README.md               # This file

Search Operators (Both Methods)

Operator Example Description
$TICKER $NVDA Stock cashtag
from:user from:elonmusk From specific user
to:user to:nvidia Replies to user
#hashtag #AI Hashtag
"phrase" "options flow" Exact match
-word -spam Exclude word
lang:xx lang:en Language filter

Quick Reference

Browser MCP (Recommended)

Search Twitter for $NVDA

Navigate to x.com/search?q=$TSLA&f=live

Twitter API

Use twitter_search to find tweets about $SPY

Use twitter_research_agent to research AI stock sentiment

Direct URL Pattern

https://x.com/search?q=%24NVDA&f=live     # Latest $NVDA
https://x.com/search?q=from%3Aunusual_whales  # From user

Troubleshooting

Browser MCP Issues

"No connection to browser extension"

  • Click Browser MCP extension icon → Click "Connect"
  • Make sure Chrome is open with the extension active

Page not loading

  • Check if Chrome is responsive
  • Try refreshing the connected tab

Twitter API Issues

"429 Too Many Requests"

  • Hit rate limit, wait 15 minutes

"TWITTER_BEARER_TOKEN not set"

  • Check .env file exists with valid token

MCP server not appearing

  1. Restart Claude Code CLI
  2. Run /mcp to check status
  3. Verify path in .claude.json

Security Notes

  • Never commit .env file (contains API keys)
  • Browser MCP uses your logged-in session - be careful what you automate
  • The .gitignore excludes sensitive files

Credits

About

Search Twitter/X directly from Claude Code CLI using two methods: 1. **Browser MCP** (Recommended) - Uses your logged-in Chrome session, no API needed 2. **Twitter API** (Backup) - Uses official Twitter API with rate limits

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages