Statewright

Agent-Generated Workflows

Let your agent create its own state machine from context

Agent-Generated Workflows

Your agent can design its own constraints. Tell it what phases you need, it generates a state machine definition, pushes it via MCP, and then operates within the guardrails it just created. The workflow schema is at statewright.ai/workflow-schema.json, or the agent can call statewright_search_docs("schema") to pull it directly.

The Flow

  1. Agent reads the workflow schema at https://statewright.ai/workflow-schema.json
  2. Agent generates a workflow definition based on your task description
  3. Agent calls statewright_create_workflow(name, definition) to save it
  4. Agent calls statewright_load_workflow(name) to activate enforcement

Example Prompt

Read the statewright workflow schema at https://statewright.ai/workflow-schema.json.
Then create a workflow called "data-pipeline" for an ETL task:
1. extract phase: only Read, Bash(curl *), Bash(wget *)
2. transform phase: Read, Edit, Write, Bash(python *)  
3. load phase: Bash(psql *), Bash(curl *) with approval gate
4. verify phase: Read, Bash(psql *) read-only queries only

The agent generates the JSON and pushes it via MCP:

{
  "id": "data-pipeline",
  "initial": "extract",
  "states": {
    "extract": {
      "allowed_tools": ["Read", "Bash"],
      "allowed_commands": ["curl", "wget"],
      "on": { "EXTRACTED": "transform" }
    },
    "transform": {
      "allowed_tools": ["Read", "Edit", "Write", "Bash"],
      "allowed_commands": ["python", "python3"],
      "on": { "TRANSFORMED": "load" }
    },
    "load": {
      "allowed_tools": ["Bash"],
      "allowed_commands": ["psql", "curl"],
      "on": {
        "LOADED": {
          "target": "verify",
          "requires_approval": true
        }
      }
    },
    "verify": {
      "allowed_tools": ["Read", "Bash"],
      "allowed_commands": ["psql"],
      "on": { "VERIFIED": "completed" }
    },
    "completed": { "type": "final" }
  }
}

Schema Validation

The gateway validates the definition against the schema before saving. Invalid definitions are rejected with a descriptive error. Common validation failures:

  • State referenced in a transition doesn't exist
  • Initial state not defined in states
  • Guard referenced but not defined in guards section
  • Empty states object

When to Use This

You're debugging a production database issue at 2am. You don't have a workflow for this. Tell the agent what phases you want — read-only diagnosis, then a fix with an approval gate, then verification — and it generates the workflow before it starts. Your sleep-deprived review gate still works.

Also useful for complex multi-step operations where you want guardrails but don't want to open a browser, and for team setups where a lead agent creates the workflow and execution agents follow it.

If schema validation fails, the agent gets the error and can fix the definition. Common mistakes: referencing a state that doesn't exist in a transition target, or using a guard name that's not defined.

The Round-Trip

Workflows created via MCP appear in the visual editor where you can visualize the state diagram, tweak tool restrictions, and add guards. Edit in the UI, then the agent loads the updated version next time.

The loop: an agent creates a workflow, constrains itself with it, operates within those constraints, and the run history shows whether the constraints were right. Tighten or loosen on the next iteration. The agent builds its own cage — and the cage makes it better at the job.

On this page