TwitterAPI MCP服务器
一个模型上下文协议(MCP)服务器,通过 推特API.io 服务。此服务器使Claude和其他MCP客户端能够与Twitter的生态系统进行交互,而无需Twitter开发人员帐户批准。
归因这个项目是 下载 kinhunt/twitterapi-mcp 修复了错误并进行了改进,以与官方TwitterAPI.io文档相匹配。
特性
- 用户信息:获取详细的用户资料、关注者和以下列表
- 推特操作:搜索推文,获取推文详细信息、回复和用户时间线
- 搜索功能:推文和用户的高级搜索
- 写入操作:发布推文并与内容互动(需要登录)
- 分页支持:所有列表端点都支持基于光标的分页
- 企业就绪:代理支持和强大的错误处理
- 无Twitter身份验证:使用TwitterAPI.io,不需要Twitter开发人员批准
安装
npx快速入门(推荐)
npx twitterapi-mcp-server全球安装
npm install -g twitterapi-mcp-server本地安装
npm install twitterapi-mcp-server配置
获取API密钥
- 访问 推特API.io
- 创建帐户并登录
- 从仪表板获取API密钥
- API密钥格式如下所示:
new1_xxxxxxxxxxxxxxxxxxxxx
环境变量
| 变量 | 必填 | 描述 |
|---|---|---|
TWITTERAPI_API_KEY | 是 | 您的TwitterAPI.io API密钥 |
PROXY_URL | 否 | 企业环境的代理URL |
HTTP_PROXY | 否 | 替代代理配置 |
HTTPS_PROXY | 否 | 替代代理配置 |
MCP客户端配置
克劳德桌面版
将此添加到您的Claude Desktop配置文件中:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
视窗: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"twitterapi": {
"command": "npx",
"args": ["-y", "twitterapi-mcp-server"],
"env": {
"TWITTERAPI_API_KEY": "your_api_key_here"
}
}
}
}带代理的Claude桌面
{
"mcpServers": {
"twitterapi": {
"command": "npx",
"args": ["-y", "twitterapi-mcp-server"],
"env": {
"TWITTERAPI_API_KEY": "your_api_key_here",
"PROXY_URL": "http://proxy.company.com:8080"
}
}
}
}光标IDE
添加到光标MCP设置(.cursor/mcp.json):
{
"mcpServers": {
"twitterapi": {
"command": "npx",
"args": ["-y", "twitterapi-mcp-server"],
"env": {
"TWITTERAPI_API_KEY": "your_api_key_here"
}
}
}
}克劳德代码
添加到您的Claude Code MCP设置中(~/.claude/settings.json):
{
"mcpServers": {
"twitterapi": {
"command": "npx",
"args": ["-y", "twitterapi-mcp-server"],
"env": {
"TWITTERAPI_API_KEY": "your_api_key_here"
}
}
}
}直接与Node一起使用
如果您更喜欢直接使用Node而不是npx运行:
{
"mcpServers": {
"twitterapi": {
"command": "node",
"args": ["/path/to/twitterapi-mcp-server/build/index.js"],
"env": {
"TWITTERAPI_API_KEY": "your_api_key_here"
}
}
}
}可用工具
用户信息
| 工具 | 描述 | 必需参数 | 可选参数 |
|---|---|---|---|
get_user_by_username | 按用户名获取用户详细信息 | username | - |
get_user_by_id | 按用户ID获取用户详细信息 | user_id | - |
get_user_followers | 获取用户的关注者(200/页) | username | cursor, pageSize |
get_user_following | 让用户有人关注(200/页) | username | cursor, pageSize |
search_users | 按关键字搜索用户 | query | cursor |
推特操作
| 工具 | 描述 | 必需参数 | 可选参数 |
|---|---|---|---|
get_user_tweets | 获取用户的推文(20/页) | username 或 userId | cursor, includeReplies |
search_tweets | 按关键字搜索推文 | query | queryType (最新/顶部), cursor |
get_tweet_by_id | 按ID获取推文 | tweet_ids (数组) | - |
get_tweet_replies | 获取推文的回复(20/页) | tweetId | cursor, sinceTime, untilTime |
写入操作(需要登录)
| 工具 | 描述 | 必需参数 | 可选参数 |
|---|---|---|---|
login_user | 登录推特帐户 | user_name, email, password, proxy | totp_secret |
create_tweet | 发布新推文 | tweet_text, proxy | reply_to_tweet_id, attachment_url, media_ids |
例子
获取用户信息
// Get user by username
await get_user_by_username({ username: "elonmusk" })
// Get user followers with pagination
await get_user_followers({
username: "elonmusk",
pageSize: 100
})
// Get next page using cursor
await get_user_followers({
username: "elonmusk",
cursor: "next_cursor_from_previous_response"
})搜索和检索推文
// Search latest tweets
await search_tweets({
query: "artificial intelligence",
queryType: "Latest"
})
// Search top/popular tweets
await search_tweets({
query: "OpenAI",
queryType: "Top"
})
// Advanced search with operators
await search_tweets({
query: "AI from:elonmusk since:2024-01-01"
})
// Get user's recent tweets
await get_user_tweets({ username: "openai" })
// Get user's tweets including replies
await get_user_tweets({
username: "openai",
includeReplies: true
})
// Get specific tweets by IDs
await get_tweet_by_id({
tweet_ids: ["1234567890123456789", "9876543210987654321"]
})
// Get replies to a tweet
await get_tweet_replies({
tweetId: "1234567890123456789"
})创建内容(需要登录)
// Login first (requires residential proxy)
await login_user({
user_name: "your_username",
email: "your_email@example.com",
password: "your_password",
proxy: "http://user:pass@proxy:port"
})
// Post a tweet
await create_tweet({
tweet_text: "Hello from MCP!",
proxy: "http://user:pass@proxy:port"
})
// Reply to a tweet
await create_tweet({
tweet_text: "Great point!",
reply_to_tweet_id: "1234567890123456789",
proxy: "http://user:pass@proxy:port"
})分页
所有列表端点都返回基于光标导航的分页结果:
{
"data": [...],
"has_next_page": true,
"next_cursor": "cursor_string_for_next_page"
}要获取下一页,请传递 next_cursor 值为 cursor 参数在您的下一个请求中。
API定价
TwitterAPI.io提供按需付费的定价:
| 操作 | 价格 |
|---|---|
| 推特 | 每1000条0.15美元 |
| 用户资料 | 1000美元 |
| 关注者/关注者 | 每1000人0.15美元 |
| 登录 | 每次通话0.003美元 |
| 创建推特 | 每次通话0.003美元 |
- 最低费用:每次请求0.00015美元
- 无月费
- 免费试用积分
- 学生和研究机构的折扣率
发展
从源头构建
git clone https://github.com/Jing-yilin/twitterapi-mcp-server.git
cd twitterapi-mcp-server
npm install
npm run build运行测试
# Using bun
bun test
# Or with npm (requires bun installed)
npm test手动测试服务器
# Set your API key
export TWITTERAPI_API_KEY="your_api_key"
# Test tools list
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {}}' | node build/index.js
# Test a tool call
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "get_user_by_username", "arguments": {"username": "elonmusk"}}}' | node build/index.js项目结构
twitterapi-mcp-server/
├── src/
│ ├── index.ts # Main server implementation
│ ├── index.test.ts # Unit tests
│ └── integration.test.ts # Integration tests
├── build/ # Compiled JavaScript
├── package.json
├── tsconfig.json
└── README.md错误处理
服务器处理常见错误:
| 错误 | 原因 | 解决方案 |
|---|---|---|
| 401未经授权 | API密钥无效 | 请检查您的 TWITTERAPI_API_KEY |
| 402需要付款 | 积分不足 | 在TwitterAPI.io仪表板上添加积分 |
| 429速率受限 | 请求太多 | 等待重试,或降低请求速率 |
| 400错误请求 | 无效参数 | 检查参数名称和格式 |
安全考虑
- 将API键存储为环境变量,从不存储在代码中
- 登录凭据仅用于身份验证,而不是持久存储
- 所有API请求都使用HTTPS
- 代理支持可满足企业安全要求
- 这
login_cookie来自登录的信息仅存储在会话的内存中
故障排除
“未经授权”错误
- 验证您的API密钥是否正确
- 检查一下
TWITTERAPI_API_KEY环境变量已设置
“积分不足”错误
- 将点数添加到您的TwitterAPI.io帐户
- 在仪表板上查看您的使用情况
服务器未启动
- 确保已安装Node.js>=18.0.0
- 跑
npm run build编译TypeScript - 检查stderr中的错误消息
代理问题
- 验证代理URL格式:
http://user:pass@host:port - 独立测试代理连接
- 对于HTTPS代理,请使用
HTTPS_PROXY变量
贡献
- 复刻仓库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
支持
- API文档: 文档.twitterapi.io
- 问题:
- TwitterAPI.io支持: 网站 twitterapi.io
致谢
- 最初分叉自 下载 kinhunt/twitterapi-mcp
- 建立在 推特API.io 服务
- 使用 模型上下文协议
- 成长中的MCP生态系统的一部分
______________________________________________________________________
备注:这是TwitterAPI.io的非官方MCP服务器。使用此工具时,请确保遵守Twitter的服务条款。
