Skip to content

Quickstart

This example registers demo.ping, executes one known binding and prints {"value":"pong"}. Its fake sandbox deliberately ignores the submitted code. It checks registration, validation and cleanup, not JavaScript isolation.

import { Mycelium } from "@fungi.computer/mycelium";
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { z } from "zod";
const runtime = await Mycelium.make({
modules: [
Mycelium.module({
id: "example-demo",
name: "demo",
operations: {
ping: Mycelium.operation({
input: z.object({}),
output: z.object({ value: z.string() }),
execute: async () => ({ value: "pong" }),
}),
},
}),
],
sandbox: {
execute: (request, signal) => {
signal.throwIfAborted();
const ping = request.bindings.demo?.ping;
if (ping === undefined) throw new Error("Missing demo.ping binding");
return ping({});
},
},
});
const lease = await runtime.acquire();
try {
const prepared = await Effect.runPromise(
lease.executeTool.prepare({
type: "toolCall",
id: "example-execute",
name: "execute",
arguments: { code: "return demo.ping({})" },
}),
);
const parts = await Effect.runPromise(Stream.runCollect(prepared.execute()));
const text = parts[0]?.result.content[0];
if (text?.type !== "text" || text.text !== '{"value":"pong"}') {
throw new Error("Unexpected execute result");
}
console.log(text.text);
} finally {
await lease.release();
await runtime.close();
}

The public-import fixture uses the same known-binding pattern. A real evaluator must implement the public sandbox contract. Its request carries code, execution identity, limits and admitted bindings. Do not replace it with evaluation in the host’s privileged process.