这是一个为X打造的增强型模型上下文协议(MCP)服务器,在原实现基础上增加了OAuth 2.0支持、v2 API媒体上传功能,以及全面的速率限制。
在开始之前,你需要完成以下准备工作:
本服务器支持两种认证方法,你可以根据需求进行选择:
%APPDATA%\Claude\claude_desktop_config.json
~/Library/Application Support/Claude/claude_desktop_config.json
添加以下配置:{
"mcpServers": {
"twitter-mcp": {
"command": "npx",
"args": ["-y", "@mbelinky/x-mcp-server"],
"env": {
"API_KEY": "your_api_key_here",
"API_SECRET_KEY": "your_api_secret_key_here",
"ACCESS_TOKEN": "your_access_token_here",
"ACCESS_TOKEN_SECRET": "your_access_token_secret_here"
}
}
}
}
若使用OAuth 2.0:{
"mcpServers": {
"twitter-mcp": {
"command": "npx",
"args": ["-y", "@mbelinky/x-mcp-server"],
"env": {
"AUTH_TYPE": "oauth2",
"OAUTH2_CLIENT_ID": "your_client_id",
"OAUTH2_CLIENT_SECRET": "your_client_secret",
"OAUTH2_ACCESS_TOKEN": "your_access_token",
"OAUTH2_REFRESH_TOKEN": "your_refresh_token"
}
}
}
}
git clone https://github.com/mbelinky/x-mcp-server.git
cd x-mcp-server/twitter-mcp
npm install
npm run build
然后更新配置文件,指向本地安装路径:{
"mcpServers": {
"twitter-mcp": {
"command": "node",
"args": ["/path/to/twitter-mcp/build/index.js"],
"env": {
// ... 你的凭证
}
}
}
}
全局安装服务器并添加到Claude:
# 对于OAuth 1.0a
claude mcp add twitter-mcp "npx" "-y" "@mbelinky/x-mcp-server" --scope user \
--env "API_KEY=your_api_key" \
--env "API_SECRET_KEY=your_secret_key" \
--env "ACCESS_TOKEN=your_access_token" \
--env "ACCESS_TOKEN_SECRET=your_access_token_secret"
# 对于OAuth 2.0
claude mcp add twitter-mcp "npx" "-y" "@mbelinky/x-mcp-server" --scope user \
--env "AUTH_TYPE=oauth2" \
--env "OAUTH2_CLIENT_ID=your_client_id" \
--env "OAUTH2_CLIENT_SECRET=your_client_secret" \
--env "OAUTH2_ACCESS_TOKEN=your_access_token" \
--env "OAUTH2_REFRESH_TOKEN=your_refresh_token"
本服务器会根据认证方法和操作智能使用不同的X API版本:
upload.twitter.com
)api.x.com/2/media/upload
)本服务器支持两种认证方法,你可以根据需求进行选择:
API_KEY=your_api_key_here
API_SECRET_KEY=your_api_secret_key_here
ACCESS_TOKEN=your_access_token_here
ACCESS_TOKEN_SECRET=your_access_token_secret_here
# 首先克隆此仓库
git clone https://github.com/mbelinky/x-mcp-server.git
cd x-mcp-server/twitter-mcp
npm install
# 运行OAuth2设置脚本
node scripts/oauth2-setup.js
选项B - 手动设置:
使用带有PKCE的OAuth 2.0流程,所需范围:tweet.read
, tweet.write
, users.read
, media.write
, offline.access
,用授权码交换访问令牌。AUTH_TYPE=oauth2
OAUTH2_CLIENT_ID=your_client_id_here
OAUTH2_CLIENT_SECRET=your_client_secret_here
OAUTH2_ACCESS_TOKEN=your_access_token_here
OAUTH2_REFRESH_TOKEN=your_refresh_token_here
安装完成后,Claude可以使用以下工具:
post_tweet
发布新推文,可选择附带媒体和回复。 示例提示:
search_tweets
搜索推文,可自定义结果数量(10 - 100)。 示例提示:
delete_tweet
根据推文ID删除推文。 示例提示:
当使用Claude发布附带图片的推文时:
示例用法:
# ✅ 推荐用于Claude
"Post tweet with image at /Users/me/photos/sunset.png"
# ❌ Claude目前不支持
"Post this image: [pasting an image directly]"
# ✅ 编程方式可用
// 在代码中,你仍然可以使用Base64
{
"text": "Hello world!",
"media": [{
"data": "iVBORw0KGgoAAAANS...",
"media_type": "image/png"
}]
}
项目包含全面的测试:
# 运行所有测试
npm test
# 运行特定测试套件
npm test -- --testNamePattern="OAuth"
npm test -- --testPathPattern="unit"
git clone https://github.com/mbelinky/x-mcp-server.git
cd x-mcp-server/twitter-mcp
npm install
npm run build # 构建TypeScript
npm run dev # 在开发模式下运行
npm test # 运行测试
npm run lint # 代码检查
npm run format # 格式化代码
创建一个.env
文件用于本地开发:
# OAuth 1.0a
API_KEY=your_api_key
API_SECRET_KEY=your_api_secret_key
ACCESS_TOKEN=your_access_token
ACCESS_TOKEN_SECRET=your_access_token_secret
# OAuth 2.0(如果使用)
AUTH_TYPE=oauth2
OAUTH2_CLIENT_ID=your_client_id
OAUTH2_CLIENT_SECRET=your_client_secret
OAUTH2_ACCESS_TOKEN=your_access_token
OAUTH2_REFRESH_TOKEN=your_refresh_token
# 可选
DEBUG=true # 启用调试日志
现在OAuth 1.0a和OAuth 2.0都支持媒体上传!
注意:OAuth 2.0进行媒体上传需要media.write
范围。
Twitter的v2删除端点目前存在问题(返回500错误)。MCP服务器会优雅地处理此问题:
这是Twitter API的临时问题。问题解决后,两种认证方法都将使用v2删除功能。
"Could not authenticate you"
"Rate limit exceeded"
"Media upload failed"
media.write
范围"403 Forbidden"
通过设置DEBUG
环境变量启用详细日志记录:
{
"env": {
"DEBUG": "true",
// ... 其他凭证
}
}
%APPDATA%\Claude\logs\mcp-server-twitter.log
~/Library/Logs/Claude/mcp-server-twitter.log
欢迎贡献代码!请按照以下步骤进行:
本MCP服务器:
你的推文、搜索和媒体在你和Twitter/X之间保持私密。
对于安全漏洞,请直接发送电子邮件,而不是创建公开问题。
MIT
这是 @enescinar/twitter-mcp 的增强版分支,增加了以下功能:
原始实现由 @enescinar 完成。