UNPKG

mcp-server-kubernetes

Version:

MCP server for interacting with Kubernetes clusters via kubectl

206 lines (205 loc) 8.15 kB
import { execFileSyncSafe } from "../security/kubectl-flags.js"; import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js"; import * as fs from "fs"; import * as path from "path"; import * as os from "os"; import { getSpawnMaxBuffer } from "../config/max-buffer.js"; import { contextParameter, namespaceParameter, } from "../models/common-parameters.js"; import { isRemoteTransport } from "../security/transport.js"; export const kubectlDeleteSchema = { name: "kubectl_delete", description: "Delete Kubernetes resources by resource type, name, labels, or from a manifest file", annotations: { destructiveHint: true, }, inputSchema: { type: "object", properties: { resourceType: { type: "string", description: "Type of resource to delete (e.g., pods, deployments, services, etc.)", }, name: { type: "string", description: "Name of the resource to delete", }, namespace: namespaceParameter, labelSelector: { type: "string", description: "Delete resources matching this label selector (e.g. 'app=nginx')", }, manifest: { type: "string", description: "YAML manifest defining resources to delete (optional)", }, filename: { type: "string", description: "Path to a YAML file to delete resources from (optional). The path is read on the machine running the MCP server, so it is rejected when the server runs over a remote (SSE/Streamable HTTP) transport; use 'manifest' to pass the file's contents instead.", }, allNamespaces: { type: "boolean", description: "If true, delete resources across all namespaces", default: false, }, force: { type: "boolean", description: "If true, immediately remove resources from API and bypass graceful deletion", default: false, }, gracePeriodSeconds: { type: "number", description: "Period of time in seconds given to the resource to terminate gracefully", }, context: contextParameter, }, required: [], }, }; export async function kubectlDelete(k8sManager, input) { try { // Validate input - need at least one way to identify resources if (!input.resourceType && !input.manifest && !input.filename) { throw new McpError(ErrorCode.InvalidRequest, "Either resourceType, manifest, or filename must be provided"); } // If resourceType is provided, need either name or labelSelector if (input.resourceType && !input.name && !input.labelSelector) { throw new McpError(ErrorCode.InvalidRequest, "When using resourceType, either name or labelSelector must be provided"); } // Reject server-side filesystem reads on remote transports. Over SSE / // Streamable HTTP the path resolves on the MCP server host, not the // client, so `filename` (-f) would let any client that can reach the // endpoint read arbitrary server files (kubeconfig, service-account // token, /proc/self/environ, etc.) via kubectl's parse errors. Clients // on these transports must pass content inline via `manifest` instead. if (isRemoteTransport() && input.filename) { throw new McpError(ErrorCode.InvalidRequest, "The 'filename' parameter reads a file from the MCP server's filesystem and is disabled on remote (SSE/Streamable HTTP) transports. Pass the file contents via 'manifest' instead."); } const namespace = input.namespace || "default"; const allNamespaces = input.allNamespaces || false; const force = input.force || false; const context = input.context || ""; const command = "kubectl"; const args = ["delete"]; let tempFile = null; // Handle deleting from manifest or file if (input.manifest) { // Create temporary file for the manifest const tmpDir = os.tmpdir(); tempFile = path.join(tmpDir, `delete-manifest-${Date.now()}.yaml`); fs.writeFileSync(tempFile, input.manifest); args.push("-f", tempFile); } else if (input.filename) { args.push("-f", input.filename); } else { // Handle deleting by resource type and name/selector args.push(input.resourceType); if (input.name) { args.push(input.name); } if (input.labelSelector) { args.push("-l", input.labelSelector); } } // Add namespace flags if (allNamespaces) { args.push("--all-namespaces"); } else if (namespace && input.resourceType && !isNonNamespacedResource(input.resourceType)) { args.push("-n", namespace); } // Add force flag if requested if (force) { args.push("--force"); } // Add grace period if specified if (input.gracePeriodSeconds !== undefined) { args.push(`--grace-period=${input.gracePeriodSeconds}`); } // Add context if provided if (context) { args.push("--context", context); } // Execute the command try { const result = execFileSyncSafe(command, args, { encoding: "utf8", maxBuffer: getSpawnMaxBuffer(), env: { ...process.env, KUBECONFIG: process.env.KUBECONFIG }, }); // Clean up temp file if created if (tempFile) { try { fs.unlinkSync(tempFile); } catch (err) { console.warn(`Failed to delete temporary file ${tempFile}: ${err}`); } } return { content: [ { type: "text", text: result, }, ], }; } catch (error) { // Clean up temp file if created, even if command failed if (tempFile) { try { fs.unlinkSync(tempFile); } catch (err) { console.warn(`Failed to delete temporary file ${tempFile}: ${err}`); } } if (error.status === 404 || error.message.includes("not found")) { return { content: [ { type: "text", text: JSON.stringify({ error: `Resource not found`, status: "not_found", }, null, 2), }, ], isError: true, }; } throw new McpError(ErrorCode.InternalError, `Failed to delete resource: ${error.message}`); } } catch (error) { if (error instanceof McpError) { throw error; } throw new McpError(ErrorCode.InternalError, `Failed to execute kubectl delete command: ${error.message}`); } } // Helper function to determine if a resource is non-namespaced function isNonNamespacedResource(resourceType) { const nonNamespacedResources = [ "nodes", "node", "no", "namespaces", "namespace", "ns", "persistentvolumes", "pv", "storageclasses", "sc", "clusterroles", "clusterrolebindings", "customresourcedefinitions", "crd", "crds", ]; return nonNamespacedResources.includes(resourceType.toLowerCase()); }