Applicability
When to Use
✓When business processes have well-defined states and transitions
✓When you need to track workflow progress
✓When different actions happen at different stages
Overview
How It Works
This pattern models business processes as finite state machines where each state transition triggers actions via MCP servers. The current state is persisted in a database, and the agent evaluates available transitions based on the current state and incoming events.
For example, an order workflow might have states: created, paid, shipped, delivered, returned. Each transition (e.g., paid -> shipped) triggers specific MCP server actions: create a shipping label, notify the customer, update the CRM.
Implementation
Code Example
typescript
const orderWorkflow = {
created: {
pay: { target: "paid", action: async (order) => await stripe.createCharge({ amount: order.total }) },
cancel: { target: "cancelled", action: async (order) => await notifyCustomer(order, "cancelled") }
},
paid: {
ship: { target: "shipped", action: async (order) => {
const label = await createShippingLabel(order);
await twilio.sendSMS({ to: order.phone, body: `Shipped! Track: ${label.trackingUrl}` });
}}
},
shipped: {
deliver: { target: "delivered", action: async (order) => await requestReview(order) }
}
};
async function transition(orderId, event) {
const order = await postgres.query("SELECT * FROM orders WHERE id=$1", [orderId]);
const currentState = orderWorkflow[order.state];
const trans = currentState?.[event];
if (!trans) throw new Error(`Invalid transition: ${order.state} -> ${event}`);
await trans.action(order);
await postgres.query("UPDATE orders SET state=$1 WHERE id=$2", [trans.target, orderId]);
}Quick Info
Categoryorchestration
ComplexityMedium