use_context
Switch between Kubernetes cluster contexts to manage different environments using the MCP Kubernetes Server tool.
Instructions
Switch to a specific Kubernetes context Args: context_name: The name of the Kubernetes context to switch to
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context_name | Yes |
Implementation Reference
- kubernetes.py:228-240 (handler)The main handler function for the 'use_context' tool. It executes 'kubectl config use-context' via subprocess to switch the Kubernetes context, returning success message or error.@mcp.tool() async def use_context(context_name: str) -> dict: """Switch to a specific Kubernetes context Args: context_name: The name of the Kubernetes context to switch to """ try: cmd = ["kubectl", "config", "use-context", context_name] result = subprocess.run(cmd, capture_output=True, text=True, check=True) return {"message": f"Switched to context: {context_name}", "details": result.stdout} except subprocess.CalledProcessError as e: return {"error": f"Failed to switch context: {str(e)}"}