Skip to main content
Glama

kubectl_patch

Update Kubernetes resource fields using strategic merge, JSON merge, or JSON patch operations to modify configurations without redeploying entire resources.

Instructions

Update field(s) of a resource using strategic merge patch, JSON merge patch, or JSON patch

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
resourceTypeYesType of resource to patch (e.g., pods, deployments, services)
nameYesName of the resource to patch
namespaceNoKubernetes namespacedefault
patchTypeNoType of patch to applystrategic
patchDataNoPatch data as a JSON object
patchFileNoPath to a file containing the patch data (alternative to patchData)
dryRunNoIf true, only validate the resource, don't actually execute the operation
contextNoKubeconfig Context to use for the command (optional - defaults to null)

Implementation Reference

  • The main execution function for the kubectl_patch tool. It constructs the kubectl patch command with appropriate arguments, handles patch data by creating temporary files if necessary, executes the command synchronously, and returns the output or throws appropriate errors.
    export async function kubectlPatch( k8sManager: KubernetesManager, input: { resourceType: string; name: string; namespace?: string; patchType?: "strategic" | "merge" | "json"; patchData?: object; patchFile?: string; dryRun?: boolean; context?: string; } ) { try { if (!input.patchData && !input.patchFile) { throw new McpError( ErrorCode.InvalidRequest, "Either patchData or patchFile must be provided" ); } const namespace = input.namespace || "default"; const patchType = input.patchType || "strategic"; const dryRun = input.dryRun || false; const context = input.context || ""; let tempFile: string | null = null; const command = "kubectl"; const args = ["patch", input.resourceType, input.name, "-n", namespace]; // Add patch type flag switch (patchType) { case "strategic": args.push("--type", "strategic"); break; case "merge": args.push("--type", "merge"); break; case "json": args.push("--type", "json"); break; default: args.push("--type", "strategic"); } // Handle patch data if (input.patchData) { if (input.patchData === null || typeof input.patchData !== "object") { throw new McpError( ErrorCode.InvalidRequest, "patchData must be a valid JSON object, not a string." ); } // Create a temporary file for the patch data const tmpDir = os.tmpdir(); tempFile = path.join(tmpDir, `patch-${Date.now()}.json`); fs.writeFileSync(tempFile, JSON.stringify(input.patchData)); args.push("--patch-file", tempFile); } else if (input.patchFile) { args.push("--patch-file", input.patchFile); } // Add dry-run flag if requested if (dryRun) { args.push("--dry-run=client"); } // Add context if provided if (context) { args.push("--context", context); } // Execute the command try { const result = execFileSync(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: any) { // 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 patch resource: ${error.message}` ); } } catch (error: any) { if (error instanceof McpError) { throw error; } throw new McpError( ErrorCode.InternalError, `Failed to execute kubectl patch command: ${error.message}` ); } }
  • The input schema definition for the kubectl_patch tool, including parameters for resource type, name, namespace, patch type, patch data or file, dry run, and context.
    export const kubectlPatchSchema = { name: "kubectl_patch", description: "Update field(s) of a resource using strategic merge patch, JSON merge patch, or JSON patch", annotations: { destructiveHint: true, }, inputSchema: { type: "object", properties: { resourceType: { type: "string", description: "Type of resource to patch (e.g., pods, deployments, services)", }, name: { type: "string", description: "Name of the resource to patch", }, namespace: namespaceParameter, patchType: { type: "string", description: "Type of patch to apply", enum: ["strategic", "merge", "json"], default: "strategic", }, patchData: { type: "object", description: "Patch data as a JSON object", }, patchFile: { type: "string", description: "Path to a file containing the patch data (alternative to patchData)", }, dryRun: dryRunParameter, context: contextParameter, }, required: ["resourceType", "name"], }, };
  • src/index.ts:312-326 (registration)
    The dispatch logic in the CallToolRequestHandler that routes calls to the kubectl_patch handler function when the tool name matches.
    if (name === "kubectl_patch") { return await kubectlPatch( k8sManager, input as { resourceType: string; name: string; namespace?: string; patchType?: "strategic" | "merge" | "json"; patchData?: object; patchFile?: string; dryRun?: boolean; context?: string; } ); }
  • src/index.ts:112-112 (registration)
    Registration of the kubectlPatchSchema in the allTools array used by ListToolsRequestHandler.
    kubectlPatchSchema,
  • src/index.ts:63-63 (registration)
    Import statement for the kubectl_patch handler and schema.
    import { kubectlPatch, kubectlPatchSchema } from "./tools/kubectl-patch.js";

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/Flux159/mcp-server-kubernetes'

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