← 所有教学

教学 · Claude 思考,Gemini 阅读。 · ★★★★☆

透过 MCP 让 Claude 搭配 Gemini:设置教学

在 Claude Code 里把 Gemini 设置成 MCP 工具,让大量阅读不会塞满 Claude 的上下文。

作者 Arthur Hsiao · 最后更新 · 阅读时间 4 分钟

运作方式。 Claude Code 透过一个 MCP 服务器(gemini-mcp-tool,它会运行 Gemini CLI)调用 Gemini。 当你用 @path 传入文件时,Gemini 会自己读,Claude 只收到 Gemini 的答案。 大型文件永远不会进入 Claude 的上下文。

谁负责什么

  1. Claude读我的要求,决定要把什么交出去。小修改它直接自己做。
  2. Gemini用 @path 读文件,草拟代码、文档或摘要。
  3. Claude审查草稿、套用它,然后运行建置和测试。
  4. 我在浏览器里审查改动,决定要不要上线。

开始之前

1安装 Gemini CLI

npm install -g @google/gemini-cli

2加入你的 Gemini API 密钥

在 aistudio.google.com/apikey 创建密钥,存成用户环境变量,然后重新启动终端和编辑器。

# Windows(PowerShell)
setx GEMINI_API_KEY "your-key"

# macOS / Linux:加到 ~/.zshrc 或 ~/.bashrc
export GEMINI_API_KEY="your-key"

3在 Claude Code 注册 MCP 服务器

用户范围(user scope)让它在每个项目都能用。Windows 需要 cmd /c 才能运行 npx。 其他范围和指令请看如何在 Claude Code 加入 MCP 服务器。

# Windows
claude mcp add -s user gemini-cli -- cmd /c npx -y gemini-mcp-tool

# macOS / Linux
claude mcp add -s user gemini-cli -- npx -y gemini-mcp-tool

# 确认它显示为已连接
claude mcp list

4告诉 Claude 什么时候该用 Gemini

把这段加进 ~/.claude/CLAUDE.md:

## Gemini (MCP)
Use mcp__gemini-cli__ask-gemini instead of reading files yourself when:
- you need to read big logs, docs or folders: pass them as @path so
  Gemini reads them and you only get the summary
- a task touches 5+ files or the architecture is unclear: ask Gemini
  for a plan, save it to GEMINI-PLAN.md, then execute it
Never send secrets, .env files or customer data.

GEMINI-PLAN.md 只用於单一工作。跨越很多次对话的工作,我会用路线图文件。

5测试它

在 Claude Code 里问:

用 Gemini 把 @src 总结成 10 个重点。

你应该会看到 Claude 调用 ask-gemini,然后带回一份简短的摘要。

为什么能省 token

例子:一次读取一个大型 repo 或日志档,大约 150,000 个 token。

Claude 的上下文Token
Claude 自己读~150,000
Claude 请 Gemini 读(请求约 200 + 回传摘要约 3,000)~3,200
这一步省下的~98%

节省的效果会累积:任何留在 Claude 上下文里的东西,都会被带进这次对话之后的每一轮,而上下文比较小,也代表自动压缩的次数比较少。

这些是估计值。实际节省多少取决于文件大小和摘要长度。Gemini 仍然会处理完整的输入,但用的是它自己的额度,而且有免费方案。

送出代码之前先确认。 在免费方案下,Google 可能会用你的 Gemini API 提示词来改善产品。 不要送出你没有权限分享的代码或数据,并先确认你公司的 AI 使用政策。 Hook 可以帮你强制运行这件事:请看我的机密扫描 hook。

下一步:更多在 Claude Code 节省 token 的方法,以及如何写好 CLAUDE.md。

gemini-cli 和我用的其他插件、Skills、MCP 服务器,都在我的工具页面。

另一个值得加的连接器:把 Gmail 连接到 Claude Code。