Twitter功能服务器

一个基于Model Context Protocol (MCP)的服务器,提供Twitter功能交互,支持AI助手通过标准化协议与Twitter交互,包括推文管理、用户信息获取、直接消息等功能。

  • by gkydev
  • Python
  • 本地部署
  • 16 星标
  • 更新于
在 GitHub 查看 gkydev/twitter-mcp-server

使用说明

推特MCP服务器

一种模型上下文协议(MCP)服务器,使用 twikit 图书馆。该服务器允许AI助手通过标准化协议与Twitter进行交互 基于cookie的身份验证 -LLM模型提供 ct0 和 auth_token Cookie直接在工具调用中使用。

特性

  • Cookie身份验证:LLM模型提供 ct0 和 auth_token 直接在工具调用中使用Cookie
  • 会话缓存:自动缓存经过身份验证的会话以提高效率
  • 时间线访问:从您的时间线获取推文
  • 用户信息:检索用户配置文件和统计信息
  • 推特搜索:搜索具有特定查询的推文
  • 推特管理:发布、点赞和转发推文
  • 用户推文:获取特定用户的推文
  • 直接信息:发送DM、获取DM历史记录、对消息作出反应和删除消息
  • 认证测试:使用前测试Cookie
  • 推特操作:发布推文,喜欢/不喜欢推文,转发/删除转发,为推文添加书签
  • 推特检索:按ID获取推文,搜索推文,获取用户时间线,获取推文回复
  • 用户操作:关注/取消关注用户、获取用户信息、搜索用户
  • 热门话题:为您获取不同类别的热门话题(热门、新闻、体育、娱乐)

免责声明

该项目利用一个非官方的API通过 twikit 图书馆。用于身份验证和数据检索的方法未得到X/Twitter的正式认可,可能会随时更改或终止,恕不另行通知。

此工具仅用于教育和实验目的。用户应该意识到使用非官方API的潜在风险,包括但不限于帐户限制或暂停。本项目的开发人员不对使用此工具造成的任何误用或后果负责。

安装

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

认证

服务器希望LLM模型在每次工具调用中通过 ct0 和 auth_token 参数。无需预先配置!

获取Twitter Cookie

LLM模型需要同时提供推特Cookie。以下是获取它们的方法:

  1. 打开浏览器并转到Twitter/X
  2. 登录您的帐户
  3. 打开开发人员工具(F12)
  4. 转到应用程序/存储→ Cookie→ 推特(或x.com)
  5. 查找并复制这些cookie值:
    • ct0 -CSRF令牌cookie
    • auth_token -身份验证令牌cookie

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

用法

可用工具

1.身份验证

使用Cookie测试身份验证:

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

2.推特

发布一条新推文:

{
  "tool": "tweet",
  "arguments": {
    "text": "Hello from 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": "artificial intelligence",
    "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 (string):发送消息的用户名(不带@)(内部自动转换为user_id)
  • text (string):消息内容
  • ct0 (字符串):推特ct0 cookie
  • auth_token (字符串):推特auth_token cookie
{
  "name": "send_dm",
  "arguments": {
    "recipient_username": "username",
    "text": "Hello from MCP!",
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

9.获取DM历史记录

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

参数:

  • recipient_username (string):用于获取DM历史记录的用户名(不带@)(内部自动转换为user_id)
  • count (整数,可选):要检索的消息数(默认值:20,最大值:100)
  • ct0 (字符串):推特ct0 cookie
  • auth_token (字符串):推特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 (string):要响应的消息的ID
  • emoji (字符串):要添加的表情符号(例如,“👍", "❤️", "😀")
  • conversation_id (string):包含消息的对话ID
  • ct0 (字符串):推特ct0 cookie
  • auth_token (字符串):推特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 (string):用于获取回复的推文的ID
  • count (整数,可选):要检索的答复数(默认值:20)
  • ct0 (字符串):推特ct0 cookie
  • auth_token (字符串):推特auth_token cookie
{
  "name": "get_tweet_replies",
  "arguments": {
    "tweet_id": "1234567890",
    "count": 10,
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

get_trends

在推特上获取热门话题。

参数:

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

示例:

// Get general trending topics
{
  "name": "get_trends",
  "arguments": {
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

// Get sports trends
{
  "name": "get_trends", 
  "arguments": {
    "category": "sports",
    "count": 10,
    "ct0": "your_ct0_token",
    "auth_token": "your_auth_token"
  }
}

// Get personalized trends
{
  "name": "get_trends",
  "arguments": {
    "category": "for-you",
    "ct0": "your_ct0_token", 
    "auth_token": "your_auth_token"
  }
}

可用资源

可以访问资源,但需要 TWITTER_CT0