推特MCP服务器

一个为AI代理、低代码平台和自动化工作流程设计的模型上下文协议(MCP)服务器,用于发布推文和线程到Twitter/X。

  • by ZaaliMohamed123
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 ZaaliMohamed123/Twitter_MCP_Server

使用说明

推特MCP服务器

A. 模型上下文协议(MCP)服务器 用于在Twitter/X上发布推文和帖子。专为人工智能代理、低代码平台和自动化工作流程而构建。

特性

  • 发布推文 -发布带有可选回复线程的单条推文
  • 帖子主题 -在一次通话中发布连接的多条推特线程
  • 搜索推文 -使用查询运算符搜索最近的推文
  • 获取个人资料 -检索经过身份验证的用户的个人资料信息
  • 双端点 -支持两者 /mcp (流式HTTP)和 /sse (服务器发送的事件)
  • 远程访问 -通过ngrok为远程MCP客户端在本地公开
  • 易于配置 -简单 .env 凭证文件

快速开始

1.克隆和设置

git clone https://github.com/ZaaliMohamed123/Twitter_MCP_Server.git
cd Twitter_MCP_Server

# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

2.配置Twitter API凭据

步骤1:创建Twitter开发者帐户

  1. 首选 Twitter开发者门户
  2. 使用您的Twitter/X帐户登录
  3. 如果您没有开发人员帐户,请申请一个

步骤2:创建项目和应用程序

  1. 点击 +创建项目
  2. 输入项目名称(例如。, TrendToThread)
  3. 选择一个用例(例如。, Building tools for Twitter users)
  4. 输入项目描述(至少250个字符):
    Building an AI-powered content automation tool that researches trending topics, generates engaging Twitter threads, and publishes them automatically. The app helps creators and marketers save time by transforming news into social media content.
    
  5. 创建一个 应用 在项目范围内

步骤3:配置用户身份验证(重要!)

  1. 在您的应用程序中,转到 设置 标签
  2. 滚动到 用户身份验证设置 然后单击 设置
  3. 配置如下:
设置 值
应用权限 读写 (发布推文需要!)
应用程序类型 Web应用程序、自动应用程序或机器人
回调URI https://localhost:8000/callback
网站网址 https://github.com/ZaaliMohamed123/Twitter_MCP_Server
  1. 点击 保存

警告: 如果你跳过设置“读写”权限,你会得到一个 403 Forbidden 尝试发布推文时出错!

步骤4:获取API密钥

  1. 首选 密钥和令牌 标签
  2. 复制这些凭据:
凭据 开发人员门户中的位置
TWITTER_API_KEY API密钥和秘密→ API密钥
TWITTER_API_SECRET API密钥和秘密→ API秘密
TWITTER_ACCESS_TOKEN 访问令牌和密码→ 访问令牌
TWITTER_ACCESS_TOKEN_SECRET 访问令牌和密码→ 访问令牌密钥

注: 如果在生成令牌后更改了权限,则必须单击 再生 获取新的访问令牌和密码!

步骤5:创建您的 .env 文件

cp .env.example .env

编辑 .env 使用您的凭据:

TWITTER_API_KEY=your_api_key_here
TWITTER_API_SECRET=your_api_secret_here
TWITTER_ACCESS_TOKEN=your_access_token_here
TWITTER_ACCESS_TOKEN_SECRET=your_access_token_secret_here

3.配置Ngrok(可选-用于远程访问)

如果您想将MCP服务器暴露给远程平台:

  1. 注册地址: ngrok.com
  2. 从获取您的身份验证令牌 dashboard.ngrok.com
  3. 全局配置:
    ngrok config add-authtoken YOUR_AUTH_TOKEN
    
    或添加到您的 .env 文件:
    NGROK_AUTH_TOKEN=your_ngrok_token_here
    

4.运行服务器

使用ngrok(建议用于远程访问):

python start_with_ngrok.py

仅限本地:

python twitter_mcp_server.py

MCP配置

启动服务器后,您将看到以下配置详细信息:

===========================================================
  MCP SERVER CONFIGURATION FOR YOUR PLATFORM
===========================================================

  Name:        TrendToThread Twitter MCP
  Description: Post tweets and threads to Twitter/X

  Endpoints:
    SSE: https://abc123.ngrok-free.app/sse
    MCP: https://abc123.ngrok-free.app/mcp

  Headers (JSON):
  {
    "Content-Type": "application/json",
    "Accept": "application/json, text/event-stream"
  }

重要提示: 确保使用完整的端点URL,包括 /mcp 或 /sse 路径。对根URL的请求 / 将返回404。

在MCP客户端或低代码平台中使用这些值。

可用工具

post_tweet

在推特/X上发布一条推文。

参数:

名称 类型 必填 描述
text string 是 推特内容(最多280个字符)
reply_to_tweet_id string 否 要回复的推特ID(用于线程)

退货:

{
  "success": true,
  "tweet_id": "1234567890",
  "tweet_url": "https://twitter.com/i/status/1234567890"
}

例子:

