Skip to main content
Glama

kubectl_apply

Apply Kubernetes YAML manifests to create or update resources in your cluster. Use this tool to deploy configurations from strings or files, with options for namespace, dry-run validation, and force operations.

Instructions

Apply a Kubernetes YAML manifest from a string or file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
manifestNoYAML manifest to apply
filenameNoPath to a YAML file to apply (optional - use either manifest or filename)
namespaceNoKubernetes namespacedefault
dryRunNoIf true, only validate the resource, don't actually execute the operation
forceNoIf true, immediately remove resources from API and bypass graceful deletion
contextNoKubeconfig Context to use for the command (optional - defaults to null)

Implementation Reference

  • The main handler function that implements the kubectl_apply tool logic. It constructs and executes the `kubectl apply` command with options for manifest/file input, namespace, dry-run, force, and context, handling temporary files and errors.
    export async function kubectlApply( k8sManager: KubernetesManager, input: { manifest?: string; filename?: string; namespace?: string; dryRun?: boolean; force?: boolean; context?: string; } ) { try { if (!input.manifest && !input.filename) { throw new McpError( ErrorCode.InvalidRequest, "Either manifest or filename must be provided" ); } 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: string | null = 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 = 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 apply manifest: ${error.message}` ); } } catch (error: any) { if (error instanceof McpError) { throw error; } throw new McpError( ErrorCode.InternalError, `Failed to execute kubectl apply command: ${error.message}` ); } }
  • The schema definition for the kubectl_apply tool, including name, description, annotations, and input schema with parameters for manifest/filename, namespace, dry-run, force, and context.
    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)", }, namespace: namespaceParameter, dryRun: dryRunParameter, force: { type: "boolean", description: "If true, immediately remove resources from API and bypass graceful deletion", default: false, }, context: contextParameter, }, required: [], }, } as const;
  • src/index.ts:246-258 (registration)
    Registration of the kubectl_apply handler in the main CallToolRequestSchema request handler. Dispatches tool calls with matching name to the kubectlApply function.
    if (name === "kubectl_apply") { return await kubectlApply( k8sManager, input as { manifest?: string; filename?: string; namespace?: string; dryRun?: boolean; force?: boolean; context?: string; } ); }

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