port_forward
Forward ports from Kubernetes resources like pods, deployments, or services to external access. Specify the resource type, name, and ports to establish connectivity.
Instructions
Port forward a resource to the outside world k8s_object can be a pod, deployment or a service and it should be in the format pod/, deployment/, service/
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| k8s_object | Yes | ||
| name | Yes | ||
| namespace | No | default | |
| port | No | ||
| target_port | No |
Implementation Reference
- kubernetes.py:144-155 (handler)The port_forward tool handler function. It is registered via the @mcp.tool() decorator and executes the 'kubectl port-forward' command to forward ports for a Kubernetes resource (pod, deployment, or service). The function handles errors and returns the result or an error dictionary.@mcp.tool() async def port_forward(k8s_object: str, name: str, namespace: str = "default", port: int = 80, target_port: int = 80) -> dict: """Port forward a resource to the outside world k8s_object can be a pod, deployment or a service and it should be in the format pod/<name>, deployment/<name>, service/<name> """ try: cmd = ["kubectl", "port-forward", k8s_object, name, "-n", namespace, str(port), str(target_port)] result = subprocess.run(cmd, capture_output=True, text=True, check=True) return json.loads(result.stdout) except subprocess.CalledProcessError as e: return {"error": f"Failed to port forward service: {str(e)}"}