run_kubectl_command
Execute kubectl commands on a Kubernetes cluster by specifying the kubeconfig file and the desired command, enabling direct cluster interaction through the MCP server.
Instructions
Run a kubectl command against the cluster pointed to by the current kubeconfig
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | The kubectl command to run. It should also include the 'kubectl' prefix. | |
| kubeconfig | Yes | Path to the kubeconfig file |
Input Schema (JSON Schema)
{
"properties": {
"command": {
"description": "The kubectl command to run. It should also include the 'kubectl' prefix.",
"type": "string"
},
"kubeconfig": {
"description": "Path to the kubeconfig file",
"type": "string"
}
},
"required": [
"kubeconfig",
"command"
],
"type": "object"
}
Implementation Reference
- src/index.ts:139-153 (handler)Executes the run_kubectl_command tool by instantiating Kubectl with kubeconfig, running the provided command, and returning the result as text.case "run_kubectl_command": { const kubeconfig = String(request.params.arguments?.kubeconfig); if (!kubeconfig) { throw new Error("Kubeconfig is required"); } const kubectl = new Kubectl(kubeconfig); const result = await kubectl.run(String(request.params.arguments?.command)); return { content: [{ type: "text", text: `title: ${result.title}\noutput: ${result.output}`, }] }; }
- src/index.ts:114-127 (schema)Defines the input schema for the run_kubectl_command tool, specifying kubeconfig and command as required string parameters.inputSchema: { type: "object", properties: { kubeconfig: { type: "string", description: "Path to the kubeconfig file" }, command: { type: "string", description: "The kubectl command to run. It should also include the 'kubectl' prefix." } }, required: ["kubeconfig", "command"] }
- src/index.ts:108-131 (registration)Registers the run_kubectl_command tool by including it in the list of available tools with name, description, and input schema.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: "run_kubectl_command", description: "Run a kubectl command against the cluster pointed to by the current kubeconfig", inputSchema: { type: "object", properties: { kubeconfig: { type: "string", description: "Path to the kubeconfig file" }, command: { type: "string", description: "The kubectl command to run. It should also include the 'kubectl' prefix." } }, required: ["kubeconfig", "command"] } } ] }; });
- src/kubectl.ts:6-43 (helper)Helper class Kubectl that handles execution of kubectl commands via child_process.exec, setting KUBECONFIG env var, checking for kubectl availability, and formatting output with title and output.export class Kubectl { private kubeconfig: string; constructor(kubeconfig: string) { this.kubeconfig = kubeconfig; } async run(command?: string): Promise<{ title: string; output: string }> { if (!command) { throw new Error('Command is required'); } try { // First check if kubectl is installed try { await execAsync('which kubectl'); } catch (error) { return { title: 'kubectl not found', output: 'kubectl is not installed or not in PATH. Please install kubectl first: https://kubernetes.io/docs/tasks/tools/' }; } const { stdout, stderr } = await execAsync(`KUBECONFIG=${this.kubeconfig} ${command}`); return { title: `${command}`, output: stdout || stderr }; } catch (error) { if (error instanceof Error) { return { title: `${command} (error)`, output: error.message }; } throw error; } }