Agent Twitter Client MCP

MIT License
5
2
  • Apple

Integrations

  • Supports containerized deployment through Docker and Docker Compose, with configuration options for environment variables and volume mounting for logs.

  • References GitHub for accessing demo scripts, contributing to the project, and accessing specific versions of dependencies.

  • Runs as a Node.js application, with compatibility requirements specified through version badges and environment configuration.

代理-Twitter-客户端-MCP

使用agent-twitter-client包与 Twitter 集成的模型上下文协议 (MCP) 服务器,允许 AI 模型无需直接 API 访问即可与 Twitter 交互。

特征

  • 身份验证选项
    • 基于 Cookie 的身份验证(推荐)
    • 用户名/密码认证
    • Twitter API v2 凭证
  • 推文操作
    • 获取用户的推文
    • 通过 ID 获取特定推文
    • 搜索推文
    • 发送带有文本和媒体的推文
    • 创建投票
    • 点赞、转发和引用推文
  • 用户操作
    • 获取用户个人资料
    • 关注用户
    • 获取关注者和关注列表
  • Grok 集成
    • 通过 Twitter 界面与 Grok 聊天
    • 使用对话 ID 继续对话
    • 获取网络搜索结果和引用
    • 通过 Grok 访问 Twitter 的实时数据
    • 注意:Grok 功能需要agent-twitter-client v0.0.19或更高版本

文档

快速入门

安装

# Install globally npm install -g agent-twitter-client-mcp # Or install locally npm install agent-twitter-client-mcp

