Skip to main content

Dataset patterns: mock systems and shared stores

Use datasets with code and AI tools to mock an external system behind a plug-in connection, keep a store that several workflows share, or build a service that processes and stores records

A dataset on its own is a table with tools to read and change rows. Put custom tools over it and publish only those tools, and the thunk becomes a small system that other thunks call: a mock of a business application, a store that several workflows share, or a service that processes documents and keeps what it found.

This article describes three patterns built that way. All three use the same layers.

The layers

  1. Tables. Datasets hold the raw records, one dataset per kind of record, the way a database holds tables. To link a row to a row of another dataset, store the other row's id (or a business key such as an employee id) in a Text column.

  2. Business tools. Custom tools do the work on those records and call the dataset tools to read and write them. Use a code tool for exact rules: filters, validation, totals, paging, the wording of an error. Use an AI tool for judgment, such as reading the fields out of a document.

  3. Interface. Publish only the business tools. A dataset's tools are off by default when you publish a thunk, and they should stay off: callers then depend on the business operations, and you can change the tables without changing them.

The thunk that holds the datasets is the provider. Its own code tools can call its dataset tools whether or not those are published: enable the dataset's tools under each code tool's Tools section, or the call fails with Tool … not found. Only the provider has datasets; the thunks that call it use its published tools.

Mock an external system

A workflow that changes records in an HR system, an ERP or a ticketing system needs something to run against while you build and test it. A mock is a provider thunk that answers like the real system, from records you control. This uses the plug-in architecture described in How to use modularity effectively when building a thunk: the workflow calls the system through a plug-in connection, and the mock and the real system are two implementations of the same interface.

Build the mock from the interface

  1. Write the interface the workflow needs, based on what the real system returns: its tool names, inputs and declared outputs. Add it to the workflow as a plug-in connection.

  2. Create an MCP server thunk from the interface file. Its tools start with the interface's names and inputs, which you can't change. Interface tools start as AI tools: on the Tools step, choose Code for each one.

  3. Add one dataset per kind of record the real system has. Use the real system's ids and field names in the columns, so that ids a step reads from the mock look like real ones.

  4. Seed rows with realistic records, including the ones that make the real system refuse a request.

  5. Write each interface tool as a code tool over the dataset tools, with the dataset's tools enabled under the code tool's Tools section (see Write the tools like the real system).

  6. Turn the export on under Admin → Deploy → Export. Creating the server does not publish it, and the plug-in connection only offers connections that match its interface.

For example, a mock of an HR system's job-change interface might have these datasets:

Dataset

Columns

Managers

managerName

Direct Reports

employeeId, employeeName, businessTitle, country, jobChangeInProgress (Yes or No)

Cost Centers

costCenterId, label, allowedCountries ("All" or a comma-separated list of countries)

Seed at least one direct report with a change already in progress, and cost centers that are allowed in only some countries, so the workflow meets the refusals it will meet in production.

Write the tools like the real system

Each interface tool reads the tables and returns what the real system would return:

  • The same envelope. If the real system returns pages of 10 with a total and hasNextPage, so does the mock. Declare the same output as the interface, so the binding matches.

  • The same refusals. Check the request the way the real system does and answer in its wording, such as "Cost center 'X' is not allowed for workers in France."

  • The same derived lists. A tool such as "cost centers allowed for this person" is a filter in code: look up the person's country, keep the cost centers whose allowedCountries is "All" or lists that country, and match the search text on the label.

const input = getTypedInput();
const refuse = (reason) => ({ success: false, message: reason, eventId: null });

const { data: reports } = await tools.query_direct_reports({
  filter: { items: [{ field: "employeeId", operator: "equals", value: input.employeeId }] },
});
const report = reports[0]?.properties;
if (!report) {
  return refuse(`The worker ${input.employeeId} is not one of your direct reports.`);
}
if (report.jobChangeInProgress) {
  return refuse(`There is already a job change in progress for ${report.employeeName}.`);
}

Read-only or stateful

Decide what a mock's write tools do:

  • Read-only. The tool checks the request and answers as the real system would, with a success message and an event id, but changes no rows. The data never drifts, and every test run starts from the same records. This suits most workflow tests.

  • Stateful. The tool also writes the change, so a later read sees it. Use this when a workflow reads back what it changed, or when a test covers several steps that depend on each other. Plan how to reset the rows between runs. There is no bulk delete: delete rows one at a time, and after each round of deletes query again from the start, without a page token, because deleting rows shifts the pages that follow. Test work items that run at the same time share the mock's rows, so a stateful mock can make concurrent tests interfere.

