Connect an agent over MCP
How an agent host wraps the Workflows operations in its own tools: the engine each call uses, the operations that change state, and telemetry.
Early access
Workflows is in early access, built in public. Read How early access works before you rely on it.
@sanity/workflows/agents is a library of Workflows operations for AI agents. An agent host wraps each operation in a tool of its own, so an agent can work with your workflows. With these tools, the agent can:
- Operate workflows: list what’s deployed and running, inspect and diagnose an instance, start instances, and fire actions.
- Author workflows: read the authoring guide, validate a definition, and deploy it into a partition you name.
This guide covers what a host needs to wrap the operations: the engine each call runs against, the operations that change state, telemetry, and an example tool. Every operation, with its input and result, is in the Workflows MCP tools reference.
Run the operations in an agent host
The package is a library, not a server you run. Your agent host embeds it. The host provides the transport, authentication, and engine construction. It also defines each tool’s name, description, and input schema. The package supplies one typed function per operation, its result type, and the authoring guide.
The host creates a Sanity client with a token. The Content Lake checks every call against that token’s access, so the token limits what the agent can read or change. Give it no more access than the agent needs.
Workflow history records each action as the token’s user. History can’t tell the person driving the agent apart from that user. To mark entries as coming from the agent, pass an executionContext with kind mcp to createWorkflowEngine. The engine then adds that label to each history entry. The label is advisory. For more on execution contexts, see Actors and enforcement.
An engine from createWorkflowEngine accepts document references into any resource the token can reach, not only the workflow resource. That includes a dataset: in any project, and a media-library:, canvas:, or dashboard: resource. So the agent can:
- Start a workflow on a document in another dataset, project, or resource.
- Fire actions whose params reference content there.
getWorkflowState and listWorkflowInstances read each subject’s title from the subject’s own resource.
The engine stores any well-formed global document reference (GDR) without checking that the token can reach it. A start fails before it writes if a supplied reference names a document that doesn’t exist, but a reference to a resource the token can’t reach is stored and then fails later, when the workflow reads or writes there. Those reads and writes use the same token, so scope the token to only the resources the agent should reach.
To accept fewer resources, build the engine yourself. Call createEngine from @sanity/workflows with your own resourceClients resolver, and pass that engine to the operations.
Target a workflow environment
There’s no default environment. Each engine serves one deployment, so build it for the address of that deployment. The address has two parts:
workflowResource: the deployment’s workflow resource, such as a dataset.partition: the deployment’s partition.
For the quick start’s dev deployment, the address is:
import type {WorkflowEnvironmentAddress} from '@sanity/workflows/agents'
export const address: WorkflowEnvironmentAddress = {
workflowResource: {type: 'dataset', id: 'PROJECT_ID.DATASET_NAME'},
partition: 'dev',
}In your host, you decide how the agent names the environment. It can take a resource and partition as tool input, or bind one environment up front. A deployment without a partition in sanity.workflow.ts uses default. To let the agent find partitions, wrap listWorkflowPartitions. It takes a client for one resource instead of an engine. For the address formats and errors, see the address reference.
Know what the agent can change
The agent can list, inspect, and diagnose without changing anything. Only three operations change state:
startWorkflowstarts an instance.fireWorkflowActionadvances an instance. Read its current state first.deployWorkflowDefinitionsdeploys workflow definitions.
The Content Lake enforces access, not the operations. They report disabled actions, guard verdicts, and validation errors to help the agent, but those checks are advisory. Dataset access control (custom roles) sets what the host’s token can do. A token that can’t write to an environment can’t fire actions or deploy there.
Some definition features need a newer reader model, the lowest data model version an engine must support to read the definition’s documents. Before the agent deploys such a definition with deployWorkflowDefinitions, follow Upgrade Workflows packages. Then pass the reviewed expectedMinReaderModel. If you leave it out, the operation uses the baseline reader model, 4. The model a definition needs depends on its features, not on the package version.
Telemetry
The operations record no Workflows telemetry unless the host passes a logger from createMcpSessionTelemetry to createWorkflowEngine. The write operations then record engine events, such as a started instance or a fired action. Read operations and validation record none.
Even with a logger, nothing is sent in these cases:
- The host passes
denied: true. CIorDO_NOT_TRACKis set to a true value, such as1ortrue. This denies the session even when the host passesdenied: false.- The account hasn’t granted telemetry consent.
Wrap an operation in a tool
Each operation is a function you call as operation({engine}, input). It returns plain data that your host can send as the tool result. The operations don’t check their input at runtime, so validate each tool’s input against your own schema before you call one.
This example tool fires an action. It builds an engine for the address the agent passed, calls fireWorkflowAction, and returns the new state. On failure, it returns the text from workflowErrorText, which starts with the error’s stable kind:
import {createClient} from '@sanity/client'
import {parseResourceGdr} from '@sanity/workflows'
import {
createWorkflowEngine,
fireWorkflowAction,
workflowClientConfig,
workflowErrorText,
} from '@sanity/workflows/agents'
// The tool input, after your host has validated it against its own schema.
interface FireActionToolInput {
workflow_resource: string
partition: string
instance_id: string
activity: string
action: string
params?: Record<string, unknown>
}
export async function fireActionTool(input: FireActionToolInput, token: string) {
const workflowResource = parseResourceGdr(input.workflow_resource)
const client = createClient(workflowClientConfig({resource: workflowResource, token, base: {}}))
const engine = createWorkflowEngine({
address: {workflowResource, partition: input.partition},
client,
executionContext: {kind: 'mcp'},
})
try {
const state = await fireWorkflowAction(
{engine},
{
instanceId: input.instance_id,
activity: input.activity,
action: input.action,
params: input.params,
},
)
return {content: [{type: 'text', text: JSON.stringify(state, null, 2)}]}
} catch (error) {
return {content: [{type: 'text', text: workflowErrorText(error)}], isError: true}
}
}In this example, token is the token your host authenticated for the person driving the agent. A host that serves several calls in a session can reuse one engine per resource and partition.
The authoring guide and some error messages use the suggested tool names, such as workflows_start and its instance_id input. If your host uses other names or input spellings, check that this text still makes sense to your agent.
The Embedding API lists the other exports: client policy, engine construction, telemetry, and error text.
Next steps
- Workflows MCP tools reference: every operation’s input, result, and errors.
- Configure and deploy workflow definitions: deploy definitions and the sanity.workflow.ts deployments the environment addresses point at.
- Test a workflow before you deploy it: prove a definition in the in-memory bench before an agent deploys it.
- Browse the full API in @sanity/workflows/agents on reference.sanity.io.
Was this page helpful?
Feedback submitted, thanks for helping us improve the docs. Have you tried these alternatives?