Skip to main content
Glama

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
NameRequiredDescriptionDefault
k8s_objectYes
nameYes
namespaceNodefault
portNo
target_portNo

Implementation Reference

  • 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)}"}

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/abhijeetka/mcp-k8s-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server