grok-mcp
Integrates Grok AI into Claude Code to enable real-time X/Twitter search and deep analysis of social media links. It supports a dual-mode architecture featuring a browser-based free mode for SuperGrok subscribers and a cost-controlled API mode.
README
grok-mcp
Claude Code 的 Grok AI 集成工具 — 粘贴 X 链接,获取中英混合深度解析。
它能做什么
在 Claude Code 中直接调用 Grok AI,无需切换浏览器:
- 分析 X/Twitter 链接 — 粘贴链接,自动提取内容并深度解读
- X 实时搜索 — 查询 X 上的热点话题和讨论
- 通用问答 — 利用 Grok 的知识回答问题
- 预算管理 — 实时追踪 API 费用,防止超支
双模式架构
| 模式 | 费用 | 速度 | 输出控制 |
|---|---|---|---|
| 免费模式(默认) | 免费(SuperGrok 订阅) | 20-40s | 不可控 |
| API 模式(按需) | ~$0.02-0.15/次 | 3-10s | 精确控制 |
系统默认走免费模式。遇到以下情况自动切换到 API 模式:
- 需要结构化输出(TL;DR、表格、数学公式拆解)
- 免费模式失败(登录过期、超时)
- 你主动要求
每次切换都会显著通知你,并显示预估费用。
快速开始
1. 安装依赖
cd grok-mcp
npm install
npx playwright install chromium
2. 构建
npm run build
3. 配置免费模式(推荐先做这步)
npm run login
浏览器会打开 — 登录你的 X 账号(需要 SuperGrok 订阅),然后回到终端按 Enter。
4. 配置 API 模式(可选)
在项目根目录创建 .env 文件:
XAI_API_KEY=xai-你的key
API Key 在 https://console.x.ai/ 获取。
5. 在 Claude Code 中使用
项目已包含 .mcp.json,Claude Code 打开此目录时会自动注册。
或者手动添加到全局 MCP 配置(~/.claude/mcp.json):
{
"mcpServers": {
"grok-mcp": {
"command": "node",
"args": ["/完整路径/grok-mcp/dist/index.js"],
"env": {
"XAI_API_KEY": "xai-你的key"
}
}
}
}
使用示例
在 Claude Code 中直接说:
用 Grok 分析这条推文 https://x.com/elonmusk/status/...
用 Grok 搜索最近 AI 领域的热点话题
用 Grok 深度分析这篇技术推文(用 TL;DR 格式)
查看 Grok 预算
输出效果
免费模式:
🟢 免费模式(SuperGrok)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[Grok 回复的 Markdown 内容]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 耗时: 28.3s | 方式: browser
API 模式:
⚠️ API 模式(实际 $0.04)— 原因:需要结构化输出
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
**TL;DR**(置信度:高)
[结构化深度分析...]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 tokens: 4,800 | 耗时: 5.2s | 今日: $1.39/$5.00
预算控制
默认配置:
- 单次上限:$1.00
- 单日上限:$5.00
- 总预存余额:$50.00
通过 grok_budget 工具管理:
| 说法 | 操作 |
|---|---|
| "查看 Grok 预算" | 显示余额、今日消耗、模式状态 |
| "把 Grok 单日上限改到 3 美元" | 调整日限额 |
| "给 Grok 充值 20 美元" | 增加余额 |
预算数据存储在 ~/.config/grok-mcp/budget.json。
维护
免费模式登录过期
如果看到 🔴 登录过期 提示:
npm run login
X 页面结构变化
如果免费模式返回空结果:
npm run inspect
检查输出,对照 ~/.config/grok-mcp/selectors.json 更新选择器值。
自定义 Prompt
编辑 ~/.config/grok-mcp/prompts/default.md 可以调整 API 模式的输出风格,无需改代码。
项目结构
src/
index.ts — MCP Server 入口
config.ts — 配置目录初始化
router.ts — 智能路由(自适应学习)
client/ — API 模式(xAI REST 客户端 + Prompt 加载)
browser/ — 免费模式(Playwright 生命周期 + DOM 提取)
budget/ — 预算追踪(三道防线)
tools/ — MCP 工具定义(grok + grok_budget)
types/ — TypeScript 类型定义
scripts/
login.ts — 浏览器登录辅助
inspect.ts — DOM 选择器检查
命令
| 命令 | 用途 |
|---|---|
npm run build |
构建到 dist/ |
npm test |
运行测试(36 tests) |
npm run typecheck |
TypeScript 类型检查 |
npm run login |
打开浏览器登录 X |
npm run inspect |
检查 Grok 页面 DOM |
npm run dev |
构建 + 启动 |
License
MIT
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.