Switch between the mock and the real system

The workflow calls the plug-in connection, never the mock directly, so switching is a change to the binding, not to the steps:

  • One at a time. Bind the plug-in connection to the mock while you build and test. When you move to production, bind it to the real system. See Improve a live thunk safely and Move a thunk into production.

  • Both at once, per work item. Give the plug-in connection a default binding to the real system and a binding case that binds a value, such as mock, to the mock's connection. Add a work-item column such as Data source, and set Bind by Column Value to it on each step that uses the plug-in. Test work items set Data source to mock; live work items leave it empty and use the real system. See How a module is selected in How to use modularity effectively when building a thunk.

Run the same tests against the mock and the real system, so you know they behave the same way where they should. See Automated tests for custom tools and Automated tests for workflow steps.

A store that several workflows share

A dataset inside a workflow thunk belongs to that thunk. Another workflow thunk, or a copy of the thunk, has its own separate table. When several workflows must read and update the same records (decisions already made, a registry of suppliers, the state of a case that passes between teams), put the datasets in one provider thunk and give it business tools such as "record a decision" and "find past decisions".

  • Other thunks call the store through Imported Thunk Libraries in their Connections pane (see Modular reuse of tools), or through a plug-in connection bound to the store. Publish the store's business tools in its export settings. To import the library, the person building the calling thunk must be a user of the store thunk; people who run the calling workflow don't need access to it.

  • Every row records its owner. When an end user submitted the calling work item, that end user is the owner. Rows you add on the Data tab belong to you, and work items with no end user share one owner. Turn on the dataset's owner filter only when each end user should see only their own rows. The filter separates end users, not workflows: with it on, two workflows run for different people can't see each other's rows, and seed rows you added are hidden from everyone else.

  • The store's owners and admins see every row on the dataset's Data tab, whatever the filter.

  • A workflow copied for another team should connect to the same store, not copy it. A copy of the store thunk is a separate store, and it gets the seed rows (up to 500 per dataset) only when the copy also copies work items or content-folder documents.

See Building thunks from reusable parts for how libraries, interfaces and published thunks fit together.

A service that processes and stores

A provider can also do work on what it stores. An invoice store, published as an MCP server, takes invoice files, extracts them once and answers questions from what it stored:

Dataset

Columns (some)

Invoice Headers

VendorName, InvoiceNumber, InvoiceDate (Calendar date), TotalAmount, TotalsCheck, IngestedOn

Invoice Line Items

InvoiceId (the header row's id), LineNumber, Sku, Quantity, UnitPrice, Amount

Its published tools:

  • ingest_invoice (code) calls an internal AI tool that extracts the header and lines from the file. Code then checks the extraction: it adds up the lines and compares the sum with the invoice total, which is arithmetic, not a judgment for the AI. Before it writes, it looks for an invoice with the same vendor and invoice number and, if one is stored, returns that one with a status such as AlreadyStored instead of storing it twice. Then it writes the header row and the line-item rows.

  • get_invoice, find_invoices and search_line_items (code) query the tables and return the records in the service's own shape.

  • delete_invoice deletes the header and its lines.

The extraction tool and the helpers that write or delete line items are internal: leave them unpublished, so callers can only use the service's own operations.

Limits to design for

  • Pages of 256 rows. A query returns at most 256 rows per call. Filter in the query rather than reading the whole table, and while hasMore is true, follow nextPageToken. A query returns no total count: to return a total, read every page.

  • At most 50 tool calls in one code tool run. Every dataset read and write is one call, and so is each page of a query. To write many rows, handle fewer rows per call and call the tool more than once, for example in batches of 40 line items. See How custom code tools work.

  • No transactions and no unique keys. Each write changes one row, and a failed write does not undo the ones before it. A header and its line items are separate writes. Checking for an existing record before writing makes a retry safe, but two runs at the same moment can both write. Nothing stops two rows from having the same Name or business key.

  • Renames. Renaming a dataset renames its tools. The provider's own code tools that call the old names stop working; fix them in the provider. Callers keep using the published business tools, which don't change.

Did this answer your question?