Skip to content
CodeBook

Worker SDKs

Build an external CodeBook worker in TypeScript, Python, Java or .NET with a workflow-scoped token.

CodeBook provides TypeScript/JavaScript, Python, Java and .NET clients for outside workers. The packages live in the CodeBook app repository; they are not published to a package registry yet.

Connect a worker

  1. Use a private test factory and an empty test station. Create an external pull runtime in Factory settings → Runtimes, assign it to that station, and allow external execution in the factory policy.
  2. Have a workflow owner open the workflow's Share dialog, enable API access, and create a separate write token for this worker. Record whether API access was already enabled so you can restore the setting after a temporary check.
  3. Copy the MCP endpoint shown in that dialog and keep the token in your worker's secret environment. This is a workflow API token, separate from the personal OAuth connection used by Claude chat or ChatGPT.
  4. Move a synthetic test card into the external station. Confirm the run is waiting for the outside worker, and use that exact run ID for the check below. Do not test against a customer card.

From a checkout of the app repository:

npm install ./sdk/typescript
python3 -m pip install ./sdk/python

Keep CODEBOOK_MCP_ENDPOINT and CODEBOOK_WORKER_TOKEN in your worker's environment. The token grants access to one workflow. It does not grant SSO administration or expose model-provider credentials.

import { CodeBookWorker } from '@codebook/worker';
const worker = new CodeBookWorker({
  endpoint: process.env.CODEBOOK_MCP_ENDPOINT,
  token: process.env.CODEBOOK_WORKER_TOKEN,
});
const { work } = await worker.listWork();
const item = work.find(item => !item.claimed || item.claimed_by_you);
if (item) {
  const { brief } = await worker.claimWork(item.run);
  await worker.startRun(item.run);
  // Your runtime executes and verifies the work described by brief.
  await worker.requestHuman(item.run, 'Please review the output before accepting it.');
}

Python uses CodeBookWorker from codebook_worker, with methods such as list_work, claim_work, start_run and request_human. See the repository SDK README for complete examples and the method reference.

Java and .NET source clients

The Java client requires JDK 17 or later and uses the JDK HTTP client. From sdk/java, build a local JAR:

