返回市场
推特-MCP服务器

推特-MCP服务器

作者:gkydev10 星标更新:2025-06-28

项目介绍

Twitter MCP 服务器

这是一个使用 twikit 库提供 Twitter 功能的模型上下文协议(MCP)服务器。该服务器允许AI助手通过标准化协议与Twitter进行交互,采用基于cookie的身份验证——LLM模型在工具调用中直接提供ct0auth_token cookie。

特性

  • Cookie身份验证:LLM模型在工具调用中直接提供ct0auth_token cookie
  • 会话缓存:自动缓存已认证的会话以提高效率
  • 时间线访问:获取您的时间线中的推文
  • 用户信息:检索用户资料和统计数据
  • 推文搜索:根据特定查询搜索推文
  • 推文管理:发布、点赞和转发推文
  • 用户推文:获取特定用户的推文
  • 直接消息:发送DM,获取DM历史记录,对消息做出反应并删除消息
  • 身份验证测试:在使用前测试cookies
  • 推文操作:发布推文,点赞/取消点赞推文,转发/取消转发推文,收藏推文
  • 推文检索:通过ID获取推文,搜索推文,获取用户时间线,获取推文回复
  • 用户操作:关注/取消关注用户,获取用户信息,搜索用户
  • 热门话题:获取不同类别(热门、新闻、体育、娱乐、为您推荐)的热门话题

免责声明

本项目利用非官方API通过twikit库与X(前身为Twitter)进行交互。所使用的认证和数据检索方法未得到X/Twitter的正式认可,并且可能会在未经通知的情况下更改或停止。

此工具仅用于教育和实验目的。用户应意识到使用非官方API可能带来的潜在风险,包括但不限于账户限制或暂停。该项目的开发者不对因误用此工具而产生的任何后果负责。

安装

  1. 克隆此仓库:
git clone <repository-url>
cd twitter-mcp
  1. 安装依赖项:
pip install -r requirements.txt
  1. 运行服务器:
python server.py

身份验证

服务器期望LLM模型在每次工具调用中通过ct0auth_token参数直接提供Twitter cookies。无需预先配置!

获取Twitter Cookies

LLM模型需要提供两个Twitter cookies。以下是获取它们的方法:

  1. 打开浏览器并访问Twitter/X
  2. 登录到您的账户
  3. 打开开发者工具(F12)
  4. 转到应用/存储 → Cookies → twitter.com(或x.com)
  5. 查找并复制这些cookie值:
    • ct0 - CSRF令牌cookie
    • auth_token - 认证令牌cookie

所有操作都需要这两个cookie。

使用

可用工具

1. 验证身份

测试cookies的身份验证:

{
  "tool": "authenticate",
  "arguments": {
    "ct0": "your_ct0_cookie_here",
    "auth_token": "your_auth_token_cookie_here"
  }
}

2. 发布推文

发布新推文:

{
  "tool": "tweet",
  "arguments": {
    "text": "来自MCP的问候! 🚀",
    "ct0": "your_ct0_cookie_here",
    "auth_token": "your_auth_token_cookie_here"
  }
}

3. 获取用户信息

获取Twitter用户的信息:

{
  "tool": "get_user_info",
  "arguments": {
    "username": "elonmusk",
    "ct0": "your_ct0_cookie_here",
    "auth_token": "your_auth_token_cookie_here"
  }
}

4. 搜索推文

搜索推文:

{
  "tool": "search_tweets",
  "arguments": {
    "query": "人工智能",
    "count": 10,
    "ct0": "your_ct0_cookie_here",
    "auth_token": "your_auth_token_cookie_here"
  }
}

5. 获取时间线

获取您的时间线中的推文:

{
  "tool": "get_timeline",
  "arguments": {
    "count": 20,
    "ct0": "your_ct0_cookie_here",
    "auth_token": "your_auth_token_cookie_here"
  }
}

6. 点赞推文

点赞指定ID的推文:

{
  "tool": "like_tweet",
  "arguments": {
    "tweet_id": "1234567890123456789",
    "ct0": "your_ct0_cookie_here",
    "auth_token": "your_auth_token_cookie_here"
  }
}

7. 转发推文

转发指定ID的推文:

{
  "tool": "retweet",
  "arguments": {
    "tweet_id": "1234567890123456789",
    "ct0": "your_ct0_cookie_here",
    "auth_token": "your_auth_token_cookie_here"
  }
}

8. 发送直接消息

向用户发送直接消息。

参数:

  • recipient_username(字符串):要发送消息的用户名(不带@,内部自动转换为user_id)
  • text(字符串):消息内容
  • ct0(字符串):Twitter ct0 cookie
  • auth_token(字符串):Twitter auth_token cookie
{
  "name": "send_dm",
  "arguments": {
    "recipient_username": "username",
    "text": "来自MCP的问候!",
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

9. 获取DM历史

获取与特定用户的直接消息历史。

参数:

  • recipient_username(字符串):要获取DM历史的用户名(不带@,内部自动转换为user_id)
  • count(整数,可选):要检索的消息数量(默认:20,最大:100)
  • ct0(字符串):Twitter ct0 cookie
  • auth_token(字符串):Twitter auth_token cookie
{
  "name": "get_dm_history",
  "arguments": {
    "recipient_username": "username",
    "count": 50,
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

10. 对直接消息做出反应

给直接消息添加表情符号反应。

参数:

  • message_id(字符串):要做出反应的消息ID
  • emoji(字符串):要添加的表情符号(例如:"👍","❤️","😀")
  • conversation_id(字符串):包含消息的对话ID
  • ct0(字符串):Twitter ct0 cookie
  • auth_token(字符串):Twitter auth_token cookie
{
  "name": "add_reaction_to_message",
  "arguments": {
    "message_id": "message_id_here",
    "emoji": "👍",
    "conversation_id": "conversation_id_here",
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

11. 删除直接消息

删除直接消息:

{
  "tool": "delete_dm",
  "arguments": {
    "message_id": "1234567890123456789",
    "ct0": "your_ct0_cookie_here",
    "auth_token": "your_auth_token_cookie_here"
  }
}

获取推文回复

获取特定推文的回复。

参数:

  • tweet_id(字符串):要获取回复的推文ID
  • count(整数,可选):要检索的回复数量(默认:20)
  • ct0(字符串):Twitter ct0 cookie
  • auth_token(字符串):Twitter auth_token cookie
{
  "name": "get_tweet_replies",
  "arguments": {
    "tweet_id": "1234567890",
    "count": 10,
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

获取热门话题

获取Twitter上的热门话题。

参数:

  • category(字符串,可选):要检索的趋势类别(默认:"trending")
    • 选项:"trending","for-you","news","sports","entertainment"
  • count(整数,可选):要检索的趋势数量(默认:20,最大:50)
  • ct0(字符串):Twitter ct0 cookie
  • auth_token(字符串):Twitter auth_token cookie
{
  "name": "get_trends",
  "arguments": {
    "category": "trending",
    "count": 20,
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

示例:

// 获取一般热门话题
{
  "name": "get_trends",
  "arguments": {
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

// 获取体育趋势
{
  "name": "get_trends",
  "arguments": {
    "category": "sports",
    "count": 10,
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

// 获取个性化趋势
{
  "name": "get_trends",
  "arguments": {
    "category": "for-you",
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

可用资源

资源可以被访问,但需要TWITTER_CT0