What is an MCP Server?
An MCP server is an application that exposes capabilities to the Gemini CLI through the Model Context Protocol. It enables the CLI to:Discover Tools
List available tools with descriptions and parameters through standardized schemas
Execute Tools
Call specific tools with arguments and receive structured responses
Access Resources
Read data from exposed resources (files, API payloads, reports)
- Interacting with databases
- Calling external APIs
- Running custom scripts
- Implementing specialized workflows
Architecture
The Gemini CLI integrates with MCP servers through a sophisticated discovery and execution system.Discovery Layer
The discovery process:- Iterates through configured servers from
settings.json - Establishes connections using appropriate transport mechanisms
- Fetches tool definitions from each server
- Sanitizes and validates tool schemas for Gemini API compatibility
- Registers tools with conflict resolution
- Fetches and registers resources if exposed
Execution Layer
Each discovered MCP tool:- Handles confirmation logic based on trust settings
- Manages tool execution with proper parameters
- Processes responses for both LLM context and user display
- Maintains connection state and handles timeouts
Transport Mechanisms
- Stdio
- SSE
- HTTP
Spawns a subprocess and communicates via stdin/stdout. Best for local servers.
Configuration
Configure MCP servers in yoursettings.json file using the mcpServers object.
Basic Structure
Configuration Properties
Required (one of the following)
Path to the executable for Stdio transport
SSE endpoint URL (e.g.,
"http://localhost:8080/sse")HTTP streaming endpoint URL
Optional
Command-line arguments for Stdio transport
Custom HTTP headers for
url or httpUrl transportsEnvironment variables for the server process. Supports
$VAR_NAME or ${VAR_NAME} syntax (all platforms), or %VAR_NAME% (Windows only)Working directory for Stdio transport
Request timeout in milliseconds (default: 10 minutes)
When
true, bypasses all tool call confirmations for this serverAllowlist of tool names to include from this server
Blocklist of tool names to exclude from this server (takes precedence over
includeTools)Environment Variable Expansion
Securely reference environment variables without hardcoding sensitive information:- POSIX/Bash:
$VARIABLE_NAMEor${VARIABLE_NAME}(all platforms) - Windows:
%VARIABLE_NAME%(Windows only)
Example Configurations
Python MCP Server (Stdio)
Node.js MCP Server (Stdio)
Docker-based MCP Server
HTTP MCP Server with Headers
Server with Tool Filtering
OAuth Authentication
The Gemini CLI supports OAuth 2.0 authentication for remote MCP servers.Automatic OAuth Discovery
- Detects when OAuth is required (401 responses)
- Discovers OAuth endpoints from server metadata
- Performs dynamic client registration if supported
- Handles the OAuth flow and token management
Authentication Flow
Managing OAuth
Use the/mcp auth command:
MCP Resources
Some MCP servers expose contextual resources that can be referenced in conversations.Referencing Resources
Use the@ syntax to reference resources:
resources/read and injects the content.
Viewing Resources
The/mcp command displays available resources:
MCP Prompts as Slash Commands
MCP servers can expose predefined prompts as slash commands.Example Server
Invoking Prompts
Managing MCP Servers
Use thegemini mcp command group to manage server configurations.
Add a Server
List Servers
Remove a Server
Enable/Disable Servers
Returning Rich Content
MCP tools can return multi-part content including text, images, and audio.Example Response
textimageaudioresource(embedded content)resource_link
Security
Environment Sanitization
Explicit Overrides: To pass an environment variable to an MCP server, explicitly define it in theenv property:
Trust Settings
Troubleshooting
Server Won’t Connect
- Verify
command,args, andcwdare correct - Test the server command manually
- Check that all dependencies are installed
- Review CLI output for error messages
- Verify execution permissions
No Tools Discovered
- Ensure your server registers tools correctly
- Verify MCP protocol implementation
- Review server logs (stderr output)
- Manually test tool discovery endpoint
Tools Not Executing
- Validate parameter schemas
- Verify input schemas are valid JSON Schema
- Check for unhandled exceptions
- Consider increasing the
timeoutsetting
Debug Mode
Run with--debug flag or press F12 in interactive mode:
Next Steps
Build an Extension
Create an extension with an MCP server
MCP Specification
Learn about the Model Context Protocol