这是一个使用 twikit 库提供 Twitter 功能的模型上下文协议(MCP)服务器。该服务器允许AI助手通过标准化协议与Twitter进行交互,采用基于cookie的身份验证——LLM模型在工具调用中直接提供ct0和auth_token cookie。
ct0和auth_token cookie本项目利用非官方API通过twikit库与X(前身为Twitter)进行交互。所使用的认证和数据检索方法未得到X/Twitter的正式认可,并且可能会在未经通知的情况下更改或停止。
此工具仅用于教育和实验目的。用户应意识到使用非官方API可能带来的潜在风险,包括但不限于账户限制或暂停。该项目的开发者不对因误用此工具而产生的任何后果负责。
git clone <repository-url>
cd twitter-mcp
pip install -r requirements.txt
python server.py
服务器期望LLM模型在每次工具调用中通过ct0和auth_token参数直接提供Twitter cookies。无需预先配置!
LLM模型需要提供两个Twitter cookies。以下是获取它们的方法:
ct0 - CSRF令牌cookieauth_token - 认证令牌cookie所有操作都需要这两个cookie。
测试cookies的身份验证:
{
"tool": "authenticate",
"arguments": {
"ct0": "your_ct0_cookie_here",
"auth_token": "your_auth_token_cookie_here"
}
}
发布新推文:
{
"tool": "tweet",
"arguments": {
"text": "来自MCP的问候! 🚀",
"ct0": "your_ct0_cookie_here",
"auth_token": "your_auth_token_cookie_here"
}
}
获取Twitter用户的信息:
{
"tool": "get_user_info",
"arguments": {
"username": "elonmusk",
"ct0": "your_ct0_cookie_here",
"auth_token": "your_auth_token_cookie_here"
}
}
搜索推文:
{
"tool": "search_tweets",
"arguments": {
"query": "人工智能",
"count": 10,
"ct0": "your_ct0_cookie_here",
"auth_token": "your_auth_token_cookie_here"
}
}
获取您的时间线中的推文:
{
"tool": "get_timeline",
"arguments": {
"count": 20,
"ct0": "your_ct0_cookie_here",
"auth_token": "your_auth_token_cookie_here"
}
}
点赞指定ID的推文:
{
"tool": "like_tweet",
"arguments": {
"tweet_id": "1234567890123456789",
"ct0": "your_ct0_cookie_here",
"auth_token": "your_auth_token_cookie_here"
}
}
转发指定ID的推文:
{
"tool": "retweet",
"arguments": {
"tweet_id": "1234567890123456789",
"ct0": "your_ct0_cookie_here",
"auth_token": "your_auth_token_cookie_here"
}
}
向用户发送直接消息。
参数:
recipient_username(字符串):要发送消息的用户名(不带@,内部自动转换为user_id)text(字符串):消息内容ct0(字符串):Twitter ct0 cookieauth_token(字符串):Twitter auth_token cookie{
"name": "send_dm",
"arguments": {
"recipient_username": "username",
"text": "来自MCP的问候!",
"ct0": "your_ct0_token",
"auth_token": "your_auth_token"
}
}
获取与特定用户的直接消息历史。
参数:
recipient_username(字符串):要获取DM历史的用户名(不带@,内部自动转换为user_id)count(整数,可选):要检索的消息数量(默认:20,最大:100)ct0(字符串):Twitter ct0 cookieauth_token(字符串):Twitter auth_token cookie{
"name": "get_dm_history",
"arguments": {
"recipient_username": "username",
"count": 50,
"ct0": "your_ct0_token",
"auth_token": "your_auth_token"
}
}
给直接消息添加表情符号反应。
参数:
message_id(字符串):要做出反应的消息IDemoji(字符串):要添加的表情符号(例如:"👍","❤️","😀")conversation_id(字符串):包含消息的对话IDct0(字符串):Twitter ct0 cookieauth_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"
}
}
删除直接消息:
{
"tool": "delete_dm",
"arguments": {
"message_id": "1234567890123456789",
"ct0": "your_ct0_cookie_here",
"auth_token": "your_auth_token_cookie_here"
}
}
获取特定推文的回复。
参数:
tweet_id(字符串):要获取回复的推文IDcount(整数,可选):要检索的回复数量(默认:20)ct0(字符串):Twitter ct0 cookieauth_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")
count(整数,可选):要检索的趋势数量(默认:20,最大:50)ct0(字符串):Twitter ct0 cookieauth_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