This tutorial will guide you through creating a Model Context Protocol (MCP) server that connects Twitter's trending topics with Claude's analysis capabilities. The server will fetch real-time Twitter trends and use Claude to analyze them for business opportunities.
mkdir twitter-trends-mcp
cd twitter-trends-mcp
python -m venv .venv
.venv\Scripts\activate # On Windows
pip install tweepy mcp python-dotenv hatchling
Create the following directory structure:
twitter-trends-mcp/
├── pyproject.toml
├── twitter_server_run.py
├── src/
│ └── twitter_trends_mcp/
│ ├── __init__.py
│ └── server.py
pyproject.toml
in the root directory:[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "twitter-trends-mcp"
version = "0.1.0"
description = "Twitter Trends MCP Server"
requires-python = ">=3.8"
dependencies = [
"tweepy",
"mcp",
"python-dotenv"
]
[tool.hatch.build]
packages = ["src/twitter_trends_mcp"]
include = ["src/twitter_trends_mcp/*"]
[project.scripts]
twitter-trends-server = "twitter_trends_mcp:main"
src/twitter_trends_mcp/__init__.py
:"""Twitter Trends MCP Server package."""
import asyncio
from . import server
def main():
"""Main entry point for the package."""
asyncio.run(server.main())
__all__ = ['main', 'server']
twitter_server_run.py
:#!/usr/bin/env python
import os
import sys
import logging
from pathlib import Path
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('twitter_server.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger('twitter-trends-mcp')
# Add the src directory to the Python path
src_path = str(Path(__file__).parent / "src")
sys.path.insert(0, src_path)
logger.info(f"Python path: {sys.path}")
try:
from twitter_trends_mcp.server import main
logger.info("Successfully imported server module")
except Exception as e:
logger.error(f"Error importing server module: {e}")
raise
if __name__ == "__main__":
try:
logger.info("Starting server...")
import asyncio
asyncio.run(main())
except KeyboardInterrupt:
logger.info("Server stopped by user")
except Exception as e:
logger.error(f"Server error: {e}")
raise
Create src/twitter_trends_mcp/server.py
with the complete server code, including:
Key components:
# Initialize Twitter clients
client_v2 = tweepy.Client(...)
auth = tweepy.OAuthHandler(...)
api_v1 = tweepy.API(auth)
# Define server capabilities
app = Server("twitter-trends-server")
# Implement handlers
@app.list_resources()
async def list_resources() -> list[Resource]: ...
@app.read_resource()
async def read_resource(uri: AnyUrl) -> str: ...
@app.list_tools()
async def list_tools() -> list[Tool]: ...
@app.call_tool()
async def call_tool(name: str, arguments: Any) -> Sequence[TextContent]: ...
Locate your Claude Desktop config file:
%APPDATA%\Claude\claude_desktop_config.json
~/Library/Application Support/Claude/claude_desktop_config.json
Update the configuration:
{
"mcpServers": {
"twitter-trends": {
"command": "C:\\Users\\YOUR_USERNAME\\twitter-trends-mcp\\.venv\\Scripts\\python.exe",
"args": ["C:\\Users\\YOUR_USERNAME\\twitter-trends-mcp\\twitter_server_run.py"],
"env": {
"PYTHONPATH": "C:\\Users\\YOUR_USERNAME\\twitter-trends-mcp\\src",
"PYTHONUNBUFFERED": "1"
},
"cwd": "C:\\Users\\YOUR_USERNAME\\twitter-trends-mcp"
}
}
}
pip install -e .
python twitter_server_run.py
In Claude Desktop:
Monitor logs:
Get-Content twitter_server.log -Wait
Common Issues:
Log Locations:
twitter_server.log
%APPDATA%\Claude\Logs\mcp*.log
Seamless access to top MCP servers powering the future of AI integration.