1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// The example composition, offline: the stub agent speaks MCP to code mode in Node (runCompose, a
// stand-in for the host's call), and on the Rust atlibs CLI with js/cli.mjs as its exec handler.
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { mkdtemp, readFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { createNodeContext } from "@publicdomainrelay/atlibs/server/index.js";
import { EXAMPLE, build, runAgent } from "./agent.mjs";
const ctx = createNodeContext({ id: "code-mode-example-test", stateDir: await mkdtemp(join(tmpdir(), "code-mode-example-")) });
const text = (value) => JSON.parse(value.result.content[0].text);
test("Node: the agent lists the tools and runs its program, whose call goes through the host's call", async () => {
const call = async (nsid, request) => (assert.equal(request.service, "did:plc:alice"), { handle: "alice.test", collections: ["a", "b"] });
const ran = await runAgent(ctx, { code: EXAMPLE, input: { did: "did:plc:alice" } }, { call });
assert.equal(ran.error, undefined);
assert.equal(ran.result.stdout, "server: atlibs-code-mode 2025-11-25\ntool: run_assemblyscript\n");
assert.deepEqual(text(ran.result.value), { stdout: "describing did:plc:alice\n", result: { handle: "alice.test", collections: 2 } });
assert.deepEqual(ran.graph.map((g) => [g.handler, g.ok]), Array(4).fill(["mcp", true]));
});
test("compose.ts writes composition.json", async () => {
const { stdout } = await promisify(execFile)(process.execPath, [fileURLToPath(new URL("compose.ts", import.meta.url))]);
assert.equal(stdout, await readFile(new URL("composition.json", import.meta.url), "utf8"));
});
const atlibs = await promisify(execFile)("atlibs", ["--help"]).then(() => "atlibs", () => undefined);
test("Rust: atlibs run with js/cli.mjs as the mcp exec handler", { skip: !atlibs && "no atlibs CLI on PATH (cargo host in ../atlibs)" }, async () => {
await build(); // modules/stub-agent.wasm, beside the composition's modules/stub-agent.json
const handler = `mcp=exec:${process.execPath} ${fileURLToPath(new URL("../js/cli.mjs", import.meta.url))}`;
const input = { input: { code: 'export function main(run: Run): void { print("on wasmtime\'s host"); run.finish(Json.number(7)); }' } };
const { stdout } = await promisify(execFile)("atlibs", ["run", fileURLToPath(new URL("composition.json", import.meta.url)), "agent", "run", JSON.stringify(input), "--handler", handler, "--allow-exec"], { env: { PATH: process.env.PATH, HOME: process.env.HOME } });
assert.deepEqual(text(JSON.parse(stdout).result.value), { stdout: "on wasmtime's host\n", result: 7 });
});