mkdir -p build/classes
javac --release 17 -Xlint:all -Werror -d build/classes src/main/java/work/codebook/worker/*.java
jar --create --file build/codebook-worker-0.1.0.jar -C build/classes .

The C# client targets .NET 10 and uses HttpClient and System.Text.Json. From sdk/dotnet, build and pack it locally:

dotnet build CodeBook.Worker/CodeBook.Worker.csproj --configfile NuGet.Config
dotnet pack CodeBook.Worker/CodeBook.Worker.csproj -o packages --configfile NuGet.Config

Both clients have no external library dependencies and preserve the complete brief, including fields added by the server. The supplied examples list waiting work by default. Their explicit --claim-one option claims, starts and heartbeats one task, then hands it to a person; it performs no implementation and never reports a passing result.

Java uses CodeBookWorker methods such as claimWork, heartbeat and requestHuman. C# uses CodeBookWorker methods such as ClaimWorkAsync, HeartbeatAsync and RequestHumanAsync. Keep the returned lease fence and supply it on heartbeat and completion. Those are the protocol operations that accept a fence; an old claim must stop after another claim takes control.

Java thread interruption and .NET cancellation tokens cancel the local HTTP request. They do not cancel the server run or prove that an uncertain write failed. The workflow protocol has no remote cancel or claim-release tool. Use the authorized app controls or report a truthful blocked or human-review outcome.

The Java 17-targeted build and .NET 10 build, protocol fixtures, safe examples and local package creation have been checked. These are source packages, not published Maven Central or NuGet releases, and the new-language fixture checks are separate from the deployed TypeScript acceptance below.

Progress, outputs and completion

  • Send heartbeat while working. It updates liveness without extending the runtime deadline.
  • Read getContext or getMemory after claiming a run. Canary station instructions remain frozen; card discussion and artifacts reflect current data.
  • Add progress with comment and typed outputs with produceArtifact. Output kinds come from the brief's artifact_kinds.
  • Report cumulative tokens with reportUsage. Repeating an earlier total never counts the same tokens twice. This is operational usage, not a charge to SSO billing.
  • Finish with pass, fail or block and an evidence summary. Python calls a passing result pass_work because pass is a language keyword. Completion still goes through CodeBook's contracts, evaluations and routes.
  • Use requestHuman to park the run for an authorized person's accept/reject decision. It stops the outside worker's control; it does not automatically resume after a chat answer.

Inspect the supplied context

The run’s Context usage → Prepared inputs records the brief prepared for claimWork and getContext, plus the memory-only response prepared for getMemory. It identifies the claim, linked retrieval selection, brief and envelope hashes, and approved-memory revisions. A record proves preparation, not receipt or execution by your worker. Outside-worker brief evidence

Failures and retries

Clients have request timeouts and surface HTTP, protocol and tool errors. They refuse redirects so a token is not forwarded to another endpoint. Deleting the token or disabling workflow API access revokes future requests.

Clients do not automatically retry writes. Claims and start calls are repeatable by the same token, cumulative usage is safe to repeat, and identical artifacts return the saved output. Comments are append-only: inspect the card after an uncertain response before retrying. Do not complete work after requesting human review.

Check a deployed factory

sdk/typescript/scripts/deployed-check.mjs checks one real run against the deployed API. From the app repository checkout, supply the endpoint and write token through your environment. For an interactive Bash session, this reads the token without showing it or placing it in shell history:

export CODEBOOK_MCP_ENDPOINT="<endpoint copied from the workflow Share dialog>"
read -r -s -p "Workflow write token: " CODEBOOK_WORKER_TOKEN
printf '\n'
export CODEBOOK_WORKER_TOKEN
read -r -p "Queued synthetic run ID: " CODEBOOK_CHECK_RUN
CODEBOOK_REPORT_TOKENS=0 node sdk/typescript/scripts/deployed-check.mjs "$CODEBOOK_CHECK_RUN"
unset CODEBOOK_WORKER_TOKEN

Enter the queued synthetic run's number when prompted. On Windows, supply the same environment variables through your worker's secret-management mechanism, then run the node command from PowerShell or the terminal you use. Never put a real token in a ticket, chat, committed file or screenshot. A production worker should load it from its deployment secret store.

With an allowed artifact kind and CODEBOOK_REPORT_TOKENS=0, expect these ten successful steps:

Step What to verify
list_work The intended run is queued on this workflow.
claim_work This worker receives the run's brief.
start_run The run becomes active.
heartbeat The run accepts a liveness update.
get_context The worker can read its scoped task context.
get_memory The worker can read the memory available to this run.
run_comment The lifecycle comment appears on the card.
produce_artifact The card receives SDK deployed check in an allowed output kind.
report_usage The operational usage total is recorded; zero is valid for this check.
request_human The run stops waiting for the worker and asks a person to decide.

The script skips the artifact if the brief allows no output kind and skips usage reporting if CODEBOOK_REPORT_TOKENS is unset. Those skips reduce the step count; they do not exercise the skipped operation. The check builds nothing, invokes no model, and does not pass the run itself.

Open the card in CodeBook, read its comment and artifact, then use the ordinary Approve or Reject action. Verify the resulting station and terminal run state. A successful script exit alone does not complete this human decision.

For a temporary acceptance check, return to Share, delete the dedicated test token and turn off workflow API access only if you enabled it solely for the test. Confirm the token is absent; later requests using it must be refused. Leave another worker's token or pre-existing API access intact.

The deployed TypeScript lifecycle passed all ten steps in a private synthetic factory on 18 November 2026, followed by owner acceptance, token revocation and API shutdown. This verifies that measured lifecycle, not a customer-provider integration or an uninterrupted 16-hour run. The same script is also exercised by the SDK's automated test against the real MCP dispatcher.

The SDK language does not restrict which programming languages a worker can edit. Choose the client that fits your runtime; the workflow’s permissions and completion rules remain the same.