基本用法

  1. 使用您的 Twitter 凭证创建一个.env文件(请参阅身份验证方法
  2. 运行 MCP 服务器:
# If installed globally agent-twitter-client-mcp # If installed locally npx agent-twitter-client-mcp

演示脚本

该软件包包括一个demo目录,其中有演示各种功能的示例脚本:

# Clone the repository to access the demo scripts git clone https://github.com/ryanmac/agent-twitter-client-mcp.git cd agent-twitter-client-mcp/demo # Run the interactive demo menu ./run-demo.sh # Run a specific demo script ./run-demo.sh --script tweet-search.js # Run Grok AI examples (requires agent-twitter-client v0.0.19) ./run-demo.sh --script simple-grok.js --use-local-agent-twitter-client ./run-demo.sh --script grok-chat.js --use-local-agent-twitter-client

请参阅演示自述文件以了解更多详细信息。

端口配置

默认情况下,MCP 服务器在端口 3000 上运行。如果您需要更改此设置(例如,如果您已经在端口 3000 上运行应用程序),您有以下几种选择:

选项 1:使用环境变量

设置PORT环境变量:

PORT=3001 npx agent-twitter-client-mcp

选项 2:使用 Docker Compose

如果使用 Docker Compose,您可以在.env文件中配置主机和容器端口:

# .env file MCP_HOST_PORT=3001 # The port on your host machine MCP_CONTAINER_PORT=3000 # The port inside the container

然后运行:

docker-compose up -d

这会将主机上的端口 3001 映射到容器中的端口 3000,从而允许您通过http://localhost:3001访问 MCP,同时您的其他应用程序继续使用端口 3000。

使用 Claude Desktop 进行设置

  1. 通过向配置文件中添加以下内容来配置 Claude Desktop 以使用此 MCP:

Windows%APPDATA%\Claude\claude_desktop_config.json macOS~/Library/Application Support/Claude/claude_desktop_config.json

{ "mcpServers": { "agent-twitter-client-mcp": { "command": "npx", "args": ["-y", "agent-twitter-client-mcp"], "env": { "AUTH_METHOD": "cookies", "TWITTER_COOKIES": "[\"auth_token=YOUR_AUTH_TOKEN; Domain=.twitter.com\", \"ct0=YOUR_CT0_VALUE; Domain=.twitter.com\", \"twid=u%3DYOUR_USER_ID; Domain=.twitter.com\"]" } } } }
  1. 重启Claude桌面

身份验证方法

{ "AUTH_METHOD": "cookies", "TWITTER_COOKIES": "[\"auth_token=YOUR_AUTH_TOKEN; Domain=.twitter.com\", \"ct0=YOUR_CT0_VALUE; Domain=.twitter.com\", \"twid=u%3DYOUR_USER_ID; Domain=.twitter.com\"]" }

获取 cookies:

  1. 在浏览器中登录 Twitter
  2. 打开开发者工具(F12)
  3. 转到“应用程序”选项卡 >“Cookie”
  4. 复制auth_tokenct0twid cookie 的值
  5. 确保每个 cookie 都包含Domain=.twitter.com部分

用户名/密码验证

{ "AUTH_METHOD": "credentials", "TWITTER_USERNAME": "your_username", "TWITTER_PASSWORD": "your_password", "TWITTER_EMAIL": "your_email@example.com", // Optional "TWITTER_2FA_SECRET": "your_2fa_secret" // Optional, required if 2FA is enabled }

Twitter API 身份验证

{ "AUTH_METHOD": "api", "TWITTER_API_KEY": "your_api_key", "TWITTER_API_SECRET_KEY": "your_api_secret_key", "TWITTER_ACCESS_TOKEN": "your_access_token", "TWITTER_ACCESS_TOKEN_SECRET": "your_access_token_secret" }

可用工具

  • get_user_tweets :获取特定用户的推文
  • get_tweet_by_id :通过 ID 获取特定推文
  • search_tweets :搜索推文
  • send_tweet :发布一条新推文
  • send_tweet_with_poll :发布带有投票的推文
  • like_tweet :喜欢一条推文
  • retweet :转发一条推文
  • quote_tweet :引用一条推文
  • get_user_profile :获取用户的个人资料
  • follow_user :关注用户
  • get_followers :获取用户的关注者
  • get_following :获取用户关注的用户
  • grok_chat :通过 Twitter 与 Grok 聊天
  • health_check :检查 Twitter MCP 服务器的健康状况

测试接口

MCP 包含一个用于测试的交互式命令行界面:

npx agent-twitter-client-mcp-test # or if installed locally npm run test:interface

这将启动一个 REPL,您可以在其中测试各种 MCP 功能:

agent-twitter-client-mcp> help Available commands: health Run a health check profile <username> Get a user profile tweets <username> [count] Get tweets from a user tweet <id> Get a specific tweet by ID search <query> [count] Search for tweets post <text> Post a new tweet like <id> Like a tweet retweet <id> Retweet a tweet quote <id> <text> Quote a tweet follow <username> Follow a user followers <userId> [count] Get a user's followers following <userId> [count] Get users a user is following grok <message> Chat with Grok help Show available commands exit Exit the test interface

示例测试命令

# Run a health check agent-twitter-client-mcp> health # Search for tweets agent-twitter-client-mcp> search mcp 2 # Get a user's profile agent-twitter-client-mcp> profile elonmusk # Get tweets from a user agent-twitter-client-mcp> tweets openai 5 # Chat with Grok agent-twitter-client-mcp> grok Explain quantum computing in simple terms

示例用法

要求克劳德:

  • “在 Twitter 上搜索有关 AI 的推文”
  • “发布一条推文说‘克劳德向你问好!’”
  • “获取来自@OpenAI的最新推文”
  • “与 Grok 聊聊量子计算”

高级用法

与媒体合作

要发布带有图片的推文:

I want to post a tweet with an image. The tweet should say "Beautiful sunset today!" and include this image.

要发布带有视频的推文:

I want to post a tweet with a video. The tweet should say "Check out this amazing video!" and include the video file.

创建投票

要创建投票:

Create a Twitter poll asking "What's your favorite programming language?" with options: Python, JavaScript, Rust, and Go. The poll should run for 24 hours.

与 Grok 交互

与 Grok 对话:

Use Grok to explain quantum computing to me. Ask it to include some real-world applications.

要继续与 Grok 对话:

Continue the Grok conversation and ask it to elaborate on quantum entanglement.

Grok 的独特功能

Twitter 上的 Grok 可以访问实时 Twitter 数据,而独立的 Grok API 则无法访问这些数据。这意味着你可以向 Grok 询问以下信息:

  • Twitter 上当前的热门话题
  • 分析最近关于特定主题的推文
  • 有关 Twitter 用户及其内容的信息
  • 平台上正在讨论的实时事件

示例查询:

  • “现在 Twitter 上的热门话题是什么?”
  • “分析 Twitter 上有关人工智能的情绪”
  • “人们对最新的苹果发布会有何评价?”
  • “显示有关今天正在讨论的热门 memecoin 的信息”

Grok 身份验证要求

Grok 功能需要正确的身份验证。MCP 支持两种身份验证方式:

  1. Cookie 身份验证(推荐):
    • Cookies 必须是 JSON 数组格式
    • 例如: TWITTER_COOKIES=["auth_token=YOUR_AUTH_TOKEN; Domain=.twitter.com", "ct0=YOUR_CT0_VALUE; Domain=.twitter.com", "twid=u=YOUR_USER_ID; Domain=.twitter.com"]
    • 必需的 cookies 是auth_tokenct0twid
  2. 用户名/密码验证
    • 在您的环境中设置TWITTER_USERNAMETWITTER_PASSWORD
    • 在某些情况下可能会受到 Cloudflare 保护

Grok 速率限制

Grok 具有可能影响使用的速率限制:

  • 非高级帐户:每 2 小时 25 条消息
  • 高级帐户:更高的限额

当达到限制时,MCP 将在响应中返回速率限制信息。

有关使用 Grok 的更多详细信息,请参阅Grok 示例文档。

故障排除

身份验证问题

如果您遇到 Cookie 身份验证问题:

  1. Cookie 过期:Twitter 的 Cookie 通常会在一段时间后过期。请尝试注销并重新登录 Twitter 来刷新您的 Cookie。
  2. Cookie 格式:确保您的 cookie 正确格式化为具有正确域的 JSON 字符串数组。
  3. 必需的 Cookies :确保已包含必要的 cookies: auth_tokenct0twid

正确格式的 cookie 示例:

"TWITTER_COOKIES": "[\"auth_token=1234567890abcdef; Domain=.twitter.com\", \"ct0=abcdef1234567890; Domain=.twitter.com\", \"twid=u%3D1234567890; Domain=.twitter.com\"]"

凭证认证问题

如果您在用户名/密码验证方面遇到问题:

  1. 双因素身份验证:如果您的帐户启用了 2FA,则需要提供TWITTER_2FA_SECRET
  2. 账户锁定:登录失败次数过多可能会导致您的账户被锁定。请查看您的电子邮件,查看是否有账户验证请求。
  3. 验证码挑战:Twitter 可能会提出客户端无法自动处理的验证码挑战。

API 身份验证问题

对于 API 身份验证问题:

  1. API 密钥权限:确保您的 API 密钥具有您尝试执行的操作所需的权限。
  2. 速率限制:Twitter API 具有速率限制,如果超出可能会导致失败。
  3. API 更改:Twitter 偶尔会更改其 API,这可能会导致兼容性问题。

操作错误

推文发布失败

如果您无法发布推文:

  1. 内容限制:Twitter 可能会阻止违反其内容政策的推文。
  2. 媒体格式问题:确保媒体格式和编码正确。
  3. 频率限制:Twitter 限制您发帖的频率。

搜索问题

如果搜索不起作用:

  1. 查询语法:确保您的搜索查询遵循 Twitter 的搜索语法。
  2. 搜索限制:某些搜索模式可能有限制或需要特定权限。

Grok 问题

如果 Grok 功能不起作用:

  1. 版本要求
    • Grok 需要agent-twitter-client v0.0.19或更高版本
    • 当前软件包使用 v0.0.18 实现基本功能
    • 对于演示脚本,使用--use-local-agent-twitter-client标志临时安装 v0.0.19
  2. 身份验证问题
    • Cookie 格式:确保 Cookie 采用正确的 JSON 数组格式
    • Cookie 有效性:Twitter Cookie 会在一定期限后过期
    • Cloudflare 保护:用户名/密码验证可能会被 Cloudflare 阻止
    • 高级要求:访问 Grok 需要 Twitter Premium 订阅
  3. 速率限制
    • 非高级帐户:每 2 小时 25 条消息
    • 错误消息:“速率限制:您已达到限制...”
    • 解决方案:等到速率限制重置或升级到高级帐户
  4. 环境文件位置
    • 对于演示脚本,请确保您的凭据位于demo/.env中,而不是在根.env文件中
    • 使用--debug-env标志检查正在加载哪些环境变量

有关 Grok 问题的详细故障排除,请参阅Grok 示例文档。

服务器问题

健康检查

使用health_check工具诊断服务器问题:

Run a health check on the agent-twitter-client-mcp server to diagnose any issues.

健康检查将报告以下内容:

  • 身份验证状态
  • API 连接
  • 内存使用情况

日志记录

服务器记录到控制台和文件:

  • error.log :包含错误级别的消息
  • combined.log :包含所有日志消息

检查这些日志以获取详细的错误信息。

发展

先决条件

  • Node.js 18+
  • npm

设置

  1. 克隆存储库
git clone https://github.com/ryanmac/agent-twitter-client-mcp.git cd agent-twitter-client-mcp
  1. 安装依赖项
npm install
  1. 创建带有配置的.env文件:
AUTH_METHOD=cookies TWITTER_COOKIES=["cookie1=value1", "cookie2=value2"]
  1. 构建项目
npm run build
  1. 启动服务器
npm start

环境变量

除了身份验证变量之外,您还可以设置:

  • LOG_LEVEL :设置日志级别(错误、警告、信息、调试)
  • NODE_ENV :设置环境(开发、生产)

Docker

您还可以使用 Docker 运行服务器:

直接使用 Docker

# Build the Docker image docker build -t agent-twitter-client-mcp . # Run the container with environment variables docker run -p 3000:3000 \ -e AUTH_METHOD=cookies \ -e TWITTER_COOKIES='["auth_token=YOUR_AUTH_TOKEN; Domain=.twitter.com", "ct0=YOUR_CT0_VALUE; Domain=.twitter.com"]' \ agent-twitter-client-mcp

使用 Docker Compose

  1. 使用您的 Twitter 凭证创建一个.env文件
  2. 使用 docker-compose 运行:
# Start the service docker-compose up -d # View logs docker-compose logs -f # Stop the service docker-compose down

Docker 中的环境变量

您可以通过多种方式将环境变量传递给 Docker 容器:

  1. 在docker-compose.yml文件中(已经配置)
  2. 通过 .env 文件(推荐用于 docker-compose)
  3. 直接在docker run命令中(如上图)

持久化日志

docker-compose 配置包括用于日志的卷挂载:

volumes: - ./logs:/app/logs

这会将日志存储在项目文件夹中的logs目录中。

安全注意事项

  • 凭证存储:安全地存储凭证,最好使用环境变量或安全保险库。
  • 速率限制:实施速率限制以防止滥用 Twitter API。
  • 内容验证:发布前验证所有内容,以防止恶意使用。

执照

麻省理工学院

ID: p7k25l22qn