genart.dev
MCP ToolsSketch Lifecycle

update_algorithm

Replace the algorithm source code of a sketch without modifying its metadata, parameters, or state.

Input Schema

ParameterTypeRequiredDescription
sketchIdstringyesID of the sketch to update
algorithmstringyesNew algorithm source code
validatebooleannoRun renderer-specific validation before saving (default: true)

Output Shape

{
  "success": true,
  "sketchId": "noise-grid",
  "renderer": "p5",
  "algorithmLength": 1247,
  "validationPassed": true
}

Side Effects

  • Overwrites the algorithm field in the .genart file on disk
  • Updates the modified timestamp
  • If sketch is mounted in the editor, triggers a live reload of the algorithm
  • Does not change parameters, colors, state, or any other metadata

Error Cases

ConditionError
Sketch not found"Sketch not found: 'bad-id'"
No workspace open"No workspace is currently open"
algorithm is empty string"Algorithm cannot be empty"
Validation fails (syntax error)"Algorithm validation failed: [error details]"
Validation fails (missing return)"Algorithm validation failed: sketch function must return an object with initializeSystem"

Golden Path Example

Request:

{
  "sketchId": "noise-grid",
  "algorithm": "function sketch(p, state) {\n  p.setup = () => {\n    p.createCanvas(state.canvas.width, state.canvas.height);\n    p.randomSeed(state.seed);\n    p.noiseSeed(state.seed);\n  };\n  p.draw = () => {\n    p.background(state.colorPalette[0]);\n    const n = state.params.cells;\n    const w = p.width / n;\n    for (let i = 0; i < n; i++)\n      for (let j = 0; j < n; j++) {\n        const val = p.noise(i * 0.15, j * 0.15, state.seed * 0.001);\n        p.fill(p.lerpColor(p.color(state.colorPalette[0]), p.color('#ffffff'), val));\n        p.noStroke();\n        p.ellipse(i * w + w/2, j * w + w/2, w * val * 0.9);\n      }\n    p.noLoop();\n  };\n  return { initializeSystem() {} };\n}",
  "validate": true
}

Response:

{
  "success": true,
  "sketchId": "noise-grid",
  "renderer": "p5",
  "algorithmLength": 582,
  "validationPassed": true
}