illancillotto/twitter-mcp-server
If you are the rightful owner of twitter-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
The Twitter MCP Server integrates with n8n to automate Twitter operations using a personal Twitter API via scraping with Twikit, exposed as an MCP server.
π¦ Twitter MCP Server + n8n Integration
Personal Twitter API tramite scraping con Twikit, esposto come MCP server per automazioni n8n.
π Indice
- Features
- Requisiti
- Installazione
- Configurazione
- Utilizzo
- API Endpoints
- n8n Workflows
- Best Practices
- Troubleshooting
β¨ Features
Operazioni Tweet
- β Creare tweet con media (foto/video)
- β Cercare tweet (Latest/Top/Media)
- β Like e Retweet
- β Reply a tweet
- β Eliminare tweet
Operazioni Utente
- β Profilo utente completo
- β Timeline utente
- β Follow/Unfollow
- β Followers e Following
Direct Messages
- β Inviare DM
- β Inbox DM (limitato)
Discovery
- β Trending topics
- β Home timeline
- β Notifiche
Gestione Avanzata
- β Rate limiting intelligente
- β Error handling con retry
- β Cookie management automatico
- β Logging completo
- β Health checks
π§ Requisiti
Software
- Docker & Docker Compose (raccomandato)
- Python 3.11+ (se installazione locale)
- n8n (incluso in docker-compose)
Account Twitter
β οΈ IMPORTANTE: Usa un account secondario per sicurezza!
- Username
- Password
Non usare il tuo account principale per evitare ban.
π¦ Installazione
Opzione 1: Docker (Raccomandato)
# 1. Clone repository
git clone <your-repo>
cd twitter-mcp-server
# 2. Crea file .env
cp .env.example .env
# Edita .env con le tue credenziali
# 3. Build e start
docker-compose up -d
# 4. Check logs
docker-compose logs -f twitter-mcp
Opzione 2: Installazione Locale
# 1. Clone repository
git clone <your-repo>
cd twitter-mcp-server
# 2. Virtual environment
python -m venv venv
source venv/bin/activate # Linux/Mac
# or
venv\Scripts\activate # Windows
# 3. Install dependencies
pip install -r requirements.txt
# 4. Setup .env
cp .env.example .env
# Edita .env
# 5. Run server
python -m uvicorn mcp_server.server:app --host 0.0.0.0 --port 8000 --reload
βοΈ Configurazione
File .env
# Twitter Credentials
TWITTER_USERNAME=your_secondary_account
TWITTER_EMAIL=secondary@email.com
TWITTER_PASSWORD=secure_password
# n8n Credentials
N8N_USER=admin
N8N_PASSWORD=strong_password_here
# Server Config
LOG_LEVEL=INFO
RATE_LIMIT_PER_MINUTE=30
RATE_LIMIT_PER_HOUR=500
Struttura Directory
twitter-mcp-server/
βββ mcp_server/ # Server code
β βββ server.py # FastAPI server
β βββ twitter_client.py # Twikit wrapper
β βββ utils.py # Utilities
βββ data/ # Cookies & data (auto-created)
βββ logs/ # Server logs (auto-created)
βββ workflows/ # n8n workflows
βββ docker-compose.yml # Docker orchestration
βββ Dockerfile # Container definition
βββ requirements.txt # Python dependencies
π Utilizzo
1. Verifica Health Check
curl http://localhost:8000/health
Risposta attesa:
{
"status": "healthy",
"authenticated": false,
"version": "1.0.0"
}
2. Autenticazione
curl -X POST http://localhost:8000/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "your_username",
"email": "your_email@example.com",
"password": "your_password"
}'
3. Crea un Tweet
curl -X POST http://localhost:8000/tweets/create \
-H "Content-Type: application/json" \
-d '{
"text": "Hello from MCP Server! π"
}'
4. Cerca Tweet
curl -X POST http://localhost:8000/tweets/search \
-H "Content-Type: application/json" \
-d '{
"query": "python automation",
"product": "Latest",
"count": 20
}'
5. Like un Tweet
curl -X POST "http://localhost:8000/tweets/like?tweet_id=1234567890"
π‘ API Endpoints
Authentication
Endpoint | Method | Descrizione |
---|---|---|
/auth/login | POST | Login con credenziali |
/auth/logout | POST | Logout e cleanup |
/auth/status | GET | Stato autenticazione |
Tweets
Endpoint | Method | Descrizione |
---|---|---|
/tweets/create | POST | Crea nuovo tweet |
/tweets/search | POST | Cerca tweets |
/tweets/like | POST | Like tweet |
/tweets/retweet | POST | Retweet |
/tweets/{id} | DELETE | Elimina tweet |
Users
Endpoint | Method | Descrizione |
---|---|---|
/users/profile | POST | Profilo utente |
/users/tweets | POST | Timeline utente |
/users/follow | POST | Follow utente |
/users/unfollow | POST | Unfollow utente |
Direct Messages
Endpoint | Method | Descrizione |
---|---|---|
/dm/send | POST | Invia DM |
/dm/inbox | GET | Inbox DM |
Discovery
Endpoint | Method | Descrizione |
---|---|---|
/trends/{location} | GET | Trending topics |
/timeline/home | GET | Home timeline |
/notifications | GET | Notifiche |
System
Endpoint | Method | Descrizione |
---|---|---|
/health | GET | Health check |
π n8n Workflows
Accesso n8n
- Apri browser:
http://localhost:5678
- Login con credenziali da
.env
:- User:
admin
(default) - Password: quella impostata in
.env
- User:
Import Workflow
- In n8n, vai su Workflows β Import from File
- Seleziona
workflows/n8n_twitter_workflow.json
- Salva
Configurazione Workflow
Il workflow include:
Trigger:
- Webhook per richieste esterne
- Manual trigger per test
Nodi Principali:
- Authentication - Login automatico
- Create Tweet - Pubblica tweet
- Search Tweets - Ricerca contenuti
- Get User Profile - Info utente
- Get Trends - Trending topics
- Like/Retweet - Interazioni
- Send DM - Messaggi diretti
Nodi Utility:
- Switch Action - Router per azioni
- Rate Limiter - Gestione rate limits
- Error Handler - Retry logic
- Data Transformer - Pulizia dati
Esempi Webhook
Crea Tweet:
curl -X POST http://localhost:5678/webhook/twitter-webhook \
-H "Content-Type: application/json" \
-d '{
"action": "create_tweet",
"text": "Automated tweet from n8n! π€"
}'
Cerca Tweet:
curl -X POST http://localhost:5678/webhook/twitter-webhook \
-H "Content-Type: application/json" \
-d '{
"action": "search",
"query": "n8n automation",
"count": 10
}'
π Best Practices
Sicurezza
-
Account Secondario
- Usa sempre account diverso dal principale
- Attiva 2FA dopo configurazione iniziale
-
Credentials
- Mai committare
.env
su Git - Usa password manager
- Ruota password regolarmente
- Mai committare
-
Rate Limiting
- Rispetta limiti configurati (30/min, 500/hour)
- Monitora logs per rate limit warnings
- Implementa backoff esponenziale
Performance
-
Cookies Management
- Cookies salvati automaticamente in
data/cookies.json
- ValiditΓ ~30 giorni
- Re-login automatico se scaduti
- Cookies salvati automaticamente in
-
Batch Operations
- Per operazioni massive, usa rate limiter
- Chunking di 10-20 items per batch
- Sleep tra batch (2-5 secondi)
-
Logging
- Logs in
logs/
directory - Rotazione automatica giornaliera
- Level INFO per produzione, DEBUG per dev
- Logs in
Error Handling
-
Retry Logic
- Retry automatico per errori temporanei
- Max 3 retry con backoff esponenziale
- Log dettagliato degli errori
-
Monitoring
- Health check ogni 30 secondi
- Alert su errori consecutivi
- Dashboard Grafana (optional)
π Troubleshooting
Errore: "Login failed"
Cause:
- Credenziali errate
- 2FA attivo (non supportato)
- Account bloccato
Soluzioni:
# Verifica credenziali
docker-compose logs twitter-mcp | grep "Login"
# Reset cookies
rm data/cookies.json
docker-compose restart twitter-mcp
# Test login manuale
curl -X POST http://localhost:8000/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"...","email":"...","password":"..."}'
Errore: "Rate limit exceeded"
Cause:
- Troppi requests
- Ban temporaneo Twitter
Soluzioni:
# Aumenta delay in .env
RATE_LIMIT_PER_MINUTE=20
RATE_LIMIT_PER_HOUR=300
# Restart
docker-compose restart
# Attendi 15-30 minuti per reset Twitter rate limit
Errore: "Connection refused"
Cause:
- Server non avviato
- Porta occupata
- Network issues
Soluzioni:
# Check containers
docker-compose ps
# Check logs
docker-compose logs twitter-mcp
# Restart tutto
docker-compose down
docker-compose up -d
# Check ports
netstat -tulpn | grep 8000
n8n non si connette a MCP Server
Soluzioni:
# Verifica network
docker network inspect twitter-network
# Test connettivitΓ
docker exec n8n ping twitter-mcp
# Usa hostname corretto in n8n:
# http://twitter-mcp:8000 (dentro Docker)
# http://localhost:8000 (host system)
Tweet non viene pubblicato
Debug steps:
# 1. Verifica auth
curl http://localhost:8000/auth/status
# 2. Test diretto
curl -X POST http://localhost:8000/tweets/create \
-H "Content-Type: application/json" \
-d '{"text":"Test tweet"}'
# 3. Check logs
docker-compose logs twitter-mcp | tail -50
# 4. Verifica lunghezza tweet (max 280 chars)
π Monitoring
Health Dashboard
Crea un workflow n8n per monitoring:
{
"nodes": [
{
"name": "Schedule Trigger",
"type": "n8n-nodes-base.cron",
"parameters": {
"cronExpression": "*/5 * * * *"
}
},
{
"name": "Health Check",
"type": "n8n-nodes-base.httpRequest",
"parameters": {
"url": "http://twitter-mcp:8000/health"
}
},
{
"name": "Alert on Error",
"type": "n8n-nodes-base.if",
"parameters": {
"conditions": {
"boolean": [{
"value1": "={{$json.status !== 'healthy'}}"
}]
}
}
}
]
}
Logs Monitoring
# Real-time logs
docker-compose logs -f twitter-mcp
# Error logs only
docker-compose logs twitter-mcp | grep ERROR
# Last 100 lines
docker-compose logs --tail=100 twitter-mcp
π€ Contributing
Contributi benvenuti! Per nuove feature:
- Fork repository
- Crea branch:
git checkout -b feature/nome-feature
- Commit:
git commit -m 'Add feature'
- Push:
git push origin feature/nome-feature
- Pull Request
π License
MIT License - vedi LICENSE file
β οΈ Disclaimer
Questo progetto Γ¨ per uso educativo e personale.
- Rispetta i Terms of Service di Twitter/X
- Non usare per spam o attivitΓ illecite
- Usa con moderazione per evitare ban
- L'autore non Γ¨ responsabile per uso improprio
π Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: your@email.com
π― Roadmap
- Support proxy rotation
- Redis caching
- Webhook notifications
- Grafana dashboard
- Auto-scaling rate limits
- Multi-account support
- Advanced analytics
Made with β€οΈ for the n8n community