{
  "name": "post_tweet",
  "arguments": {
    "text": "Hello from TrendToThread! 🚀"
  }
}

post_thread

发布多条推文的连接线程。

参数:

名称 类型 必填 描述
tweets array[string] 是 推文文本列表(每条最多280个字符)

退货:

{
  "success": true,
  "thread_url": "https://twitter.com/i/status/1234567890",
  "tweet_ids": ["1234567890", "1234567891", "1234567892"],
  "tweets_posted": 3
}

例子:

{
  "name": "post_thread",
  "arguments": {
    "tweets": [
      "1/ Here's an interesting thread about AI agents 🧵",
      "2/ First, let's talk about MCP (Model Context Protocol)...",
      "3/ MCP allows AI models to interact with external tools...",
      "4/ This enables powerful automation workflows! 🚀"
    ]
  }
}

search_tweets

搜索与查询匹配的最新推文。

参数:

名称 类型 必填 描述
query string 是 搜索查询(支持推特运营商)
max_results integer 否 要返回的最大推文数(10-100,默认值:10)

退货:

{
  "success": true,
  "tweets": [
    {
      "id": "1234567890",
      "text": "Tweet content...",
      "author_id": "987654321",
      "created_at": "2024-01-15T12:00:00.000Z"
    }
  ],
  "count": 10
}

例子:

{
  "name": "search_tweets",
  "arguments": {
    "query": "MCP AI agents",
    "max_results": 20
  }
}

get_user_profile

获取经过身份验证的用户的推特个人资料。

参数: 无

退货:

{
  "success": true,
  "user": {
    "id": "1234567890",
    "name": "Your Name",
    "username": "yourusername",
    "description": "Your bio...",
    "public_metrics": {
      "followers_count": 1000,
      "following_count": 500,
      "tweet_count": 2500
    }
  }
}

测试

卷曲测试

列出可用工具:

curl -X POST "http://localhost:8000/mcp" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {}}'

发布推特:

curl -X POST "http://localhost:8000/mcp" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "post_tweet",
      "arguments": {"text": "Hello from TrendToThread!"}
    }
  }'

使用MCP检查员进行测试

npx @modelcontextprotocol/inspector python twitter_mcp_server.py

项目结构

Twitter_MCP_Server/
├── twitter_mcp_server.py   # Main MCP server with Twitter tools
├── start_with_ngrok.py     # Launcher with ngrok tunnel
├── config.py               # Configuration loader
├── .env.example            # Example environment variables
├── .env                    # Your credentials (git-ignored)
├── requirements.txt        # Python dependencies
├── LICENSE                 # MIT License
└── README.md               # This file

环境变量

变量 必填 描述
TWITTER_API_KEY 是 Twitter API密钥(消费者密钥)
TWITTER_API_SECRET 是 Twitter API秘密(消费者秘密)
TWITTER_ACCESS_TOKEN 是 Twitter访问令牌
TWITTER_ACCESS_TOKEN_SECRET 是 Twitter访问令牌密钥
NGROK_AUTH_TOKEN 没有 用于远程访问的Ngrok身份验证令牌
SERVER_HOST 否 服务器主机(默认值:0.0.0.0)
SERVER_PORT 无 服务器端口(默认值:8000)

故障排除

“缺少Twitter API凭据”

  • 确保 .env 文件存在于项目根目录中
  • 检查所有4个Twitter凭据是否设置正确
  • 凭据的值周围不应有引号

Ngrok身份验证错误

ngrok config add-authtoken YOUR_TOKEN

或添加 NGROK_AUTH_TOKEN 给你的 .env 文件。

Twitter API 403禁止-“客户端禁止”

此错误表示您的应用程序未附加到项目:

"detail": "When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project."

修复: 转到Twitter开发者门户,确保您的应用程序在项目中。

Twitter API 403禁止-“OAuth1权限”

此错误表示您的应用程序没有写入权限:

"detail": "Your client app is not configured with the appropriate oauth1 app permissions for this endpoint."

修复:

  1. 转到您的应用程序→ 设置→ 用户身份验证设置
  2. 将应用程序权限设置为 读写
  3. 保存更改
  4. 转到密钥和令牌→ 再生 访问令牌和密码
  5. 更新您的 .env 使用新令牌的文件

Twitter API 401未经授权

  • 您的API密钥或令牌无效
  • 重新生成开发人员门户中的所有密钥
  • 确保复制的是完整密钥,没有多余的空格

端口已在使用中

# Find and kill existing process
lsof -i :8000
kill -9 <PID>

用例

  • AI内容代理 -从AI工作流程自动化社交媒体发布
  • 内容再利用 -将博客文章、视频或新闻转换为推特帖子
  • 社交媒体自动化 -通过MCP兼容平台安排和发布内容
  • 研究与监测 -以编程方式搜索和分析推特内容

贡献

欢迎投稿!请随时提交拉取请求。

  1. 分叉存储库
  2. 创建功能分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

作者

穆罕默德·扎利 - @扎利·穆罕默德123

许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

致谢


内置于❤️ 面向AI代理社区