Uses SQLite database for encrypted token storage with AES-256 encryption and OS keychain integration for managing multi-user OAuth credentials.
X MCP Server v2.0
A comprehensive Model Context Protocol (MCP) server for X (Twitter) API v2 with multi-user OAuth 2.0 + PKCE, encrypted token storage, and automatic token refresh.
๐ New in v2.0
โ Multi-User Support - Multiple users with isolated tokens and sessions
โ Dual OAuth Flows - Loopback (local) + Hosted Pairing Code (remote/multi-user)
โ Encrypted Token Storage - SQLite database with OS keychain integration
โ Automatic Token Refresh - Background refresh with scope validation
โ Session Management - Secure session handling for different transports
โ Non-Browser Support - Works without browser access via pairing codes
Features
โ OAuth 2.0 Authorization Code + PKCE authentication (both flows)
โ Multi-user support with per-user token isolation
โ Encrypted local token storage (AES-256 + OS keychain)
โ Automatic token refresh and persistence
โ Scope verification and re-auth instructions
โ Rate limiting with retry/backoff logic
โ Bookmark management (list, add, remove)
โ Tweet creation (text, media, replies, quotes)
โ Session-aware MCP resources
โ Comprehensive error handling and logging
Setup
1. X Developer Account Setup
Create a X Developer account
Create a new app in the Developer Portal
Configure OAuth 2.0 settings:
App permissions: Read and Write
Type of App: Web App
Callback URI:
http://localhost:3000/callback(or your custom URI)Website URL: Required (can be placeholder)
Note your Client ID and Client Secret
2. Installation
3. Environment Setup
Create a .env file or export environment variables:
4. Authentication
Run the authentication helper to complete OAuth flow:
This will:
Open your browser to X's authorization page
Start a local callback server
Exchange the authorization code for tokens
Save tokens to
~/.x-mcp/tokens.json
Quick Start
V2.0 Multi-User Server (Recommended)
Authentication Flows
Local/Single-User (Loopback)
Multi-User/Remote (Hosted Pairing)
Legacy V1.0 Server
MCP Tools
The server provides these tools:
bookmarks.list
List user bookmarks with pagination support.
Parameters:
user_id(optional): User ID (defaults to authenticated user)max_results(optional): Maximum results per page (1-100, default: 10)pagination_token(optional): Token for next page of results
Example:
bookmarks.add
Add a tweet to bookmarks.
Parameters:
tweet_id(required): ID of the tweet to bookmarkuser_id(optional): User ID (defaults to authenticated user)
Returns: { "ok": true }
bookmarks.remove
Remove a tweet from bookmarks.
Parameters:
tweet_id(required): ID of the tweet to remove from bookmarksuser_id(optional): User ID (defaults to authenticated user)
Returns: { "ok": true }
tweet.create
Create a new tweet.
Parameters:
text(required): Tweet text content (max 280 characters)media_ids(optional): Array of media IDs to attachreply(optional): Object within_reply_to_tweet_idfor repliesquote_tweet_id(optional): ID of tweet to quote
Example:
MCP Resources
mcp://x/user/me
Current authenticated X user information:
mcp://x/bookmarks/latest
Last fetched bookmarks page with pagination token:
Required Scopes
The server requires these X API scopes:
bookmark.readorbookmarks.read- Read bookmarksbookmarks.write- Modify bookmarkstweet.write- Post tweetstweet.read- Read tweets (for tweet creation responses)users.read- Read user informationoffline.access- Refresh token support
If scopes are missing, the server will provide re-authentication instructions.
Rate Limiting
The server automatically handles X API rate limits:
โ Retries on 429/5xx responses with exponential backoff
โ Reads
x-rate-limit-*headers for intelligent throttlingโ Surfaces MCP notifications when limits are low
โ Adapts to actual API limits (doesn't hardcode limits)
Current typical limits (may vary by plan):
GET bookmarks: ~180 requests per 15 minutes
POST/DELETE bookmarks: ~50 requests per 15 minutes
POST tweets: ~300 requests per 15 minutes
File Structure
Scripts
Configuration
V2.0 Environment Variables:
X_CLIENT_ID- X app client ID (required)X_CLIENT_SECRET- X app client secret (required)X_REDIRECT_URI- OAuth redirect URI (default:http://127.0.0.1:3000/auth/x/cb)X_HOSTED_MODE- Enable hosted pairing mode (true/false)X_BASE_URL- Base URL for hosted mode (e.g.,https://yourapp.com)X_MCP_ENCRYPTION_KEY- Manual encryption key (base64, optional)
Storage Locations:
V2.0 Database:
~/.mcp/x/tokens.db(SQLite with encryption)V1.0 Tokens:
~/.x-mcp/tokens.json(legacy JSON format)Encryption Key: OS Keychain or
~/.mcp/x/.encryption_key
Error Handling
The server provides detailed error messages for:
โ Missing authentication/tokens
โ Insufficient scopes with re-auth instructions
โ API errors with request IDs (in debug mode)
โ Rate limit violations with retry guidance
โ Network errors with retry logic
Testing
Comprehensive Test Suite
Manual E2E Testing (V2.0)
Start server:
npm startLoopback auth: Call
auth/startwithmode: "loopback"Complete OAuth: Open returned URL, authorize
Test bookmarks:
bookmarks.list,bookmarks.add,bookmarks.removeTest tweets:
tweet.create
Multi-User Testing (V2.0)
Enable hosted mode: Set
X_HOSTED_MODE=true,X_BASE_URLStart pairing: Call
auth/startwithmode: "hosted"Complete auth: Visit login URL with pairing code
Check status: Call
auth/statuswith pairing codeTest with session: Include session context in subsequent calls
Legacy Testing (V1.0)
Authentication:
npm run authStart legacy server:
npm run start:legacyTest tools: Use existing MCP tools
Troubleshooting
Authentication Issues
Verify X_CLIENT_ID and X_CLIENT_SECRET are correct
Check that redirect URI matches your app configuration
Ensure your X app has correct permissions (Read and Write)
Scope Issues
Re-run
npm run authto get updated scopesCheck that your X app is configured for the required permissions
Rate Limit Issues
The server will automatically retry and backoff
Check the logs for rate limit information
Consider the plan limits for your X developer account
Token Issues
Tokens are auto-refreshed when expired
V2.0: Check database at
~/.mcp/x/tokens.db, runnpm testfor diagnosticsV1.0: Delete
~/.x-mcp/tokens.jsonand re-authenticate if needed
๐ Detailed Documentation
For comprehensive documentation on the new multi-user OAuth system, architecture, and advanced usage, see:
README-MULTIUSER.md - Complete v2.0 documentation with:
๐๏ธ Detailed architecture overview
๐ Security implementation details
๐๏ธ Database schema and design
๐ก Complete API reference
๐งช Advanced testing scenarios
๐ ๏ธ Development and extension guide