给你的 AI 智能体装一个 Twitter 阅读器:Claude、ChatGPT 和 Codex 的 MCP 与工具定义

更新于
连接 UnifAPI MCP 服务器,或粘贴付费 X API 的工具定义,就能让 Claude、ChatGPT 或 Codex 读取公开 X 主页、帖子、回复和搜索结果。
一句话总结:智能体读 X 需要四样东西:一个能用用户名或帖子链接调用的操作、一个它能读的 JSON 响应体、一个它能据以分支的错误,以及一个它看得见的费用。四样一次凑齐的最简单办法是 UnifAPI MCP 服务器:在 Claude、ChatGPT 或 Codex 里添加
https://mcp.unifapi.com,客户端提示时用 OAuth 登录(无需粘贴密钥),智能体就能通过call_api读 X,按量付费,每返回一条记录 1 积分($0.001)。如果你自己跑智能体循环,下面给出 Anthropic 工具调用和 OpenAI 函数调用的可复制定义(附调用付费 X API 的处理函数)、给 Claude Code 和 Codex 用的 SKILL.md,外加模型需要的三条规则:翻页前先算积分,按Retry-After休眠,精简放进上下文窗口的内容。
模型早就知道怎么读 X。问 Claude 或 ChatGPT @NASA 这周发了什么,它们会去试:网页搜索一下,抓一次 x.com,也许再拼一个训练数据里的 Nitter URL。搜索返回的是摘要,抓取返回的是一个 JavaScript 空壳,Nitter 实例早就没了。然后模型要么瞎猜,要么道歉。
解法不是更聪明的模型,而是一个工具:它的操作模型能调用,它的响应体模型能解析。本文就是这个工具,以人们真正会部署的形式给出:先是 MCP 服务器,再是你自己接入的工具。
先连接 MCP 服务器
如果智能体跑在 Claude、ChatGPT 或 Codex 里,你根本不用写工具。添加 UnifAPI MCP 服务器后,智能体会得到三个工具:用来搜索目录的 list_operations、用来查看单个操作参数的 get_operation,以及用来执行操作的 call_api。各客户端(包括 Cursor 等)的步骤见 unifapi.com/mcp,简要版如下:
- Claude: Settings → Connectors → Add custom connector。命名为 UnifAPI,粘贴
https://mcp.unifapi.com,把 Authentication 保持为 OAuth,然后 Add 并 Connect。 - ChatGPT: 在 Plus、Pro、Team 或 Enterprise 套餐下启用 Developer Mode(Settings → Connectors → Advanced),点击 Create app,把同一个 URL 粘贴到 MCP Server URL,Authentication 保持为 OAuth。
- Codex: 运行
codex mcp add unifapi --url https://mcp.unifapi.com,如果登录页没有自动打开,再运行codex mcp login unifapi(详见 Codex MCP 设置)。 - Claude Code: 运行
claude mcp add --transport http unifapi https://mcp.unifapi.com。
客户端会打开浏览器,登录你的 UnifAPI 工作区;无需粘贴密钥。调用消耗工作区积分,按量付费。之后用平常的话提问("@NASA 最近发了哪些带图片或视频的帖子?"),智能体就会发出类似这样的调用:
{
"path": "/x/tweets/search/recent",
"method": "GET",
"query": { "query": "from:nasa filter:media", "max_results": 20 }
}
这次调用最多返回 20 条帖子,最多花 20 积分($0.02)。服务器的更多细节见 MCP 文档。
X 操作
无论智能体通过 call_api 调用,还是你自己的处理函数通过 HTTP 调用,读 X 用的都是付费 X API 上同一组 GET 操作(参考页 unifapi.com/apis/twitter,完整规范 https://api.unifapi.com/openapi.json):
| 查询 | 操作 | 参数 | 返回 |
|---|---|---|---|
| 主页 | /x/users/by/username/{username} | username(不带 @) | 一个主页,含数字 id 和各项指标 |
| 时间线 | /x/users/{id}/tweets | max_results(1–100)、pagination_token、exclude | 一个账号的帖子,按时间倒序 |
| 帖子 | /x/tweets/{id} | id | 一条帖子,含作者、媒体、指标 |
| 媒体 | /x/tweets/search/recent | query=from:<handle> filter:media | 一个账号近期的照片和视频帖子 |
| 回复 | /x/tweets/search/recent | query=conversation_id:<post id> | 一条帖子下的公开回复 |
| 搜索 | /x/tweets/search/recent | query、max_results(1–100)、pagination_token | 近期帖子;支持 X 搜索运算符 |
| 热搜 | /x/trends/by/woeid/{woeid} | woeid(1 为全球)、max_trends(1–50) | 当前热搜 |
| 社区 | /x/communities/search | query | 某话题的 X 社区 |
| 关注者 | /x/users/{id}/followers、/x/users/{id}/following | max_results(1–100)、pagination_token | 谁在关注一个账号,或它关注了谁 |
每个响应都是同一个信封:
{
"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 是对象,列表时是数组,列表还带 pagination;要下一页,就把 next_cursor 作为 pagination_token 传回去。字段名沿用 X API v2:帖子有 text、created_at、conversation_id、public_metrics 和 author;用户有 username、description 和 public_metrics.followers_count。每个错误都是 { error: { type, message } },类型是一个封闭集合(unauthorized、insufficient_credits、not_found、rate_limited、upstream_error 等几种)。每个 HTTP 响应都带 RateLimit-* 和 X-RateLimit-* 头;429 另加 Retry-After。
费用是每返回一条记录 1 积分($0.001),每次调用最少 1 积分:一个主页或一条帖子 1 积分,一个主页加 10 条帖子 11 积分,一页 20 条帖子的搜索 20 积分($0.02)。没有订阅。
如果你用过 /tools/x/ 下的旧免费端点:这些 URL 自 2026 年 10 月 17 日起会对脚本和智能体返回 HTTP 402,响应体里写明 MCP 服务器,以及替代每种查询的付费操作。查看器页面,比如 X 主页查看器,在浏览器里依然免费。通过 MCP 服务器(https://mcp.unifapi.com)或 X API,每返回一条记录收 1 个积分($0.001),每次调用至少 1 个积分,所以一个账号资料加最近 10 条帖子要 11 个积分($0.011)。
Anthropic 工具调用
在你自己运行的智能体循环里,同样的读取就变成一个工具,加一个处理函数:它用从环境变量读取的工作区 API 密钥调用 https://api.unifapi.com。获取密钥的方法:在 api.unifapi.com/sign-in 登录,在 /dashboard/billing 充值,再到 /dashboard/api-keys 创建密钥;任意 X 查看器页面上的 Connect 按钮能一次走完这三步。
对模型来说,一个带 lookup 枚举的工具比四个工具更简单,而且每种查询都对应一个固定的操作,模型永远不用自己拼路径:profile 调用 /x/users/by/username/{username}(设置了 posts 时再加 /x/users/{id}/tweets),post 调用 /x/tweets/{id},replies 在 /x/tweets/search/recent 里搜索 conversation_id:<post id>,search 把 q 发给同一个搜索。
{
"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"]
}
}
处理函数就是包着一次带认证 fetch 的小 switch:
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 });
}
}
把网关的 billing 块原样返回给模型是刻意为之。每次调用后都能看到 credits_charged 和 balance_remaining 的模型,就有了停止翻页、用手头结果作答所需的信息。
OpenAI 函数调用
同样的形状,换成 OpenAI 的外壳:
{
"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"]
}
}
}
上面的 readX 处理函数无需改动:解析调用里的 arguments 字符串,把对象传进去即可。
给 Claude Code 和 Codex 的 SKILL.md
连上 MCP 服务器的编码智能体已经有了工具;它们缺的是读 X 该调用哪些操作、每个要花多少。项目里放一个 skill 文件就能告诉它们:
---
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
有了这个文件,"看看 @NASA 这周发了什么,并总结最热门那条帖子下的回复"就变成智能体自己写出的两次 call_api 调用:一次 from:nasa 搜索,再对点赞最多的那条帖子做一次 conversation_id: 搜索。max_results 设为 10 和 20 时,最多 30 积分,也就是 3 美分。
给模型的三条规则
- 翻页前先算积分。 每返回一条记录花 1 积分($0.001),每次调用最少 1 积分,而且每个响应都会写明:
billing.credits_charged是这次调用花的,billing.balance_remaining是还剩的。一个主页加 10 条帖子是 11 积分,一页 20 条帖子的搜索是 20,每多翻一页 100 条就再加 100。请求能回答问题的最小max_results,只有答案确实需要更多时才跟着next_cursor翻页。余额不足时,列表调用会少返回记录并标记truncated_due_to_balance;余额绝不会变成负数。 - 按
Retry-After休眠,绝不循环重试。 每个密钥默认每分钟 60 次请求,计数在RateLimit-*和X-RateLimit-*头里;429rate_limited另加Retry-After。402insufficient_credits也不是该重试的情况:它意味着先到 api.unifapi.com/dashboard/billing 充值(经由 MCP 时,结果里带有payment.top_up_url),再重复同一次调用。 - 精简放进上下文窗口的内容。 一条帖子会带着
public_metrics、attachments、edit_history_tweet_ids、一个author对象和media的多个版本。结果是拿来读的,就只留text、author.username、created_at、public_metrics和billing块;模型要过滤、排序或把数据交给另一个工具时,再保留完整 JSON。
MCP 还是自己的工具
如果智能体已经会说 MCP,连接服务器比上面任何一个工具都省事,覆盖面也更广:提供这些 X 操作的同一个目录里,还有 X API 的其余部分(列表、引用帖、点赞用户、翻译)以及 UnifAPI 上其他公开数据平台,全部只需一次 OAuth 登录,从同一份工作区积分里计费。当你在 Anthropic 或 OpenAI 的 API 上自己跑智能体循环,想要一个用环境变量里的密钥、范围窄而固定的工具时,再自己接一个 read_x。两条路调用的是同样的操作,拿到的是同样的信封,所以为其中一条写的 SKILL.md 或提示词,另一条也能直接用。如果只是手动查几次,X 查看器页面在浏览器里依然免费,无需登录,按访客限流。
常见问题
Claude 能读 Twitter 吗?
有工具就能。Claude 自己加载不了 x.com(页面是个 JavaScript 空壳),但连上 UnifAPI MCP 服务器之后(Settings → Connectors),它就能通过 call_api 读取主页、帖子、回复和搜索结果并据此推理。在基于 Anthropic API 构建的应用里,上面的 read_x 工具也能做到同样的事。
ChatGPT 能读推文吗?
同样的答案:在 ChatGPT 的 Developer Mode 里连接 MCP 服务器(步骤见 unifapi.com/mcp),或者在你自己基于 OpenAI API 的应用里使用上面的函数定义。ChatGPT 内置的浏览功能渲染不了 x.com。
这需要 API 密钥吗?
用 MCP 不需要:客户端提示时,服务器会让你用 OAuth 登录,无需粘贴密钥。你自己的工具需要一个工作区 API 密钥,从 UNIFAPI_API_KEY 这样的环境变量里读取。两条路都不需要 X 开发者账号,也都是用工作区积分按量付费:每返回一条记录 1 积分($0.001),没有订阅。
智能体能用这些数据做的事有限制吗?
这些操作只读公开数据,从不发帖、点赞或关注。每个密钥默认每分钟 60 次请求,每次调用按返回的记录数计费。按 X 服务条款对公开内容的允许范围使用数据。