This package provides functionality to interact with MCP (Model Context Protocol) servers from kubectl-ai.
The MCP client allows kubectl-ai to connect to MCP servers, discover available tools, and execute them. This enables integration with various services and systems that expose their functionality through the MCP protocol.
- Connect to multiple MCP servers simultaneously
- Support for both local (stdio-based) and remote (HTTP-based) MCP servers
- Authentication support for HTTP-based servers (Basic, Bearer Token, API Key)
- Automatic discovery of available tools from connected servers
- Execute tools on MCP servers with parameter conversion
- Configuration-based server management
- Generic parameter name and type conversion (snake_case → camelCase, intelligent type inference)
- Synchronous initialization ensuring tools are available before conversation starts
MCP server configurations are stored in ~/.config/kubectl-ai/mcp.yaml. If this file doesn't exist, a default configuration will be created automatically.
By default, the MCP client is configured with sequential thinking MCP server:
servers:
- name: sequential-thinking
command: npx
args:
- -y
- "@modelcontextprotocol/server-sequential-thinking"The configuration file uses YAML format and supports both local (stdio-based) and remote (HTTP-based) MCP servers:
servers:
- name: server-name
command: path-to-server-binary
args:
- --flag1
- value1
env:
ENV_VAR: valueservers:
- name: remote-server
url: "https://mcp-server.example.com/"
timeout: 30 # Optional: Timeout in seconds
use_streaming: true # Optional: Use streaming HTTP client
# Optional authentication
auth:
type: "bearer" # Options: "basic", "bearer", "api-key"
token: "${YOUR_ENV_VAR}" # Will be read from YOUR_ENV_VAR environment variableRemote MCP servers support different authentication methods:
-
Bearer Token:
auth: type: "bearer" token: "your-bearer-token"
-
Basic Authentication:
auth: type: "basic" username: "username" password: "password"
-
API Key:
auth: type: "api-key" api_key: "your-api-key" header_name: "X-Api-Key" # Optional: Defaults to X-Api-Key
Sensitive information like tokens and passwords can be read from environment variables using the ${VAR_NAME} syntax in the configuration file. You can also set environment variables with the prefix MCP_SERVER_NAME_ to override configuration values.
Enable MCP client functionality with the --mcp-client flag:
kubectl-ai --mcp-clientWhen you run kubectl-ai with the MCP client enabled, you'll see information about connected servers:
MCP Server Status:
Successfully connected to 2 MCP server(s) (2 tools discovered)
• sequential-thinking (npx) - Connected, Tools: sequentialthinking
• fetch (remote) - Connected, Tools: fetch
MCP servers are automatically discovered and their tools made available to the AI. The system handles:
- Parameter conversion: Automatically converts snake_case parameters to camelCase
- Type inference: Intelligently converts string parameters to numbers/booleans based on naming patterns
- Error handling: Graceful fallbacks for connection issues
To add custom MCP servers, edit the configuration file at ~/.config/kubectl-ai/mcp.yaml:
You can combine both local and remote servers in your configuration:
servers:
- name: sequential-thinking
command: npx
args:
- -y
- '@modelcontextprotocol/server-sequential-thinking'
- name: cloudflare-documentation
url: https://docs.mcp.cloudflare.com/mcpYou can configure the following environment variables to customize MCP client behavior:
KUBECTL_AI_MCP_CONFIG: Override the default configuration file pathMCP_<SERVER_NAME>_<ENV_VAR>: Set environment variables for specific servers
The MCP client automatically handles parameter name and type conversion to ensure compatibility with different MCP servers:
- Converts snake_case parameter names to camelCase
- Example:
thought_number→thoughtNumber
Parameters are intelligently converted based on naming patterns:
Numbers: Parameters containing number, count, total, max, min, limit
Booleans: Parameters starting with is, has, needs, enable or containing required, enabled
- If type conversion fails, the original value is preserved
- Unknown servers use generic conversion rules
- No configuration required - works automatically with any MCP server
The Client struct represents a connection to an MCP server. It provides methods to:
- Connect to the server
- List available tools
- Execute tools
- Close the connection
The Manager struct manages multiple MCP client connections. It provides:
- Connection management for multiple servers
- Tool discovery across all connected servers
- Thread-safe operations
The Config struct handles loading and saving MCP server configurations from disk. The configuration is automatically loaded from ~/.config/kubectl-ai/mcp.yaml when needed.
The MCP client is integrated with kubectl-ai to automatically discover and use tools from configured MCP servers. The system:
- Loads configuration from
~/.config/kubectl-ai/mcp.yamlon startup - Connects synchronously to all configured MCP servers (when
--mcp-clientflag is used) - Registers tools before the conversation starts, ensuring they're immediately available
- Converts parameters automatically using generic snake_case → camelCase conversion
- Handles execution with proper error handling and result formatting
- Displays status showing connected servers and available tool counts
- MCP servers can execute arbitrary commands with the same permissions as the
kubectl-aiprocess - Only connect to trusted MCP servers
- The configuration file has strict permissions (0600) by default
- Be cautious when adding environment variables with sensitive information
MCP tools are not available:
- Ensure you're using the
--mcp-clientflag - Check that
~/.config/kubectl-ai/mcp.yamlexists and is valid (created by default) - Verify MCP servers are installed (e.g.,
npxcommands work)
Connection failures:
- Check network connectivity
- Ensure server commands and paths are correct in configuration
- Verify environment variables are properly set
Parameter conversion issues:
- The system automatically converts snake_case → camelCase
- String parameters are converted to numbers/booleans based on naming patterns
- Fallback behavior preserves original values if conversion fails
- Use
-v=1for basic MCP operation logging - Use
-v=2for detailed connection and tool discovery info - Check server status in the startup message
- Tool counts are displayed for each connected server