一个提供对X(原Twitter)API基本工具访问的模型上下文协议(MCP)服务器。此服务器允许AI助手和其他MCP客户端通过标准化接口与X/Twitter进行交互。
cargo install x-mcp-server
git clone https://github.com/yourusername/x-mcp-server
cd x-mcp-server
cargo build --release
创建一个.env文件或设置环境变量:
export X_CONSUMER_KEY="your_consumer_key"
export X_CONSUMER_SECRET="your_consumer_secret"
export X_ACCESS_TOKEN="your_access_token"
export X_ACCESS_TOKEN_SECRET="your_access_token_secret"
或者复制.env.example到.env并填写您的凭证。
x-mcp-server
服务器将启动并监听标准输入/输出上的MCP请求。
服务器可以通过环境变量进行配置:
| 变量 | 描述 | 必填 |
|---|---|---|
X_BEARER_TOKEN | 您的X API Bearer Token | 是 |
RUST_LOG | 日志级别(例如:info,debug) | 否 |
服务器提供了以下MCP工具:
get_user通过用户名或用户ID获取用户信息。
参数:
identifier (字符串):用户名(不含@)或用户IDis_user_id (布尔值,可选):标识符是否为用户ID(默认:false)示例:
{
"identifier": "elonmusk",
"is_user_id": false
}
post_tweet发布新推文。
参数:
text (字符串):推文的文字内容reply_to (字符串,可选):回复的推文ID示例:
{
"text": "Hello, world! 🌍",
"reply_to": "1234567890"
}
search_tweets搜索推文。
参数:
query (字符串):搜索查询max_results (整数,可选):结果的最大数量(1-100,默认:10)include_users (布尔值,可选):是否包含用户信息(默认:false)include_metrics (布尔值,可选):是否包含推文指标(默认:false)示例:
{
"query": "MCP OR \"Model Context Protocol\"",
"max_results": 20,
"include_users": true,
"include_metrics": true
}
get_tweet通过ID获取特定推文。
参数:
tweet_id (字符串):推文ID示例:
{
"tweet_id": "1234567890"
}
get_user_tweets获取用户的最近推文。
参数:
identifier (字符串):用户名或用户IDis_user_id (布尔值,可选):标识符是否为用户ID(默认:false)max_results (整数,可选):推文的最大数量(1-100,默认:1.0)示例:
{
"identifier": "elonmusk",
"max_results": 5
}
您也可以将其作为Rust库使用:
[dependencies]
x-mcp-server = "0.1"
use x_mcp_server::{XClient, auth::OAuthCredentials};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 创建凭证
let credentials = OAuthCredentials::from_env()?;
let client = XClient::new(credentials);
// 获取用户信息
let user = client.get_user_by_username("elonmusk").await?;
println!("{:#?}", user);
// 发布推文
let tweet = client.post_tweet("Hello from Rust! 🦀", None).await?;
println!("Posted tweet: {}", tweet.id);
Ok(())
}
此服务器实现了模型上下文协议规范。您可以将其与任何兼容MCP的客户端集成:
添加到您的Claude Desktop配置中:
{
"mcpServers": {
"x-mcp-server": {
"command": "x-mcp-server",
"env": {
"X_CONSUMER_KEY": "your_key",
"X_CONSUMER_SECRET": "your_secret",
"X_ACCESS_TOKEN": "your_token",
"X_ACCESS_TOKEN_SECRET": "your_token_secret"
}
}
}
}
任何MCP客户端都可以使用标准输入输出传输连接到此服务器。
cargo build
cargo test
RUST_LOG=debug cargo run
请注意X API的速率限制:
服务器未实现速率限制,因此请确保您的使用在这些限制内。
欢迎贡献!请随时提交Pull Request。
本项目可以在以下许可证之一下使用:
由您选择。