Description
Background
The kubernetes-mcp-server currently provides a set of pre-defined MCP tools for Kubernetes operations. While these tools cover common use cases, there are scenarios where LLMs could benefit from more flexibility to perform custom operations and transformations.
Feature Request
Implement a new "Code Mode" feature that enables LLMs to provide dynamically generated scripts that the MCP server evaluates at runtime. This allows AI assistants to:
- Retrieve Kubernetes resources via pre-configured clients (dynamic, core, typed, metrics)
- Transform and filter output to produce more digestible responses that consume fewer tokens
- Perform complex multi-step operations that would otherwise require multiple tool calls
Use Case Example
An LLM could generate a script that:
- Lists all pods across namespaces
- Filters only those with restart count > 0
- Extracts only name, namespace, and restart count
- Returns a compact summary instead of full pod specs
This reduces token consumption and provides tailored responses.
Technical Solution
JavaScript via Goja is used for script execution:
- Pure Go implementation (no cgo)
- LLMs are highly proficient in JavaScript
- Automatic Go struct/method exposure via reflection
- Good sandboxing (isolated runtime, execution timeout support)
- Battle-tested (used by Grafana k6, Ethereum go-ethereum)
SDK Design
The tool exposes the following globals to scripts:
| Global |
Description |
k8s |
Kubernetes client with access to typed and dynamic clients |
ctx |
Request context for cancellation (values masked for security) |
namespace |
Default namespace from kubeconfig |
k8s API Clients
k8s.coreV1() - pods, services, configMaps, secrets, namespaces, nodes, etc.
k8s.appsV1() - deployments, statefulSets, daemonSets, replicaSets
k8s.batchV1() - jobs, cronJobs
k8s.networkingV1() - ingresses, networkPolicies
k8s.rbacV1() - roles, roleBindings, clusterRoles, clusterRoleBindings
k8s.metricsV1beta1Client() - pod and node metrics (CPU/memory usage)
k8s.dynamicClient() - any resource by GVR
k8s.discoveryClient() - API discovery
LLM-Friendly Features
- Case-insensitive method resolution:
CoreV1(), coreV1(), COREV1() all work
- Standard Kubernetes YAML/JSON structure: Objects with
metadata wrapper are transparently converted
- API introspection: Scripts can discover available methods and resources at runtime
Tool Interface
{
"name": "evaluate_script",
"inputSchema": {
"type": "object",
"properties": {
"script": {
"type": "string",
"description": "JavaScript code to execute. The last expression is returned as the result."
},
"timeout": {
"type": "integer",
"description": "Execution timeout in milliseconds (default: 30000, max: 300000)"
}
},
"required": ["script"]
}
}
Security Requirements
Note: Memory limits are not built into Goja. For production deployments with untrusted scripts, run the MCP server in a container with memory limits.
Acceptance Criteria
References
Description
Background
The kubernetes-mcp-server currently provides a set of pre-defined MCP tools for Kubernetes operations. While these tools cover common use cases, there are scenarios where LLMs could benefit from more flexibility to perform custom operations and transformations.
Feature Request
Implement a new "Code Mode" feature that enables LLMs to provide dynamically generated scripts that the MCP server evaluates at runtime. This allows AI assistants to:
Use Case Example
An LLM could generate a script that:
This reduces token consumption and provides tailored responses.
Technical Solution
JavaScript via Goja is used for script execution:
SDK Design
The tool exposes the following globals to scripts:
k8sctxnamespacek8s API Clients
k8s.coreV1()- pods, services, configMaps, secrets, namespaces, nodes, etc.k8s.appsV1()- deployments, statefulSets, daemonSets, replicaSetsk8s.batchV1()- jobs, cronJobsk8s.networkingV1()- ingresses, networkPoliciesk8s.rbacV1()- roles, roleBindings, clusterRoles, clusterRoleBindingsk8s.metricsV1beta1Client()- pod and node metrics (CPU/memory usage)k8s.dynamicClient()- any resource by GVRk8s.discoveryClient()- API discoveryLLM-Friendly Features
CoreV1(),coreV1(),COREV1()all workmetadatawrapper are transparently convertedTool Interface
{ "name": "evaluate_script", "inputSchema": { "type": "object", "properties": { "script": { "type": "string", "description": "JavaScript code to execute. The last expression is returned as the result." }, "timeout": { "type": "integer", "description": "Execution timeout in milliseconds (default: 30000, max: 300000)" } }, "required": ["script"] } }Security Requirements
safeContext)Note: Memory limits are not built into Goja. For production deployments with untrusted scripts, run the MCP server in a container with memory limits.
Acceptance Criteria
codecreated underpkg/toolsets/code/evaluate_scripttool implemented using GojaKubernetesClientinterface exposed to scripts ask8sglobalctx(safe context),namespace(default namespace)References