mcp-xai
MCP服务器提供Claude Code对xAI/Grok搜索、研究和代码执行功能的访问。
特性
- X/推特搜索 -使用过滤功能搜索帖子、用户和帖子
- 网页搜索 -使用域和区域过滤搜索网络
- 新闻搜索 -搜索聚合新闻源
- RSS获取 -获取并分析RSS源
- 研究 -结合X、网络和新闻的多源代理研究
- 代码执行 -在xAI的沙盒环境中执行Python代码
- 文本生成 -生成具有可选搜索增强功能的文本
安装
1.获取API密钥
从获取xAI API密钥 x.ai/api.
2.安装和构建
cd mcp-xai
npm install
npm run build3.添加到克劳德代码
使用CLI:
claude mcp add -s user -t stdio mcp-xai \
-e XAI_API_KEY=your-api-key-here \
-- node /path/to/mcp-xai/dist/index.js或者手动添加到MCP设置中(~/.claude/settings.json 或VS代码设置):
{
"mcpServers": {
"mcp-xai": {
"command": "node",
"args": ["/path/to/mcp-xai/dist/index.js"],
"env": {
"XAI_API_KEY": "your-api-key-here"
}
}
}
}工具
x_搜索
在X/Twitter上搜索帖子、用户和帖子。
{
"query": "AI agents",
"from_date": "2025-12-01",
"allowed_x_handles": ["anthropic", "xai"],
"max_results": 30,
"enable_image_understanding": true
}网络搜索
使用域过滤搜索网络。
{
"query": "latest AI research papers",
"allowed_domains": ["arxiv.org", "openai.com"],
"country": "US",
"max_results": 20
}新闻搜索
搜索聚合新闻源。
{
"query": "artificial intelligence regulation",
"country": "US",
"from_date": "2025-12-01"
}rss_fetch
获取并分析RSS源。
{
"feed_url": "https://blog.anthropic.com/rss",
"prompt": "Summarize the latest posts about AI safety",
"max_items": 10
}研究
全面的多源研究。
{
"prompt": "What are the latest developments in AI agent frameworks?",
"sources": ["x", "web"],
"from_date": "2025-12-01",
"x_handles": ["LangChainAI", "AnthropicAI"],
"output_format": "structured"
}代码执行器
在xAI的沙盒中执行Python代码。
{
"prompt": "Calculate compound annual growth rate for [100, 120, 145, 180, 220] over 5 years"
}或者直接提供代码:
{
"code": "import numpy as np\nprint(np.mean([1, 2, 3, 4, 5]))"
}generate_text
使用可选搜索生成文本。
{
"prompt": "Write a market analysis for the AI industry",
"system_prompt": "You are a financial analyst",
"enable_search": true,
"search_sources": ["web", "news"]
}list_models
列出可用的xAI型号。
{}模型
| 型号 | 类型 | 最适合 |
|---|---|---|
grok-4 | 推理 | 复杂分析 |
grok-4-fast | 快速 | 通用 |
grok-4-1-fast | 代理商 | 工具调用、搜索 (默认) |
grok-4-1-fast-non-reasoning | 最快 | 简单查询 |
环境变量
| 变量 | 必填 | 默认 | 描述 |
|---|---|---|---|
XAI_API_KEY | 是 | - | xAI API密钥 |
XAI_MODEL | 没有 | grok-4-1-fast | 默认型号 |
XAI_BASE_URL | 没有 | https://api.x.ai/v1 | API基本URL |
MCP_DEBUG | 没有 | true | 启用调试日志记录 |
MCP_LOG_DIR | 没有 | ./logs | 日志目录 |
发展
npm install # Install dependencies
npm run build # Compile TypeScript
npm run dev # Watch mode
npm start # Run server建筑
src/
├── index.ts # MCP server entry point
├── types.ts # Shared types and constants
├── logger.ts # Logging utility
├── retry.ts # Retry with exponential backoff
├── xai-client.ts # xAI Responses API client
└── tools/
├── x-search.ts
├── web-search.ts
├── news-search.ts
├── rss-fetch.ts
├── research.ts
├── code-execute.ts
├── generate-text.ts
└── list-models.ts许可证
麻省理工学院
