Run a local MCP server that exposes the X API OpenAPI spec as tools using FastMCP. Streaming and webhook endpoints are excluded.
- Python 3.9+
- An X Developer Platform app (to get tokens)
- Optional: an xAI API key if you want to run the Grok test client
- Create a virtual environment and install dependencies:
python -m venv .venvsource .venv/bin/activatepip install -r requirements.txt
- Create your local
.env:cp env.example .env- Fill in the OAuth1 section (consumer key/secret and callback settings).
- Run the server:
python server.py
The server starts at http://127.0.0.1:8000 by default.
The MCP endpoint is http://127.0.0.1:8000/mcp.
Required (OAuth1 user context):
TWITTER_CONSUMER_KEYTWITTER_CONSUMER_SECRETX_OAUTH_CALLBACK_HOST(default127.0.0.1)X_OAUTH_CALLBACK_PORT(default8976)X_OAUTH_CALLBACK_PATH(default/oauth/callback)X_OAUTH_CALLBACK_TIMEOUT(default300)
Optional auth fallback:
X_BEARER_TOKEN(OAuth2 bearer token)
Optional server config:
MCP_HOST(default127.0.0.1)MCP_PORT(default8000)X_API_BASE_URL(defaulthttps://api.x.com)X_API_TIMEOUT(default30)X_API_DEBUG(default1)FASTMCP_EXPERIMENTAL_ENABLE_NEW_OPENAPI_PARSER
Tool filtering (comma-separated):
X_API_TOOL_TAGSX_API_TOOL_ALLOWLISTX_API_TOOL_DENYLIST
Optional Grok test client:
XAI_API_KEYXAI_MODEL(defaultgrok-4-1-fast)MCP_SERVER_URL(defaulthttp://127.0.0.1:8000/mcp)
The server runs an OAuth1 browser flow on startup and uses the resulting access token to sign every request. You must register a callback URL in your X Developer App that matches:
http://<X_OAUTH_CALLBACK_HOST>:<X_OAUTH_CALLBACK_PORT><X_OAUTH_CALLBACK_PATH>
Example:
http://127.0.0.1:8976/oauth/callback
When you start the server, it will open a browser tab for consent and wait for the callback. Tokens are kept in memory only for the lifetime of the server process.
If you want to limit the tool list (smaller context window, fewer tools),
use X_API_TOOL_ALLOWLIST or X_API_TOOL_TAGS in .env.
Example allowlist:
X_API_TOOL_ALLOWLIST=getUsersByUsername,createDirectMessagesByParticipantId
Example tags:
X_API_TOOL_TAGS=users,dm
Allowlist and tags are applied at startup when the OpenAPI spec is loaded.
If you want a user-context OAuth2 token:
- Add
CLIENT_IDandCLIENT_SECRETto your.env. - Update
redirect_uriingenerate_authtoken.pyto match your app settings. - Run
python generate_authtoken.pyand follow the prompts. - Copy the printed access token into
.envasX_OAUTH_ACCESS_TOKEN.
- Set
XAI_API_KEYin.env. - Make sure your MCP server is running locally (or set
MCP_SERVER_URL). - If Grok cannot reach
http://127.0.0.1:8000/mcp, use ngrok to tunnel your local server and pointMCP_SERVER_URLto the public ngrok URL. - Run
python test_grok_mcp.py.
- Endpoints with
/streamor/webhooksin the path are excluded. - Operations tagged
StreamorWebhooks, or marked withx-twitter-streaming: true, are excluded. - The OpenAPI spec is fetched from
https://api.twitter.com/2/openapi.jsonat startup.