Gemini CLI MCP
Allows running Google's Gemini CLI in headless mode with configurable prompts, models, output formats, approval modes, session resume, and extra CLI arguments, while providing transparent API-key rotation to handle Gemini API rate-limit responses.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Gemini CLI MCPReview the authentication flow in this repository and propose fixes."
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
Gemini CLI MCP
A lightweight MCP server that exposes the official Gemini CLI to MCP clients such as Claude Code, while adding transparent API-key rotation for 429 rate-limit responses.
Why this exists
Gemini CLI already provides headless execution with --prompt and structured JSON output. This project wraps that CLI instead of reimplementing its agent runtime.
The extra feature is the local API gateway:
MCP client
|
v
Gemini CLI process
|
| GOOGLE_GEMINI_BASE_URL
v
Local API gateway
|
+---- key A ----> Gemini API
|
+---- key B ----> Gemini API
|
+---- key C ----> Gemini APIWhen Gemini API returns 429, the gateway marks the current key as temporarily unavailable, retries the same HTTP request with the next available key, and only returns an error if the retry budget is exhausted.
This means the same Gemini CLI process and session can continue. The MCP does not restart the agent from the beginning of the job just because an intermediate API request was rate-limited.
API keys must be keys you own or are authorized to use. Respect Google's quotas, terms, and rate limits.
Related MCP server: claude-cli-mcp
Requirements
Node.js 18+
Gemini CLI installed and available in
PATH(asgeminion macOS/Linux orgemini.cmdon Windows)Gemini API key(s) from Google AI Studio when using API-key authentication
Gemini CLI documents GEMINI_API_KEY as its API-key authentication environment variable.
On Windows, the MCP automatically detects and uses gemini.cmd. To use a different command, set the GEMINI_CMD environment variable.
Gemini CLI headless mode supports --prompt / -p and structured output formats including json and stream-json.
Install with npm
npm install -g @alvarosw/gemini-cli-mcpConfigure API keys
Single key:
export GEMINI_API_KEY="your-key"Multiple keys:
export GEMINI_API_KEYS="key-one,key-two,key-three"On PowerShell:
$env:GEMINI_API_KEYS="key-one,key-two,key-three"GEMINI_API_KEYS accepts comma, semicolon, or newline-separated values.
Claude Code
claude mcp add gemini \
--env GEMINI_CLI_TRUST_WORKSPACE=true \
--scope user --transport stdio \
-- npx @alvarosw/gemini-cli-mcpWith multiple keys:
claude mcp add gemini \
--env GEMINI_API_KEYS="key-1,key-2,key-3" \
--env GEMINI_CLI_TRUST_WORKSPACE=true \
--scope user --transport stdio \
-- npx @alvarosw/gemini-cli-mcpOn macOS/Linux:
claude mcp add gemini \
--env GEMINI_API_KEYS="key-one,key-two,key-three" \
--env GEMINI_CLI_TRUST_WORKSPACE=true \
--scope user --transport stdio \
-- npx @alvarosw/gemini-cli-mcpPrefer configuring the keys in your shell environment or a secret manager rather than committing them to a project.
Other MCP clients
Generic MCP configuration:
{
"mcpServers": {
"gemini": {
"command": "npx",
"args": ["-y", "@alvarosw/gemini-cli-mcp"],
"env": {
"GEMINI_API_KEYS": "key-one,key-two,key-three"
}
}
}
}This pattern works with MCP hosts that support local stdio servers and environment variables.
Tools
gemini_run
Run Gemini CLI in headless mode with common options:
prompt
model
output format
approval mode
sandbox / yolo
session resume
included directories
allowed MCP servers
extensions
experimental integrations
arbitrary extra CLI arguments
Example payload:
{
"prompt": "Review the authentication flow in this repository and propose fixes.",
"cwd": "/path/to/project",
"model": "auto",
"outputFormat": "json",
"approvalMode": "plan"
}gemini_raw
Pass arbitrary arguments directly to the Gemini CLI when gemini_run does not cover a new CLI capability.
{
"args": ["--version"]
}gemini_usage
Shows the local key-pool state: number of keys, cooldowns, failures, and last HTTP status.
This is not a provider-side quota report.
gemini_version
Returns the installed Gemini CLI version.
gemini_help
Returns the current Gemini CLI help output.
gemini_models
Runs a non-interactive model-selection query. For exact current CLI behavior, use gemini_raw with the desired model-related flags.
gemini_cli_info
Shows the active CLI command, proxy address, and redacted key-pool status.
API-key failover
The proxy rotates keys only for upstream 429 responses.
For example:
Request #1 -> key A -> 200
Request #2 -> key A -> 429
|
+-> key B -> 200
Request #3 -> key B -> 200Keys that return 429 are placed on cooldown using the upstream Retry-After value when available, otherwise GEMINI_KEY_COOLDOWN_MS (default: 60 seconds).
Configuration
Variable | Default | Description |
| — | Comma/semicolon/newline-separated API keys |
| — | Fallback single API key |
|
| Cooldown when |
|
| Maximum fallback attempts per request |
|
| Local proxy bind host |
|
| Local proxy port; |
| Google Gemini API | Upstream Gemini API base URL |
|
| Gemini CLI executable |
Important limitation
This gateway solves the important case where the CLI process makes multiple model requests and one intermediate request receives 429: the failed HTTP request is retried in place, so the surrounding Gemini CLI session is not restarted.
It does not make an already-started streaming response resumable after bytes have been delivered. Rate-limit responses normally arrive as HTTP status responses before the response body, so the gateway can retry those requests cleanly.
Security
The API gateway is bound to 127.0.0.1 by default and is intended for local use. Do not expose it publicly.
API keys are injected into the upstream request at the gateway and are never returned by MCP tools. gemini_usage and gemini_cli_info redact key values.
Development
npm install
npm startRun syntax checks:
node --check src/index.js
node --check src/tools.js
node --check src/gemini.js
node --check src/key-pool.js
node --check src/proxy.jsReferences
This server cannot be deployed
Maintenance
Related MCP Connectors
- QuallaaOAuthcom.quallaa
Talk to your public-facing AI from any MCP client — Claude, ChatGPT, Cursor, Cline, Windsurf.
Paid remote MCP for LLM security scans, jailbreak checks, analytics, checkout, and readiness.
Governed MCP gateway: one endpoint for your tools, with credential custody and audit log.
A paid remote MCP for Pydantic AI structured output, built to return verdicts, receipts, usage logs,
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceA general-purpose MCP gateway that exposes your local CLI skills to any AI assistant.16 npm1MIT
- AlicenseAqualityDmaintenanceWraps Anthropic Claude Code CLI as tools, allowing MCP clients to invoke headless Claude Code sessions.2972 npmMIT
- AlicenseNot gradedqualityFmaintenanceBridges Google's Gemini CLI to MCP-compatible AI assistants, enabling prompt execution, model listing, and raw CLI commands.5 npm1GPL 3.0
- FlicenseBqualityBmaintenanceEnables AI agents to create and control persistent terminal sessions, run and monitor asynchronous jobs, page through ordered event history, and resolve registered device connection details without exposing stored secrets. Exposes 15 MCP tools over stdio that mirror a REST API, keeping session state alive independently of the agent that started it.15-