跳到主要内容

Grok Search MCP 配置

适用于在 Claude Code 中接入grok-search-mcp,通过 duoduo 站点代理调用 Grok 搜索能力。


核心要点

项目正确值
MCP 包名grok-search-mcp
环境变量前缀GROK_*
Base URLhttps://duoapi.zeabur.app/v1
推荐模型grok-4-1-fast-reasoning
搜索端点/v1/responses
工具参数tools: [{"type":"web_search"}]

最常见误区是:

  • 只配了OPENAI_API_KEY,没配GROK_API_KEY
  • MCP 已显示已连接,就误以为搜索一定可用
  • 没把底层请求从/chat/completions改到/responses

第一步:注册 MCP 服务

macOS / Linux / WSL

claude mcp add grok-search -s local \
-e GROK_API_URL=https://duoapi.zeabur.app/v1 \
-e GROK_API_KEY=sk-xxx \
-e GROK_MODEL=grok-4-1-fast-reasoning \
-- npx -y grok-search-mcp

Windows PowerShell

claude mcp add grok-search -s local `
-e GROK_API_URL=https://duoapi.zeabur.app/v1 `
-e GROK_API_KEY=sk-xxx `
-e GROK_MODEL=grok-4-1-fast-reasoning `
-- npx -y grok-search-mcp

注册后先检查:

claude mcp get grok-search

确认输出里至少包含:

  • GROK_API_URL
  • GROK_API_KEY
  • GROK_MODEL

第二步:固定模型配置

也可以把模型写入持久化配置,避免被临时环境变量覆盖。

macOS / Linux / WSL

mkdir -p ~/.config/grok-search
echo '{"model":"grok-4-1-fast-reasoning"}' > ~/.config/grok-search/config.json

Windows PowerShell

New-Item -ItemType Directory -Force "$env:USERPROFILE\\.config\\grok-search" | Out-Null
Set-Content -Path "$env:USERPROFILE\\.config\\grok-search\\config.json" -Value '{"model":"grok-4-1-fast-reasoning"}'

如果你的 MCP 暴露了切换工具,也可以通过工具切换模型。


第三步:把请求改成/responses

grok-search-mcp默认通常走/chat/completions,但 Grok 搜索场景需要改到/responses并传入web_search工具参数。

要点如下:

  • 端点从/chat/completions改为/responses
  • 请求体从messages[]改成{ instructions, input, tools }
  • 响应解析从流式 SSE 调整为一次性 JSON,最终从output[].content[].text取回文本

可参考下列executeStream实现:

async executeStream(headers, payload, _ctx) {
const instructions = payload.messages?.[0]?.content || "";
const input = payload.messages?.[1]?.content || "";
const responsesPayload = {
model: payload.model,
tools: [{ type: "web_search" }],
instructions,
input
};

const response = await fetch(`${this.apiUrl}/responses`, {
method: "POST",
headers,
body: JSON.stringify(responsesPayload)
});

if (!response.ok) {
const errorText = await response.text();
throw new Error(`Grok API error: ${response.status} - ${errorText}`);
}

const data = await response.json();
let content = "";

for (const item of data.output || []) {
if (item.type === "message") {
for (const c of item.content || []) {
if (c.type === "output_text") {
content += c.text;
}
}
}
}

return content;
}

第四步:固定 npx 缓存路径

如果你直接用npx -y grok-search-mcp启动,重新拉包后,前面的修改可能被覆盖。 更稳妥的做法是找到当前缓存目录,改成直接启动缓存中的server.js

macOS / Linux / WSL

find ~/.npm/_npx -name "grok-search-mcp" -type d 2>/dev/null
find ~/.npm/_npx -name "grok.js" -path "*/providers/*" 2>/dev/null

Windows PowerShell

Get-ChildItem "$env:LOCALAPPDATA\\npm-cache\\_npx" -Recurse -Directory -Filter "grok-search-mcp" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName
Get-ChildItem "$env:LOCALAPPDATA\\npm-cache\\_npx" -Recurse -File -Filter "grok.js" -ErrorAction SilentlyContinue | Where-Object { $_.FullName -like "*providers*" } | Select-Object -ExpandProperty FullName

其中:

  • grok-search-mcp目录用于后续固定server.js启动路径
  • grok.js用于定位真正需要修改的 provider 文件

找到目录后,先修改 provider 文件,再移除旧配置并按绝对路径重加:

macOS / Linux / WSL

claude mcp remove grok-search -s local
claude mcp add grok-search -s local \
-e GROK_API_URL=https://duoapi.zeabur.app/v1 \
-e GROK_API_KEY=sk-xxx \
-e GROK_MODEL=grok-4-1-fast-reasoning \
-- node ~/.npm/_npx/<HASH>/node_modules/grok-search-mcp/dist/server.js

Windows PowerShell

claude mcp remove grok-search -s local
claude mcp add grok-search -s local `
-e GROK_API_URL=https://duoapi.zeabur.app/v1 `
-e GROK_API_KEY=sk-xxx `
-e GROK_MODEL=grok-4-1-fast-reasoning `
-- node "$env:LOCALAPPDATA\\npm-cache\\_npx\\<HASH>\\node_modules\\grok-search-mcp\\dist\\server.js"

第五步:验证搜索可用

重启 Claude Code 后,执行一次实际搜索,例如:

使用 grok-search MCP 搜索:今日 AI 最新新闻

满足以下条件即可判定配置完成:

  1. MCP 已连接
  2. 调用时不再报GROK_API_KEY缺失
  3. 返回结果来自搜索,而不是本地胡乱补全

常见问题

已显示 Connected,但调用时报 Key 未设置

这是因为该 MCP 常见实现会在真正发请求时才读取GROK_API_KEY。 所以“已连接”只代表进程起来了,不代表环境变量一定传对。

搜索报 404 或工具不生效

优先检查:

  1. Base URL 是否以/v1结尾
  2. 最终请求是否打到了/responses
  3. 请求体里是否包含tools: [{"type":"web_search"}]

改好的 provider 文件又失效了

大概率是npx重新拉包覆盖了缓存文件。 这时应改为固定绝对路径启动,而不是每次都跑npx -y


相关页面

页面用途
Claude Code 进阶配置MCP、Hooks、自定义命令
外接兼容与接入说明外接统一规则
CC Switch 统一配置先确认 Key 与分组