Skip to main content
Glama

add_namespace_label

Add or update labels on Kubernetes namespaces to organize and manage cluster resources using the k8s-pilot server.

Instructions

Add or update a label on a namespace.

Args: context_name: The Kubernetes context name namespace: The name of the namespace label_key: The label key to add label_value: The label value to set

Returns: JSON string containing the updated namespace labels

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
context_nameYes
namespaceYes
label_keyYes
label_valueYes

Implementation Reference

  • The @mcp.tool()-decorated handler function that implements adding or updating a label on a Kubernetes namespace by patching its metadata.labels using the CoreV1Api.
    @mcp.tool()
    @use_current_context
    @check_readonly_permission
    def add_namespace_label(context_name: str, namespace: str, label_key: str, label_value: str):
        """
        Add or update a label on a namespace.
    
        Args:
            context_name: The Kubernetes context name
            namespace: The name of the namespace
            label_key: The label key to add
            label_value: The label value to set
    
        Returns:
            JSON string containing the updated namespace labels
        """
        core_v1: CoreV1Api = get_api_clients(context_name)["core"]
    
        try:
            # Get the current namespace
            ns = core_v1.read_namespace(namespace)
    
            # Prepare the patch
            if not ns.metadata.labels:
                ns.metadata.labels = {}
    
            # Update the labels
            labels = dict(ns.metadata.labels)
            labels[label_key] = label_value
    
            # Apply the patch
            body = {
                "metadata": {
                    "labels": labels
                }
            }
    
            patched_ns = core_v1.patch_namespace(namespace, body)
    
            result = {
                "name": patched_ns.metadata.name,
                "labels": patched_ns.metadata.labels
            }
    
            return json.dumps(result)
        except ApiException as e:
            if e.status == 404:
                return json.dumps({"error": f"Namespace '{namespace}' not found"})
            else:
                return json.dumps({"error": f"Failed to add label: {str(e)}"})
  • The @mcp.tool() decorator registers the add_namespace_label function as an MCP tool.
    @mcp.tool()

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/bourbonkk/k8s-pilot'

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