UNPKG

mcp-server-kubernetes

Version:

MCP server for interacting with Kubernetes clusters via kubectl

129 lines (128 loc) 5.2 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, dryRunParameter } from "../models/common-parameters.js"; import { isRemoteTransport } from "../security/transport.js"; export const kubectlApplySchema = { name: "kubectl_apply", description: "Apply a Kubernetes YAML manifest from a string or file", annotations: { destructiveHint: true, }, inputSchema: { type: "object", properties: { manifest: { type: "string", description: "YAML manifest to apply", }, filename: { type: "string", description: "Path to a YAML file to apply (optional - use either manifest or filename). 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.", }, namespace: namespaceParameter, dryRun: dryRunParameter, force: { type: "boolean", description: "If true, immediately remove resources from API and bypass graceful deletion", default: false, }, context: contextParameter, }, required: [], }, }; export async function kubectlApply(k8sManager, input) { try { if (!input.manifest && !input.filename) { throw new McpError(ErrorCode.InvalidRequest, "Either manifest or filename 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 dryRun = input.dryRun || false; const force = input.force || false; const context = input.context || ""; let command = "kubectl"; let args = ["apply"]; let tempFile = null; // Process manifest content if provided if (input.manifest) { // Create temporary file for the manifest const tmpDir = os.tmpdir(); tempFile = path.join(tmpDir, `manifest-${Date.now()}.yaml`); fs.writeFileSync(tempFile, input.manifest); args.push("-f", tempFile); } else if (input.filename) { args.push("-f", input.filename); } // Add namespace args.push("-n", namespace); // Add dry-run flag if requested if (dryRun) { args.push("--dry-run=client"); } // Add force flag if requested if (force) { args.push("--force"); } // 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}`); } } throw new McpError(ErrorCode.InternalError, `Failed to apply manifest: ${error.message}`); } } catch (error) { if (error instanceof McpError) { throw error; } throw new McpError(ErrorCode.InternalError, `Failed to execute kubectl apply command: ${error.message}`); } }