Twitter MCP 服务器
用于 Twitter API 集成的模型上下文协议服务器实现。
设置
克隆存储库
安装依赖项:
npm install将
.env.example复制到.env并填写您的 Twitter API 凭据构建项目:
npm run build启动服务器:
npm start
Related MCP server: MCP-Twikit
环境变量
.env中所需的 Twitter API 凭据:
X_API_KEY=your_api_key
X_API_SECRET=your_api_secret
X_ACCESS_TOKEN=your_access_token
X_ACCESS_TOKEN_SECRET=your_access_token_secret可用工具
推文操作
postTweet:发布新推文{ "text": "Your tweet text here" }postTweetWithMedia:发布带有媒体附件的推文{ "text": "Your tweet text", "mediaPath": "path/to/media/file", "mediaType": "image/jpeg|image/png|image/gif|video/mp4", "altText": "Optional alt text for accessibility" }getTweetById:通过 ID 获取特定推文{ "tweetId": "tweet_id", "tweetFields": ["created_at", "public_metrics"] }replyToTweet:回复现有推文{ "tweetId": "tweet_id", "text": "Your reply text" }deleteTweet:删除推文{ "tweetId": "tweet_id" }
搜索与分析
searchTweets:搜索推文{ "query": "search query", "maxResults": 10, "tweetFields": ["created_at", "public_metrics"] }getHashtagAnalytics:获取主题标签的分析数据{ "hashtag": "hashtag", "startTime": "ISO-8601 date", "endTime": "ISO-8601 date" }
用户操作
getUserInfo:获取用户信息{ "username": "twitter_username", "fields": ["description", "public_metrics"] }getUserTimeline:获取用户的推文{ "username": "twitter_username", "maxResults": 10, "tweetFields": ["created_at", "public_metrics"] }getFollowers:获取用户的关注者{ "username": "twitter_username", "maxResults": 100, "userFields": ["description", "public_metrics"] }getFollowing:获取用户关注的帐户{ "username": "twitter_username", "maxResults": 100, "userFields": ["description", "public_metrics"] }
订婚
likeTweet:喜欢一条推文{ "tweetId": "tweet_id" }unlikeTweet:与推文不同{ "tweetId": "tweet_id" }retweet:转发一条推文{ "tweetId": "tweet_id" }undoRetweet:撤消转发{ "tweetId": "tweet_id" }getRetweets:获取转发推文的用户{ "tweetId": "tweet_id", "maxResults": 100, "userFields": ["description", "public_metrics"] }getLikedTweets:获取用户喜欢的推文{ "userId": "user_id", "maxResults": 100, "tweetFields": ["created_at", "public_metrics"] }
列表管理
createList:创建新列表{ "name": "List name", "description": "List description", "isPrivate": false }addUserToList:将用户添加到列表{ "listId": "list_id", "username": "twitter_username" }removeUserFromList:从列表中删除用户{ "listId": "list_id", "username": "twitter_username" }getListMembers:获取列表成员{ "listId": "list_id", "maxResults": 100, "userFields": ["description", "public_metrics"] }
错误处理
所有工具都返回标准化的错误响应:
缺少参数:
Missing required parameter: parameter_nameAPI 错误:来自 Twitter API 的错误消息
未找到错误:针对资源的适当“未找到”消息
响应格式
所有成功的响应都遵循以下格式:
{
"content": [
{
"type": "text",
"text": "Operation result message"
}
]
}发展
构建:
npm run build开始:
npm start观察模式:
npm run dev
Here's a comprehensive status report of all Twitter tools:
## Working Tools (✓)
1. postTweet状态:运行正常 响应:返回推文ID 最新测试:成功
2. getTweetById状态:运行正常响应:返回完整的推文数据最新测试:成功
3. likeTweet & unlikeTweet状态:运行正常响应:确认操作最新测试:成功
4. retweet & undoRetweet状态:运行正常响应:确认操作最新测试:成功
5. replyToTweet状态:运行正常 响应:返回回复推文ID 最新测试:成功
6. getUserInfo状态:运行正常 响应:完成用户资料数据 最新测试:成功
7. followUser & unfollowUser状态:运行正常响应:确认操作最新测试:成功
8. createList状态:运行正常响应:确认列表创建最新测试:成功
9. getUserLists状态:运行正常响应:返回拥有者和成员列表最新测试:成功
## Tools with Issues (⚠️)
1. getUserTimeline状态:错误 400 错误:请求参数无效 需要修复:参数验证
2. searchTweets状态:错误 400 错误:请求参数无效 需要修复:查询参数格式
3. getLikedTweets状态:错误 400 错误:请求参数无效 需要修复:参数验证
## Missing Tools (❌)
- getHomeTimeline (not found in available tools)
- getFollowers (not available)
- getFollowing (not available)
- getHashtagAnalytics (not available)
## Priority Fixes Needed
1. Parameter Validation:
```typescript
// Implement for getUserTimeline, searchTweets, getLikedTweets
interface TwitterParamValidator {
validateTimelineParams(params: any): boolean;
validateSearchParams(params: any): boolean;
validateLikedTweetsParams(params: any): boolean;
}错误处理:
// Enhance error handling for 400 errors
interface TwitterErrorHandler {
handle400Error(endpoint: string, params: any): void;
logErrorDetails(error: any): void;
suggestParameterFixes(params: any): string[];
}