A Model Context Protocol (MCP) server that integrates with Twitter using the `agent-twitter-client` package, allowing AI models to interact with Twitter without direct API access.
**Tweet Operations**:
- Fetch tweets from users
- Get specific tweets by ID
- Search tweets
- Send tweets with text and media
- Create polls
- Like, retweet, and quote tweets
**User Operations**:
- Get user profiles
- Follow users
- Get followers and following lists
**Grok Integration**:
- Chat with Grok via Twitter's interface
- Continue conversations with conversation IDs
- Get web search results and citations
- Access Twitter's real-time data through Grok
- **Note**: Grok functionality requires [agent-twitter-client v0.0.19](https://github.com/elizaOS/agent-twitter-client/releases/tag/0.0.19) or higher
Documentation
[Developer Guide](docs/DEVELOPER_GUIDE.md) - Comprehensive guide for developers
[Testing Guide](docs/TESTING.md) - Instructions for testing the MCP
[Agent Guide](docs/AGENT_GUIDE.md) - Guide for AI agents on how to use the Twitter MCP
[Contributing Guide](CONTRIBUTING.md) - Guidelines for contributing to this project
[Changelog](CHANGELOG.md) - History of changes to this project
[Demo README](demo/README.md) - Guide for running the demo scripts
[Grok Examples](demo/GROK_EXAMPLES.md) - Documentation for the Grok AI integration examples
Quick Start
Installation
Basic Usage
Create a `.env` file with your Twitter credentials (see [Authentication Methods](#authentication-methods))
Run the MCP server:
Demo Scripts
The package includes a `demo` directory with example scripts that demonstrate various features:
See the [Demo README](demo/README.md) for more details.
Port Configuration
By default, the MCP server runs on port 3000. If you need to change this (for example, if you already have an application running on port 3000), you have several options:
Option 1: Using Environment Variables
Set the `PORT` environment variable:
Option 2: Using Docker Compose
If using Docker Compose, you can configure both the host and container ports in your `.env` file:
Then run:
This will map port 3001 on your host to port 3000 in the container, allowing you to access the MCP at http://localhost:3001 while your other application continues to use port 3000.
Setup with Claude Desktop
Configure Claude Desktop to use this MCP by adding to your config file:
Copy the values of `auth_token`, `ct0`, and `twid` cookies
Make sure to include the `Domain=.twitter.com` part for each cookie
Username/Password Authentication
Twitter API Authentication
Available Tools
`get_user_tweets`: Fetch tweets from a specific user
`get_tweet_by_id`: Fetch a specific tweet by ID
`search_tweets`: Search for tweets
`send_tweet`: Post a new tweet
`send_tweet_with_poll`: Post a tweet with a poll
`like_tweet`: Like a tweet
`retweet`: Retweet a tweet
`quote_tweet`: Quote a tweet
`get_user_profile`: Get a user's profile
`follow_user`: Follow a user
`get_followers`: Get a user's followers
`get_following`: Get users a user is following
`grok_chat`: Chat with Grok via Twitter
`health_check`: Check the health of the Twitter MCP server
Testing Interface
The MCP includes an interactive command-line interface for testing:
This launches a REPL where you can test various MCP functions:
Example Test Commands
Example Usage
Ask Claude to:
"Search Twitter for tweets about AI"
"Post a tweet saying 'Hello from Claude!'"
"Get the latest tweets from @OpenAI"
"Chat with Grok about quantum computing"
Advanced Usage
Working with Media
To post a tweet with an image:
To post a tweet with a video:
Creating Polls
To create a poll:
Interacting with Grok
To have a conversation with Grok:
To continue a conversation with Grok:
Grok's Unique Capabilities
Grok on Twitter has access to real-time Twitter data that even the standalone Grok API doesn't have. This means you can ask Grok about:
Current trending topics on Twitter
Analysis of recent tweets on specific subjects
Information about Twitter users and their content
Real-time events being discussed on the platform
Example queries:
"What are the trending topics on Twitter right now?"
"Analyze the sentiment around AI on Twitter"
"What are people saying about the latest Apple event?"
"Show me information about popular memecoins being discussed today"
Grok Authentication Requirements
Grok functionality requires proper authentication. The MCP supports two methods:
**Cookie Authentication** (Recommended):
- Cookies must be in JSON array format
- Example: `TWITTER_COOKIES=["auth_token=YOUR_AUTH_TOKEN; Domain=.twitter.com", "ct0=YOUR_CT0_VALUE; Domain=.twitter.com", "twid=u%3DYOUR_USER_ID; Domain=.twitter.com"]`
- Essential cookies are `auth_token`, `ct0`, and `twid`
**Username/Password Authentication**:
- Set `TWITTER_USERNAME` and `TWITTER_PASSWORD` in your environment
- May encounter Cloudflare protection in some cases
Grok Rate Limits
Grok has rate limits that may affect usage:
Non-premium accounts: 25 messages per 2 hours
Premium accounts: Higher limits
The MCP will return rate limit information in the response when limits are reached.
For more details on using Grok, see the [Grok Examples](demo/GROK_EXAMPLES.md) documentation.
Troubleshooting
Authentication Issues
Cookie Authentication Problems
If you're experiencing issues with cookie authentication:
**Cookie Expiration**: Twitter cookies typically expire after a certain period. Try refreshing your cookies by logging out and back into Twitter.
**Cookie Format**: Ensure your cookies are properly formatted as a JSON array of strings with the correct domain.
**Required Cookies**: Make sure you've included the essential cookies: `auth_token`, `ct0`, and `twid`.
Example of properly formatted cookies:
Credential Authentication Problems
If you're having trouble with username/password authentication:
**Two-Factor Authentication**: If your account has 2FA enabled, you'll need to provide the `TWITTER_2FA_SECRET`.
**Account Lockouts**: Too many failed login attempts may lock your account. Check your email for account verification requests.
**Captcha Challenges**: Twitter may present captcha challenges that the client can't handle automatically.
API Authentication Problems
For API authentication issues:
**API Key Permissions**: Ensure your API keys have the necessary permissions for the actions you're trying to perform.
**Rate Limiting**: Twitter API has rate limits that may cause failures if exceeded.
**API Changes**: Twitter occasionally changes its API, which may cause compatibility issues.
Operation Errors
Tweet Posting Failures
If you can't post tweets:
**Content Restrictions**: Twitter may block tweets that violate its content policies.
**Media Format Issues**: Ensure media is properly formatted and encoded.
**Rate Limiting**: Twitter limits how frequently you can post.
Search Problems
If search isn't working:
**Query Syntax**: Ensure your search query follows Twitter's search syntax.
**Search Limitations**: Some search modes may have restrictions or require specific permissions.
Grok Issues
If Grok functionality isn't working:
**Version Requirement**:
- Grok requires [agent-twitter-client v0.0.19](https://github.com/elizaOS/agent-twitter-client/releases/tag/0.0.19) or higher
- The current package uses v0.0.18 for basic functionality
- For the demo scripts, use the `--use-local-agent-twitter-client` flag to temporarily install v0.0.19
**Authentication Issues**:
- Cookie Format: Ensure cookies are in the correct JSON array format
- Cookie Validity: Twitter cookies expire after a certain period
- Cloudflare Protection: Username/password authentication may be blocked by Cloudflare
- Premium Requirement: Grok access requires a Twitter Premium subscription
**Rate Limits**:
- Non-premium accounts: 25 messages per 2 hours
- Error Message: "Rate Limited: You've reached the limit..."
- Solution: Wait until the rate limit resets or upgrade to a premium account
**Environment File Location**:
- For the demo scripts, make sure your credentials are in `demo/.env`, not in the root `.env` file
- Use the `--debug-env` flag to check which environment variables are being loaded
For detailed troubleshooting of Grok issues, see the [Grok Examples](demo/GROK_EXAMPLES.md) documentation.
Server Issues
Health Check
Use the `health_check` tool to diagnose server issues:
The health check will report on:
Authentication status
API connectivity
Memory usage
Logging
The server logs to both console and files:
`error.log`: Contains error-level messages
`combined.log`: Contains all log messages
Check these logs for detailed error information.
Development
Prerequisites
Node.js 18+
npm
Setup
Clone the repository
Install dependencies
Create a `.env` file with configuration:
Build the project
Start the server
Environment Variables
In addition to the authentication variables, you can set:
`LOG_LEVEL`: Set logging level (error, warn, info, debug)
`NODE_ENV`: Set environment (development, production)
Docker
You can also run the server using Docker:
Using Docker Directly
Using Docker Compose
Create a `.env` file with your Twitter credentials
Run with docker-compose:
Environment Variables in Docker
You can pass environment variables to the Docker container in several ways:
**In the docker-compose.yml file** (already configured)
**Through a .env file** (recommended for docker-compose)
**Directly in the docker run command** (as shown above)
Persisting Logs
The docker-compose configuration includes a volume mount for logs:
This will store logs in a `logs` directory in your project folder.
Security Considerations
**Credential Storage**: Store credentials securely, preferably using environment variables or a secure vault.
**Rate Limiting**: Implement rate limiting to prevent abuse of the Twitter API.
**Content Validation**: Validate all content before posting to prevent malicious use.