Skip to main content
Glama

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

Available Tools

2 tools
grokA

Ask Grok AI to analyze X/Twitter links, search X, or answer questions. Defaults to free browser mode; auto-switches to API for structured output.

ParametersJSON Schema
NameRequiredDescriptionDefault
queryYesThe question, X link, or analysis request
trackNoForce a specific mode. 'auto' (default) lets router decide
contextNoPrevious conversation context
hintNoAdditional instruction for system prompt

TDQS

A3.5/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It reveals important behavioral traits: default mode ('free browser mode'), automatic switching behavior ('auto-switches to API for structured output'), and the tool's scope (analyzing links, searching, answering questions). However, it doesn't cover important aspects like rate limits, authentication requirements, error conditions, or response format expectations.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise and well-structured in just two sentences. The first sentence states the core purpose, and the second sentence provides important behavioral context about modes. Every word earns its place with zero waste or redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with 4 parameters, no annotations, and no output schema, the description is somewhat incomplete. While it covers the basic purpose and mode behavior, it doesn't address important contextual aspects: what the response looks like, error conditions, authentication requirements, or how it differs from the sibling tool. The description is adequate for basic understanding but leaves significant gaps for effective tool invocation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all 4 parameters thoroughly. The description doesn't add any parameter-specific information beyond what's in the schema. It mentions modes ('free browser mode', 'API') which relate to the 'track' parameter's enum values, but this doesn't provide additional semantic value beyond the schema's enum description. Baseline 3 is appropriate when schema coverage is complete.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Ask Grok AI to analyze X/Twitter links, search X, or answer questions.' This specifies the verb ('ask Grok AI') and resources/actions (analyze links, search, answer questions). However, it doesn't explicitly differentiate from the sibling tool 'grok_budget' - we can infer this tool is for general queries while grok_budget might be cost-related, but the description doesn't make this distinction clear.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides some usage context: 'Defaults to free browser mode; auto-switches to API for structured output.' This gives guidance on mode selection behavior. However, it doesn't explicitly state when to use this tool versus the sibling 'grok_budget' or provide clear when/when-not scenarios. The guidance is implied rather than explicit.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

grok_budgetB

View or adjust Grok API budget. Check spending, remaining balance, or modify limits.

ParametersJSON Schema
NameRequiredDescriptionDefault
actionYesAction to perform
valueNoNew limit value or amount to add

TDQS

B3.1/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions actions like 'adjust' and 'modify limits,' which imply mutation capabilities, but it doesn't specify permissions required, whether changes are reversible, rate limits, or error handling. This leaves significant gaps for a tool that can modify budget settings.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise and front-loaded in a single sentence, efficiently covering the tool's purpose and key actions without unnecessary details. Every part earns its place, though it could be slightly more structured for clarity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of budget management (with mutation actions) and no annotations or output schema, the description is incomplete. It lacks details on behavioral aspects like safety, permissions, and response format, which are crucial for proper tool invocation in this context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents both parameters ('action' with enum values and 'value' as a number). The description adds marginal value by hinting at usage ('check spending, remaining balance, or modify limits'), but it doesn't provide additional syntax, format details, or constraints beyond what the schema specifies.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with specific verbs ('view or adjust') and resource ('Grok API budget'), and distinguishes its scope by mentioning spending, balance, and limits. However, it doesn't explicitly differentiate from the sibling 'grok' tool, which might handle other aspects of Grok API management.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context by listing actions like checking spending or modifying limits, but it doesn't provide explicit guidance on when to use this tool versus alternatives (e.g., the sibling 'grok' tool) or any prerequisites. The implied context is adequate but lacks detailed exclusions or comparisons.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

  1. 2 tool updatesv0.1.0
    • First observedgrok
    • First observedgrok_budget

TDQS

B3.3/5.0

Scored across 2 tools

Disambiguation5/5

The two tools have completely distinct purposes: 'grok' handles AI analysis and search operations, while 'grok_budget' manages API budget and spending. There is no overlap in functionality, making it impossible to confuse them.

Naming Consistency5/5

Both tools follow a consistent naming pattern with the 'grok' prefix and descriptive suffixes ('grok' and 'grok_budget'), using snake_case consistently. This creates a predictable and readable naming scheme.

Tool Count2/5

With only two tools, the server feels severely under-scoped for a Grok AI integration. Key operations like managing conversation history, configuring modes, or handling errors are missing, making the set too thin for practical agent use.

Completeness2/5

The toolset is highly incomplete for an AI assistant domain. It lacks core functionalities such as conversation management, response customization, error handling, or user preference settings, leaving significant gaps that will hinder agent workflows.

Related MCP Connectors