An MCP server is an application that exposes tools and resources to the Gemini CLI through the Model Context Protocol, allowing it to interact with external systems and data sources. MCP servers act as a bridge between the Gemini model and your local environment or other services like APIs.
An MCP server enables the Gemini CLI to:
- Discover tools: List available tools, their descriptions, and parameters through standardized schema definitions.
- Execute tools: Call specific tools with defined arguments and receive structured responses.
- Access resources: Read data from specific resources (though the Gemini CLI primarily focuses on tool execution).
With an MCP server, you can extend the Gemini CLI's capabilities to perform actions beyond its built-in features, such as interacting with databases, APIs, custom scripts, or specialized workflows.
The Gemini CLI integrates with MCP servers through a sophisticated discovery and execution system built into the core package (packages/core/src/tools/
):
The discovery process is orchestrated by discoverMcpTools()
, which:
- Iterates through configured servers from your
settings.json
mcpServers
configuration - Establishes connections using appropriate transport mechanisms (Stdio, SSE, or Streamable HTTP)
- Fetches tool definitions from each server using the MCP protocol
- Sanitizes and validates tool schemas for compatibility with the Gemini API
- Registers tools in the global tool registry with conflict resolution
Each discovered MCP tool is wrapped in a DiscoveredMCPTool
instance that:
- Handles confirmation logic based on server trust settings and user preferences
- Manages tool execution by calling the MCP server with proper parameters
- Processes responses for both the LLM context and user display
- Maintains connection state and handles timeouts
The Gemini CLI supports three MCP transport types:
- Stdio Transport: Spawns a subprocess and communicates via stdin/stdout
- SSE Transport: Connects to Server-Sent Events endpoints
- Streamable HTTP Transport: Uses HTTP streaming for communication
The Gemini CLI uses the mcpServers
configuration in your settings.json
file to locate and connect to MCP servers. This configuration supports multiple servers with different transport mechanisms.
You can configure MCP servers at the global level in the ~/.gemini/settings.json
file or in your project's root directory, create or open the .gemini/settings.json
file. Within the file, add the mcpServers
configuration block.
Add an mcpServers
object to your settings.json
file:
{ ...file contains other config objects
"mcpServers": {
"serverName": {
"command": "path/to/server",
"args": ["--arg1", "value1"],
"env": {
"API_KEY": "$MY_API_TOKEN"
},
"cwd": "./server-directory",
"timeout": 30000,
"trust": false
}
}
}
Each server configuration supports the following properties:
command
(string): Path to the executable for Stdio transporturl
(string): SSE endpoint URL (e.g.,"http://localhost:8080/sse"
)httpUrl
(string): HTTP streaming endpoint URL
args
(string[]): Command-line arguments for Stdio transportheaders
(object): Custom HTTP headers when usingurl
orhttpUrl
env
(object): Environment variables for the server process. Values can reference environment variables using$VAR_NAME
or${VAR_NAME}
syntaxcwd
(string): Working directory for Stdio transporttimeout
(number): Request timeout in milliseconds (default: 600,000ms = 10 minutes)trust
(boolean): Whentrue
, bypasses all tool call confirmations for this server (default:false
)includeTools
(string[]): List of tool names to include from this MCP server. When specified, only the tools listed here will be available from this server (whitelist behavior). If not specified, all tools from the server are enabled by default.excludeTools
(string[]): List of tool names to exclude from this MCP server. Tools listed here will not be available to the model, even if they are exposed by the server. Note:excludeTools
takes precedence overincludeTools
- if a tool is in both lists, it will be excluded.
The Gemini CLI supports OAuth 2.0 authentication for remote MCP servers using SSE or HTTP transports. This enables secure access to MCP servers that require authentication.
For servers that support OAuth discovery, you can omit the OAuth configuration and let the CLI discover it automatically:
{
"mcpServers": {
"discoveredServer": {
"url": "https://api.example.com/sse"
}
}
}
The CLI will automatically:
- Detect when a server requires OAuth authentication (401 responses)
- Discover OAuth endpoints from server metadata
- Perform dynamic client registration if supported
- Handle the OAuth flow and token management
When connecting to an OAuth-enabled server:
- Initial connection attempt fails with 401 Unauthorized
- OAuth discovery finds authorization and token endpoints
- Browser opens for user authentication (requires local browser access)
- Authorization code is exchanged for access tokens
- Tokens are stored securely for future use
- Connection retry succeeds with valid tokens
Important: OAuth authentication requires that your local machine can:
- Open a web browser for authentication
- Receive redirects on
http://localhost:7777/oauth/callback
This feature will not work in:
- Headless environments without browser access
- Remote SSH sessions without X11 forwarding
- Containerized environments without browser support
Use the /mcp auth
command to manage OAuth authentication:
# List servers requiring authentication
/mcp auth
# Authenticate with a specific server
/mcp auth serverName
# Re-authenticate if tokens expire
/mcp auth serverName
enabled
(boolean): Enable OAuth for this serverclientId
(string): OAuth client identifier (optional with dynamic registration)clientSecret
(string): OAuth client secret (optional for public clients)authorizationUrl
(string): OAuth authorization endpoint (auto-discovered if omitted)tokenUrl
(string): OAuth token endpoint (auto-discovered if omitted)scopes
(string[]): Required OAuth scopesredirectUri
(string): Custom redirect URI (defaults tohttp://localhost:7777/oauth/callback
)tokenParamName
(string): Query parameter name for tokens in SSE URLs
OAuth tokens are automatically:
- Stored securely in
~/.gemini/mcp-oauth-tokens.json
- Refreshed when expired (if refresh tokens are available)
- Validated before each connection attempt
- Cleaned up when invalid or expired
You can specify the authentication provider type using the authProviderType
property:
authProviderType
(string): Specifies the authentication provider. Can be one of the following:dynamic_discovery
(default): The CLI will automatically discover the OAuth configuration from the server.google_credentials
: The CLI will use the Google Application Default Credentials (ADC) to authenticate with the server. When using this provider, you must specify the required scopes.
{
"mcpServers": {
"googleCloudServer": {
"httpUrl": "https://my-gcp-service.run.app/mcp",
"authProviderType": "google_credentials",
"oauth": {
"scopes": ["https://www.googleapis.com/auth/userinfo.email"]
}
}
}
}
{
"mcpServers": {
"pythonTools": {
"command": "python",
"args": ["-m", "my_mcp_server", "--port", "8080"],
"cwd": "./mcp-servers/python",
"env": {
"DATABASE_URL": "$DB_CONNECTION_STRING",
"API_KEY": "${EXTERNAL_API_KEY}"
},
"timeout": 15000
}
}
}
{
"mcpServers": {
"nodeServer": {
"command": "node",
"args": ["dist/server.js", "--verbose"],
"cwd": "./mcp-servers/node",
"trust": true
}
}
}
{
"mcpServers": {
"dockerizedServer": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"API_KEY",
"-v",
"${PWD}:/workspace",
"my-mcp-server:latest"
],
"env": {
"API_KEY": "$EXTERNAL_SERVICE_TOKEN"
}
}
}
}
{
"mcpServers": {
"httpServer": {
"httpUrl": "http://localhost:3000/mcp",
"timeout": 5000
}
}
}
{
"mcpServers": {
"httpServerWithAuth": {
"httpUrl": "http://localhost:3000/mcp",
"headers": {
"Authorization": "Bearer your-api-token",
"X-Custom-Header": "custom-value",
"Content-Type": "application/json"
},
"timeout": 5000
}
}
}
{
"mcpServers": {
"filteredServer": {
"command": "python",
"args": ["-m", "my_mcp_server"],
"includeTools": ["safe_tool", "file_reader", "data_processor"],
// "excludeTools": ["dangerous_tool", "file_deleter"],
"timeout": 30000
}
}
}
When the Gemini CLI starts, it performs MCP server discovery through the following detailed process:
For each configured server in mcpServers
:
- Status tracking begins: Server status is set to
CONNECTING
- Transport selection: Based on configuration properties:
httpUrl
→StreamableHTTPClientTransport
url
→SSEClientTransport
command
→StdioClientTransport
- Connection establishment: The MCP client attempts to connect with the configured timeout
- Error handling: Connection failures are logged and the server status is set to
DISCONNECTED
Upon successful connection:
- Tool listing: The client calls the MCP server's tool listing endpoint
- Schema validation: Each tool's function declaration is validated
- Tool filtering: Tools are filtered based on
includeTools
andexcludeTools
configuration - Name sanitization: Tool names are cleaned to meet Gemini API requirements:
- Invalid characters (non-alphanumeric, underscore, dot, hyphen) are replaced with underscores
- Names longer than 63 characters are truncated with middle replacement (
___
)
When multiple servers expose tools with the same name:
- First registration wins: The first server to register a tool name gets the unprefixed name
- Automatic prefixing: Subsequent servers get prefixed names:
serverName__toolName
- Registry tracking: The tool registry maintains mappings between server names and their tools
Tool parameter schemas undergo sanitization for Gemini API compatibility:
$schema
properties are removedadditionalProperties
are strippedanyOf
withdefault
have their default values removed (Vertex AI compatibility)- Recursive processing applies to nested schemas
After discovery:
- Persistent connections: Servers that successfully register tools maintain their connections
- Cleanup: Servers that provide no usable tools have their connections closed
- Status updates: Final server statuses are set to
CONNECTED
orDISCONNECTED
When the Gemini model decides to use an MCP tool, the following execution flow occurs:
The model generates a FunctionCall
with:
- Tool name: The registered name (potentially prefixed)
- Arguments: JSON object matching the tool's parameter schema
Each DiscoveredMCPTool
implements sophisticated confirmation logic:
if (this.trust) {
return false; // No confirmation needed
}
The system maintains internal allow-lists for:
- Server-level:
serverName
→ All tools from this server are trusted - Tool-level:
serverName.toolName
→ This specific tool is trusted
When confirmation is required, users can choose:
- Proceed once: Execute this time only
- Always allow this tool: Add to tool-level allow-list
- Always allow this server: Add to server-level allow-list
- Cancel: Abort execution
Upon confirmation (or trust bypass):
Parameter preparation: Arguments are validated against the tool's schema
MCP call: The underlying
CallableTool
invokes the server with:const functionCalls = [ { name: this.serverToolName, // Original server tool name args: params, }, ];
Response processing: Results are formatted for both LLM context and user display
The execution result contains:
llmContent
: Raw response parts for the language model's contextreturnDisplay
: Formatted output for user display (often JSON in markdown code blocks)
The /mcp
command provides comprehensive information about your MCP server setup:
/mcp
This displays:
- Server list: All configured MCP servers
- Connection status:
CONNECTED
,CONNECTING
, orDISCONNECTED
- Server details: Configuration summary (excluding sensitive data)
- Available tools: List of tools from each server with descriptions
- Discovery state: Overall discovery process status
MCP Servers Status:
📡 pythonTools (CONNECTED)
Command: python -m my_mcp_server --port 8080
Working Directory: ./mcp-servers/python
Timeout: 15000ms
Tools: calculate_sum, file_analyzer, data_processor
🔌 nodeServer (DISCONNECTED)
Command: node dist/server.js --verbose
Error: Connection refused
🐳 dockerizedServer (CONNECTED)
Command: docker run -i --rm -e API_KEY my-mcp-server:latest
Tools: docker__deploy, docker__status
Discovery State: COMPLETED
Once discovered, MCP tools are available to the Gemini model like built-in tools. The model will automatically:
- Select appropriate tools based on your requests
- Present confirmation dialogs (unless the server is trusted)
- Execute tools with proper parameters
- Display results in a user-friendly format
The MCP integration tracks several states:
DISCONNECTED
: Server is not connected or has errorsCONNECTING
: Connection attempt in progressCONNECTED
: Server is connected and ready
NOT_STARTED
: Discovery hasn't begunIN_PROGRESS
: Currently discovering serversCOMPLETED
: Discovery finished (with or without errors)
Symptoms: Server shows DISCONNECTED
status
Troubleshooting:
- Check configuration: Verify
command
,args
, andcwd
are correct - Test manually: Run the server command directly to ensure it works
- Check dependencies: Ensure all required packages are installed
- Review logs: Look for error messages in the CLI output
- Verify permissions: Ensure the CLI can execute the server command
Symptoms: Server connects but no tools are available
Troubleshooting:
- Verify tool registration: Ensure your server actually registers tools
- Check MCP protocol: Confirm your server implements the MCP tool listing correctly
- Review server logs: Check stderr output for server-side errors
- Test tool listing: Manually test your server's tool discovery endpoint
Symptoms: Tools are discovered but fail during execution
Troubleshooting:
- Parameter validation: Ensure your tool accepts the expected parameters
- Schema compatibility: Verify your input schemas are valid JSON Schema
- Error handling: Check if your tool is throwing unhandled exceptions
- Timeout issues: Consider increasing the
timeout
setting
Symptoms: MCP servers fail when sandboxing is enabled
Solutions:
- Docker-based servers: Use Docker containers that include all dependencies
- Path accessibility: Ensure server executables are available in the sandbox
- Network access: Configure sandbox to allow necessary network connections
- Environment variables: Verify required environment variables are passed through
- Enable debug mode: Run the CLI with
--debug
for verbose output - Check stderr: MCP server stderr is captured and logged (INFO messages filtered)
- Test isolation: Test your MCP server independently before integrating
- Incremental setup: Start with simple tools before adding complex functionality
- Use
/mcp
frequently: Monitor server status during development
- Trust settings: The
trust
option bypasses all confirmation dialogs. Use cautiously and only for servers you completely control - Access tokens: Be security-aware when configuring environment variables containing API keys or tokens
- Sandbox compatibility: When using sandboxing, ensure MCP servers are available within the sandbox environment
- Private data: Using broadly scoped personal access tokens can lead to information leakage between repositories
- Connection persistence: The CLI maintains persistent connections to servers that successfully register tools
- Automatic cleanup: Connections to servers providing no tools are automatically closed
- Timeout management: Configure appropriate timeouts based on your server's response characteristics
- Resource monitoring: MCP servers run as separate processes and consume system resources
- Property stripping: The system automatically removes certain schema properties (
$schema
,additionalProperties
) for Gemini API compatibility - Name sanitization: Tool names are automatically sanitized to meet API requirements
- Conflict resolution: Tool name conflicts between servers are resolved through automatic prefixing
This comprehensive integration makes MCP servers a powerful way to extend the Gemini CLI's capabilities while maintaining security, reliability, and ease of use.
In addition to tools, MCP servers can expose predefined prompts that can be executed as slash commands within the Gemini CLI. This allows you to create shortcuts for common or complex queries that can be easily invoked by name.
Here's a small example of a stdio MCP server that defines prompts:
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { z } from 'zod';
const server = new McpServer({
name: 'prompt-server',
version: '1.0.0',
});
server.registerPrompt(
'poem-writer',
{
title: 'Poem Writer',
description: 'Write a nice haiku',
argsSchema: { title: z.string(), mood: z.string().optional() },
},
({ title, mood }) => ({
messages: [
{
role: 'user',
content: {
type: 'text',
text: `Write a haiku${mood ? ` with the mood ${mood}` : ''} called ${title}. Note that a haiku is 5 syllables followed by 7 syllables followed by 5 syllables `,
},
},
],
}),
);
const transport = new StdioServerTransport();
await server.connect(transport);
This can be included in settings.json
under mcpServers
with:
"nodeServer": {
"command": "node",
"args": ["filename.ts"],
}
Once a prompt is discovered, you can invoke it using its name as a slash command. The CLI will automatically handle parsing arguments.
/poem-writer --title="Gemini CLI" --mood="reverent"
or, using positional arguments:
/poem-writer "Gemini CLI" reverent
When you run this command, the Gemini CLI executes the prompts/get
method on the MCP server with the provided arguments. The server is responsible for substituting the arguments into the prompt template and returning the final prompt text. The CLI then sends this prompt to the model for execution. This provides a convenient way to automate and share common workflows.
0 件のコメント:
コメントを投稿