# Patch a graph safely

Preview and save targeted operations while preserving unrelated pins, literals and metadata.

Canonical URL: https://opennodeflow.dev/wiki/agent-graph-patching/
JSON reference: https://opennodeflow.dev/wiki/agent-graph-patching/index.json
Source snapshot: 2026-10-23

Use `onf_patch_graph` to edit an existing workflow. It applies an ordered operation list to a cloned draft, validates the result and saves under the workflow's revision guard. This reduces accidental replacement of unrelated content compared with reconstructing a graph from summaries or redacted readbacks.

## Read the target first

Call `onf_get_workflow` or `onf_get_graph` for the saved graph/revision. Read exact pinned definitions for nodes being changed. Discover actual node, edge and socket IDs; labels are not identities. Read relevant context notes before changing a branch's meaning.

The tool takes `workflowId`, `expectedRevision`, `operations`, optional `dryRun` and optional `allowInvalid`. An operation list has 1–1,000 entries:

| Operation | Target |
| --- | --- |
| `add_node` | Complete pinned node instance |
| `update_node` | Selected node fields |
| `remove_node` | One actual node ID |
| `connect` | Complete edge with exact socket IDs |
| `disconnect` | One actual edge ID |
| `upgrade_node` | Explicit version of that node's definition |
| `set_frames` | Complete intended frame array |

## Preview a small change

Replace these placeholders and revision with a current read:

~~~json
{
  "workflowId": "FLOW_ID",
  "expectedRevision": 7,
  "dryRun": true,
  "operations": [
    {"op":"update_node","nodeId":"NODE_ID","patch":{"label":"Validate order"}}
  ]
}
~~~

Dry run checks the revision and returns a draft without saving or executing. Review issues and the resulting graph, then apply the intended operations with `dryRun:false` against a still-current revision. If another editor saves meanwhile, reread and reconcile; do not blindly substitute a higher number.

## Replacement remains literal

Although `update_node` targets one node, supplied `config` and `inputs` objects are deliberate replacements rather than arbitrary deep merges. Preserve their full intended fields. `set_frames` similarly replaces the collection. Inspect the wiring and documentation consequences of removing a node.

Visibility arrays contain socket IDs and change presentation only. Hiding a socket does not remove its value, validation or execution behavior. A variable binding contains exact variable/workspace/type identity and needs deliberate rebinding if that variable disappears.

`allowInvalid:true` can preserve an incomplete draft; enabled nonmanual automation still requires a valid graph. Finish with validation and a readback of the saved revision. Keep concise context notes current when purpose or wiring changes. For helper-originated changes, follow the separate [reviewed proposal workflow](/wiki/agent-reviewed-changes/).

---

## Documentation provenance

- `docs/MCP.md`
- `server/mcp-graph.ts`
- `server/mcp-tools.ts`

This page describes maintained source, not a live inventory or an authorization to perform actions.
