Give your AI agent a Twitter reader: MCP and tool definitions for Claude, ChatGPT and Codex

Updated
Let Claude, ChatGPT or Codex read public X profiles, posts, replies and search: connect the UnifAPI MCP server, or paste tool definitions for the paid X API.
In short: an agent needs four things to read X: an operation it can call with a handle or a post link, a JSON body it can read, an error it can branch on, and a cost it can see. The simplest way to get all four is the UnifAPI MCP server: add
https://mcp.unifapi.comin Claude, ChatGPT or Codex, sign in with OAuth when the client asks (no key to paste), and the agent reads X throughcall_api, pay as you go at 1 credit ($0.001) per returned record. If you run the agent loop yourself, below are copy-paste definitions for Anthropic's tool use and OpenAI's function calling with a handler for the paid X API, a SKILL.md for Claude Code and Codex, and the three rules the model needs: count credits before paging, sleep onRetry-After, and trim what goes into the context window.
Models already know how to read X. Ask Claude or ChatGPT what @NASA posted this week and they will try: a web search, a fetch of x.com, maybe a Nitter URL from training data. The search returns summaries, the fetch returns a JavaScript shell, and the Nitter instance is gone. The model then guesses or apologizes.
The fix is not a smarter model, it is a tool with an operation the model can call and a body it can parse. This post is that tool, in the formats people actually deploy: the MCP server first, then tools you wire yourself.
Connect the MCP server first
If the agent runs in Claude, ChatGPT or Codex, you do not need to write a tool at all. Add the UnifAPI MCP server and the agent gets three tools: list_operations to search the catalog, get_operation to read one operation's parameters, and call_api to run it. Per-client steps, including Cursor and others, are at unifapi.com/mcp; the short version:
- Claude: Settings โ Connectors โ Add custom connector. Name it UnifAPI, paste
https://mcp.unifapi.com, leave Authentication as OAuth, then Add and Connect. - ChatGPT: on a Plus, Pro, Team or Enterprise plan, turn on Developer Mode (Settings โ Connectors โ Advanced), click Create app, paste the same URL as the MCP Server URL and leave Authentication as OAuth.
- Codex: run
codex mcp add unifapi --url https://mcp.unifapi.com, thencodex mcp login unifapiif the sign-in did not open on its own (Codex MCP setup has the details). - Claude Code: run
claude mcp add --transport http unifapi https://mcp.unifapi.com.
The client opens a browser sign-in for your UnifAPI workspace; there is no key to paste. Calls spend Workspace Credits, pay as you go. Then ask in plain language ("what has @NASA posted with media lately?") and the agent issues a call like this one:
{
"path": "/x/tweets/search/recent",
"method": "GET",
"query": { "query": "from:nasa filter:media", "max_results": 20 }
}
That call returns up to 20 posts and costs up to 20 credits ($0.02). The MCP docs cover the server in more depth.
The X operations
Whether the agent calls them through call_api or your own handler calls them over HTTP, X reads are the same GET operations on the paid X API (reference at unifapi.com/apis/twitter, full spec at https://api.unifapi.com/openapi.json):
| Lookup | Operation | Parameters | Returns |
|---|---|---|---|
| profile | /x/users/by/username/{username} | username (no @) | one profile with its numeric id, metrics |
| timeline | /x/users/{id}/tweets | max_results (1โ100), pagination_token, exclude | an account's posts, newest first |
| post | /x/tweets/{id} | id | one post with author, media, metrics |
| media | /x/tweets/search/recent | query=from:<handle> filter:media | an account's recent photo and video posts |
| replies | /x/tweets/search/recent | query=conversation_id:<post id> | the public replies under a post |
| search | /x/tweets/search/recent | query, max_results (1โ100), pagination_token | recent posts; X search operators work |
| trends | /x/trends/by/woeid/{woeid} | woeid (1 is worldwide), max_trends (1โ50) | what is trending now |
| communities | /x/communities/search | query | X Communities for a topic |
| followers | /x/users/{id}/followers, /x/users/{id}/following | max_results (1โ100), pagination_token | who follows an account, or who it follows |
Every response is one envelope:
{
"request_id": "โฆ",
"data": [ โฆ ],
"pagination": { "has_more": true, "next_cursor": "โฆ" },
"billing": { "credits_charged": 20, "records_charged": 20, "balance_remaining": 1980, "truncated_due_to_balance": false }
}
data is an object for one item and an array for lists, and pagination is on lists; pass next_cursor back as pagination_token for the next page. Field names follow X API v2: a post has text, created_at, conversation_id, public_metrics and an author; a user has username, description and public_metrics.followers_count. Every error is { error: { type, message } } with a closed set of types (unauthorized, insufficient_credits, not_found, rate_limited, upstream_error and a few more). Every HTTP response carries RateLimit-* and X-RateLimit-* headers; a 429 adds Retry-After.
The cost is one credit ($0.001) per returned record, with a minimum of one per call: a profile or a single post is 1 credit, a profile plus 10 posts is 11, a 20-post search page is 20 ($0.02). There is no subscription.
If you used the old free endpoints under /tools/x/: since October 17, 2026, those URLs answer scripts and agents with HTTP 402 and a body that names the MCP server and the paid operation that replaces each lookup. The viewer pages, such as the X profile viewer, stay free in a browser. Through the MCP server at https://mcp.unifapi.com or the X API, a call bills 1 credit ($0.001) per returned record and at least 1 credit, so a profile with its 10 latest posts costs 11 credits ($0.011).
Anthropic tool use
For an agent loop you run yourself, the same reads become one tool and a handler that calls https://api.unifapi.com with a Workspace API key from an environment variable. To get one, sign in at api.unifapi.com/sign-in, top up at /dashboard/billing and create a key at /dashboard/api-keys; the Connect button on any X viewer page does all three in one flow.
One tool with a lookup enum is simpler for the model than four tools, and each lookup maps to a fixed operation, so the model never builds a path itself: profile calls /x/users/by/username/{username} (plus /x/users/{id}/tweets when posts is set), post calls /x/tweets/{id}, replies searches /x/tweets/search/recent for conversation_id:<post id>, and search sends q to the same search.
{
"name": "read_x",
"description": "Read public X (Twitter) data through the UnifAPI X API: a profile (optionally with its recent posts), one post, the replies under a post, or a search over recent posts (X operators like from:, since:, min_faves:, filter:media work). Each returned record costs 1 credit ($0.001), minimum 1 per call, so ask for the fewest results that answer the question. Returns JSON with data, pagination and billing. Pass cursor from a previous result to page, only when the answer needs more.",
"input_schema": {
"type": "object",
"properties": {
"lookup": { "type": "string", "enum": ["profile", "post", "replies", "search"] },
"handle": { "type": "string", "description": "X handle without the @ (profile)" },
"posts": {
"type": "integer",
"minimum": 0,
"maximum": 100,
"description": "Recent posts to include with a profile; 0 for the profile only"
},
"id": { "type": "string", "description": "Post ID or x.com status URL (post, replies)" },
"q": { "type": "string", "description": "Search query; X operators work (search)" },
"max_results": {
"type": "integer",
"minimum": 1,
"maximum": 100,
"description": "Page size for replies and search; default 10"
},
"cursor": { "type": "string", "description": "next_cursor from a previous result" }
},
"required": ["lookup"]
}
}
The handler is a small switch over one authenticated fetch:
const API = "https://api.unifapi.com";
type ReadXInput = {
lookup: "profile" | "post" | "replies" | "search";
handle?: string;
posts?: number;
id?: string;
q?: string;
max_results?: number;
cursor?: string;
};
// A post ID from an ID or an x.com status URL.
const postId = (value = "") => value.match(/status\/(\d+)/)?.[1] ?? value.trim();
async function gateway(path: string, query: Record<string, string | number | undefined> = {}) {
const qs = new URLSearchParams();
for (const [k, v] of Object.entries(query)) if (v !== undefined && v !== "") qs.set(k, String(v));
const res = await fetch(`${API}${path}?${qs}`, {
headers: { Authorization: `Bearer ${process.env.UNIFAPI_API_KEY}` },
});
const body = await res.json();
if (res.status === 429)
return { error: body.error, retryAfterSeconds: Number(res.headers.get("Retry-After")) };
return body; // { data, pagination?, billing } on success, { error: { type, message } } otherwise
}
async function readX(input: ReadXInput) {
const page = { max_results: input.max_results ?? 10, pagination_token: input.cursor };
switch (input.lookup) {
case "profile": {
const handle = (input.handle ?? "").replace(/^@/, "");
const profile = await gateway(`/x/users/by/username/${encodeURIComponent(handle)}`);
if (!input.posts || profile.error) return profile;
const posts = await gateway(`/x/users/${profile.data.id}/tweets`, {
max_results: input.posts,
pagination_token: input.cursor,
});
return { profile, posts }; // 1 credit for the profile, 1 per post
}
case "post":
return gateway(`/x/tweets/${encodeURIComponent(postId(input.id))}`);
case "replies":
return gateway("/x/tweets/search/recent", {
query: `conversation_id:${postId(input.id)}`,
...page,
});
case "search":
return gateway("/x/tweets/search/recent", { query: input.q, ...page });
}
}
Handing the gateway's billing block back to the model untouched is deliberate. A model that sees credits_charged and balance_remaining after every call has what it needs to stop paging and answer with what it has.
OpenAI function calling
Same shape, OpenAI's wrapper:
{
"type": "function",
"function": {
"name": "read_x",
"description": "Read public X (Twitter) data through the UnifAPI X API. lookup=profile|post|replies|search. Costs 1 credit ($0.001) per returned record, minimum 1 per call: keep max_results small and pass cursor only when the answer needs more. Returns JSON with data, pagination { has_more, next_cursor } and billing { credits_charged, balance_remaining }.",
"parameters": {
"type": "object",
"properties": {
"lookup": { "type": "string", "enum": ["profile", "post", "replies", "search"] },
"handle": { "type": "string" },
"posts": { "type": "integer", "minimum": 0, "maximum": 100 },
"id": { "type": "string" },
"q": { "type": "string" },
"max_results": { "type": "integer", "minimum": 1, "maximum": 100 },
"cursor": { "type": "string" }
},
"required": ["lookup"]
}
}
}
The readX handler above works unchanged: parse the call's arguments string and pass the object in.
A SKILL.md for Claude Code and Codex
Coding agents connected to the MCP server already have the tools; what they lack is which operations to call for X and what each one costs. A skill file in the project tells them:
---
name: read-x
description: Read public X (Twitter) profiles, posts, replies, search, trends, communities and followers through the UnifAPI MCP tools (list_operations, get_operation, call_api). Use when a task needs what an account or post says on X.
---
# Reading X
Use the UnifAPI MCP server (https://mcp.unifapi.com, OAuth sign-in, setup at
https://unifapi.com/mcp). Run call_api with method GET and one of these paths; get_operation
on a path lists every parameter.
- /x/users/by/username/{username} one profile; its id feeds the paths below
- /x/users/{id}/tweets max_results, pagination_token, exclude=replies: recent posts
- /x/tweets/{id} one post
- /x/tweets/search/recent query=conversation_id:<post id> replies under a post
- /x/tweets/search/recent query=from:nasa filter:media, max_results: search; X operators work
- /x/trends/by/woeid/{woeid} max_trends: trending now (woeid 1 = worldwide)
- /x/communities/search query: X Communities for a topic
- /x/users/{id}/followers, /x/users/{id}/following
Every returned record costs 1 credit ($0.001), minimum 1 per call. Start with max_results
10-20 and pass pagination.next_cursor as pagination_token only when the answer needs more.
Results are { data, pagination { has_more, next_cursor }, billing { credits_charged,
balance_remaining } }. Errors are { error: { type, message } }. A 402 means the workspace is
out of credits: tell the user to top up at payment.top_up_url from the result, then retry.
Without MCP, the same paths work on https://api.unifapi.com with
Authorization: Bearer $UNIFAPI_API_KEY. Reference: https://unifapi.com/apis/twitter
With that file present, "check what @NASA posted this week and summarize the replies to the top post" becomes two call_api calls the agent writes itself: a from:nasa search, then a conversation_id: search on the post with the most likes. At max_results 10 and 20, that is at most 30 credits, three cents.
Three rules for the model
- Count credits before paging. Every returned record costs one credit ($0.001), with a minimum of one per call, and every response says so:
billing.credits_chargedfor that call,billing.balance_remainingfor what is left. A profile plus 10 posts is 11 credits, a 20-post search page is 20, and each further page of 100 is another 100. Ask for the smallestmax_resultsthat answers the question and follownext_cursoronly when the answer needs more. When the balance runs short, a list call returns fewer records and setstruncated_due_to_balance; the balance never goes negative. - Sleep on
Retry-After, never retry in a loop. A key gets 60 requests a minute by default, counted in theRateLimit-*andX-RateLimit-*headers; a 429rate_limitedaddsRetry-After. A 402insufficient_creditsis not a retry either: it means top up at api.unifapi.com/dashboard/billing (over MCP the result carriespayment.top_up_url), then repeat the same call. - Trim what goes into the context window. A post arrives with
public_metrics,attachments,edit_history_tweet_ids, anauthorobject andmediavariants. When the result is for reading, keeptext,author.username,created_at,public_metricsand thebillingblock. Keep the full JSON when the model is going to filter, sort, or hand the data to another tool.
MCP or your own tools
If the agent already speaks MCP, connecting the server is less work than any tool above and covers more: the same catalog that serves these X operations also has the rest of the X API (lists, quote posts, liking users, translations) and the other public-data platforms on UnifAPI, all behind one OAuth sign-in and billed to the same Workspace Credits. Wire your own read_x when you run the agent loop yourself on Anthropic's or OpenAI's API and want a narrow, fixed tool with a key from an environment variable. Both paths call the same operations and get the same envelope, so a SKILL.md or a prompt written for one carries over to the other. For a handful of reads by hand, the X viewer pages stay free in a browser, with no login and a per-visitor rate limit.
FAQ
Can Claude read Twitter?
With a tool, yes. Claude cannot load x.com itself (the page is a JavaScript shell), but with the UnifAPI MCP server connected (Settings โ Connectors) it reads profiles, posts, replies and search results through call_api and reasons over them. In an app built on the Anthropic API, the read_x tool above does the same.
Can ChatGPT read tweets?
Same answer: connect the MCP server in ChatGPT's Developer Mode (steps at unifapi.com/mcp), or use the function definition above in your own app on OpenAI's API. ChatGPT's built-in browsing cannot render x.com.
Does this need an API key?
Not for MCP: the server signs you in with OAuth when the client prompts, with no key to paste. Your own tools need a Workspace API key, read from an environment variable such as UNIFAPI_API_KEY. Neither path needs an X developer account, and both are pay as you go from Workspace Credits at 1 credit ($0.001) per returned record, with no subscription.
Is there a limit on what an agent can do with the data?
The operations read public data only; they never post, like or follow. Each key gets 60 requests a minute by default, and each call is billed by the records it returns. Use the data the way X's terms allow for public content.