Skip to content

Class: StateMachineGate

Defined in: packages/core/src/fsm/StateMachineGate.ts:196

Temporal Anti-Hallucination Engine.

Wraps an XState v5 finite state machine and controls which MCP tools are visible to the LLM based on the current workflow state.

Hard constraint: Tools not bound to the current state are removed from tools/list entirely — the LLM physically cannot call them.

Soft constraint: suggestActions (HATEOAS) continues to recommend the best next action within the visible set. Zero conflict.

Constructors

Constructor

ts
new StateMachineGate(config): StateMachineGate;

Defined in: packages/core/src/fsm/StateMachineGate.ts:207

Parameters

ParameterTypeDescription
configFsmConfigFSM definition (states, transitions, initial state)

Returns

StateMachineGate

Accessors

currentState

Get Signature

ts
get currentState(): string;

Defined in: packages/core/src/fsm/StateMachineGate.ts:293

Get the current FSM state.

Returns

string


hasBindings

Get Signature

ts
get hasBindings(): boolean;

Defined in: packages/core/src/fsm/StateMachineGate.ts:337

Check if any tools are bound to this FSM gate.

Returns

boolean

true if at least one tool is state-gated

Methods

bindTool()

ts
bindTool(
   toolName, 
   allowedStates, 
   transitionEvent?): StateMachineGate;

Defined in: packages/core/src/fsm/StateMachineGate.ts:279

Bind a tool to specific FSM states.

The tool will only appear in tools/list when the FSM is in one of the specified states.

Parameters

ParameterTypeDescription
toolNamestringMCP tool name (flat: cart_add_item, grouped: cart)
allowedStatesstring[]FSM states where this tool is visible
transitionEvent?stringEvent to send on successful execution (optional)

Returns

StateMachineGate

this for chaining

Example

typescript
gate.bindTool('cart_add_item', ['empty', 'has_items'], 'ADD_ITEM');
gate.bindTool('cart_checkout', ['has_items'], 'CHECKOUT');

clone()

ts
clone(): StateMachineGate;

Defined in: packages/core/src/fsm/StateMachineGate.ts:474

Create a lightweight clone of this gate with the same config and bindings but independent mutable state.

Used in serverless/edge deployments where concurrent requests must not share _currentState. Each request gets its own clone, restores session state into it, transitions, and saves — without interfering with other concurrent requests.

The clone starts uninitialized (no XState actor) so the first transition() call will create a fresh actor from the cloned state.

Returns

StateMachineGate

A new StateMachineGate with identical config and bindings


dispose()

ts
dispose(): void;

Defined in: packages/core/src/fsm/StateMachineGate.ts:491

Stop the XState actor and release resources.

Returns

void


getTransitionEvent()

ts
getTransitionEvent(toolName): string | undefined;

Defined in: packages/core/src/fsm/StateMachineGate.ts:328

Get the transition event for a tool (if any).

Parameters

ParameterTypeDescription
toolNamestringMCP tool name

Returns

string | undefined

The event string, or undefined if no transition is bound


getVisibleToolNames()

ts
getVisibleToolNames(toolNames): string[];

Defined in: packages/core/src/fsm/StateMachineGate.ts:318

Filter a list of tool names by the current FSM state.

Parameters

ParameterTypeDescription
toolNamesstring[]All registered tool names

Returns

string[]

Only the tools allowed in the current state


init()

ts
init(): Promise<boolean>;

Defined in: packages/core/src/fsm/StateMachineGate.ts:222

Initialize the XState actor (lazy-loaded).

Called automatically on first use. Can be called explicitly at boot time for eager initialization.

Returns

Promise<boolean>

true if XState is available and the actor started


isToolAllowed()

ts
isToolAllowed(toolName): boolean;

Defined in: packages/core/src/fsm/StateMachineGate.ts:306

Check if a specific tool is allowed in the current FSM state.

Tools not registered via bindTool() are always visible (ungated — they don't participate in FSM gating).

Parameters

ParameterTypeDescription
toolNamestringMCP tool name to check

Returns

boolean

true if the tool should appear in tools/list


onTransition()

ts
onTransition(callback): () => void;

Defined in: packages/core/src/fsm/StateMachineGate.ts:408

Register a callback that fires when the FSM state changes.

Used by ServerAttachment to emit notifications/tools/list_changed when a state transition makes tools appear or disappear.

Parameters

ParameterTypeDescription
callback() => voidFunction to call on state change

Returns

Unsubscribe function

ts
(): void;
Returns

void


restore()

ts
restore(snap): void;

Defined in: packages/core/src/fsm/StateMachineGate.ts:443

Restore FSM state from a persisted snapshot.

Resets the XState actor (if running) so the next transition() re-initializes the machine starting from the restored state. This ensures restore → transition works correctly in serverless/edge deployments (Vercel, Cloudflare Workers).

Parameters

ParameterTypeDescription
snapFsmSnapshotPreviously saved snapshot

Returns

void


snapshot()

ts
snapshot(): FsmSnapshot;

Defined in: packages/core/src/fsm/StateMachineGate.ts:426

Create a serializable snapshot of the current FSM state.

Used with FsmStateStore for serverless deployments where FSM state must survive across request boundaries.

Returns

FsmSnapshot

Serializable snapshot


transition()

ts
transition(eventType): Promise<TransitionResult>;

Defined in: packages/core/src/fsm/StateMachineGate.ts:349

Send an event to the FSM, potentially triggering a state transition.

Parameters

ParameterTypeDescription
eventTypestringThe event to send (e.g. 'ADD_ITEM', 'CHECKOUT')

Returns

Promise<TransitionResult>

Result indicating whether the state changed