Liking cljdoc? Tell your friends :D

Any language: libautonomous (the C ABI)

An app does not have to be written in Clojure to be agent-operable. libautonomous is the autonomous runtime compiled into a shared library with jolt (Clojure on Chez Scheme). Any language that calls C can load it: C, C++, Rust, Go, Zig, Odin, Nim and so on.

The app keeps its own code and its own sockets. The library keeps everything autonomous decides: malli validation of arguments (with the same lenient decoding, so "3" becomes 3), scopes, leases, the journal, the manifest agents read. The app's tools are called back through one function it defines.

agent --MCP--> bridge (bb / JVM / cljw)  <--websocket-- your app (Rust, C, Go, ...)
                                                          |  frames in, frames out
                                                          v
                                                     libautonomous.so
                                                       validate, scope, journal
                                                          |
                                                          v
                                               autonomous_host_dispatch (your code)

This is the production channel of websocket.md: the app dials the bridge with a runtime token, so it opens no port and runs no REPL.

Build it

You need jolt and the Chez Scheme kernel files it links against (a threaded, -fPIC build of the Chez version jolt was made with, found at JOLT_CHEZ_CSV). Then:

bb cabi        # cabi/build/libautonomous.so, then the C smoke test as a 0.3 and a 0.2 host
bb cabi-rust   # the Rust example app
bb cabi-go     # the Go (cgo) example: registers a tool, dispatches calls, prints answers
bb cabi-zig    # the same in Zig (zig build run)
bb cabi-bindings  # all of the above in one go
bb cabi-e2e    # live: the Rust app dials a bb bridge, agents call it over MCP
languagebindingexample
Ccabi/include/autonomous.hcabi/examples/c/smoke.c
Rustcabi/bindings/rust (crate)cabi/examples/rust
Gocabi/bindings/go (cgo package)cabi/bindings/go/example, see its README for runtime.LockOSThread
Zigcabi/bindings/zig/autonomous.zigcabi/bindings/zig/example, zig build run

The library is about 30 MB: it carries the jolt runtime, its GC, malli and the autonomous runtime. It builds in about a minute.

The ABI

cabi/include/autonomous.h is the contract. Specs are EDN, because a tool's input is a malli schema; tool args and results are JSON; requests and frames are opaque text you pass through.

exportdoes
jolt_library_init(argc, argv)start the runtime, once
jolt_lookup(name)find an export below
autonomous_set_app(info_json)name and describe the app for agents
autonomous_reg_tool(id, spec_edn)declare a tool; answers {"ok": "<mcp name>"} or {"error": ..}
autonomous_unreg_tool(id)remove one
autonomous_journal(entry_json)record an app event agents can read
autonomous_ws_hello(token, runtime_id, label)the first frame on a new socket
autonomous_ws_kind(frame)request, welcome, refused, fatal or other
autonomous_ws_reply(frame)the frame answering a request, "" if none is due
autonomous_rpc(request_edn)answer one request in process (tests)

The app defines one dispatcher, and exports it from its executable (-rdynamic, or -Wl,--export-dynamic):

/* 0.3 and later: told who called */
const char *autonomous_host_dispatch_v2(const char *tool_id, const char *args_json,
                                        const char *grant_json);
/* 0.2: grant-blind, still supported */
const char *autonomous_host_dispatch(const char *tool_id, const char *args_json);

It receives arguments that already passed the tool's schema and answers the result as JSON. {"error": {"message": "..."}} reports a failure; the agent sees it as a tool error and the journal records :tool-threw.

grant_json is the caller, in the shape autonomous.js hands its handlers: {"actor": "agent-1", "scopes": ["read", "write"]}. Use the actor to say who changed what. The library shows the actor the way it shows actors everywhere else (autonomous.domain.naming/shown-actor). It is printable ASCII only: a double quote, a backslash and every other UTF-16 code unit is escaped as \uxxxx (lowercase hex), so a bidi override (\u202e), a zero-width space (\u200b) or a control character is visible, and an emoji arrives as its surrogate pair (\ud83d\ude00). The require-approval prompt shows actors through the same function. The actor reaches the host whole: one whose shown form is longer than AUTONOMOUS_MAX_SHOWN_ACTOR (64) characters is refused with :host/actor-too-long before your code runs, so two different actors never reach the host as the same string. A decoded actor is at most 64 printable-ASCII bytes. Parse grant_json with a JSON parser, and never slice it or size a buffer from it: a JSON writer may escape any character (jolt writes / as \/), so the encoded actor can take up to 6 bytes per character. Scopes are sorted names. The library looks _v2 up once per process and process-wide (dlsym over every loaded object), so a process must hold exactly one definition of each dispatcher. If any loaded object exports _v2, the library calls only _v2. The Rust binding crate exports both dispatchers, so do not link it into a process that defines its own. It never hands _v2 an actor that names no one: an empty, whitespace-only or invisible-only actor (controls, zero-width, bidi, separators) is refused with :host/grant-missing before your code runs. A request with no actor at all reaches the host as "anonymous", as everywhere in autonomous. An app built against 0.2 keeps working unchanged; the version is in AUTONOMOUS_VERSION_MAJOR / AUTONOMOUS_VERSION_MINOR.

In Rust, Runtime::tool_as(id, spec, |args, who| ..) gives the handler a Grant. Such a handler fails closed under a 0.2 library, which passes no grant. Runtime::tool stays grant-blind.

A tool spec:

{:description "Receive qty units of sku into stock. Answers the new on-hand count."
 :input       [:map [:sku [:string {:min 1}]] [:qty [:int {:min 1}]]]
 :read-only?  false          ; read-only tools need only the :read scope
 :scopes      #{:write}}     ; optional; any keyword the app invents

The same spec as JSON

An author who does not write Clojure can describe tools in JSON instead (autonomous.json-spec). input and output are either a JSON Schema object or a string of malli EDN; $refs resolve within the file, so tools can share $defs. A file holds one tool, an array of them, or {"tools": [...]}:

{"$defs": {"Sku": {"type": "string", "minLength": 1}},
 "tools": [{"id": "inventory/receive",
            "description": "Receive qty units of sku into stock.",
            "input": {"type": "object",
                      "properties": {"sku": {"$ref": "#/$defs/Sku"},
                                     "qty": {"type": "integer", "minimum": 1}},
                      "required": ["sku", "qty"]},
            "readOnly": false,
            "scopes": ["write"]}]}

bb tool-spec tools.json prints each tool as id<TAB>edn-spec: the text autonomous_reg_tool takes, so the ABI is the same for both forms. In a Clojure host, autonomous.json-spec.load/register-file! registers the file with a handler per tool id. An invalid spec is refused with what to fix (:invalid-tool), never half-registered for want of a handler, and bb tool-spec then prints nothing and exits 1.

A spec is refused rather than read loosely:

  • a $ref that does not resolve within the file (a typo, a path out of it, a URL), a $ref cycle, or a type that is not a JSON Schema type, any of which would otherwise accept any argument;
  • a key the form does not know (scope, readonly, ...), naming the key meant (scopes, readOnly);
  • malli EDN that does not compile as a tool, blank EDN, and EDN that carries code ([:fn ...], a list). Only the host can allow code, with {:allow-fn? true}; a file cannot.

The OpenAPI source refuses the first kind too (:openapi/unresolved-schema), except a cycle, which OpenAPI allows.

The loop

Every language does the same few steps (the Rust example, cabi/examples/rust/src/main.rs, is about 200 lines with its tools):

  1. jolt_library_init, then register the app and its tools.
  2. Open a websocket to wss://bridge/autonomous/ws; send autonomous_ws_hello(token, runtime_id, label).
  3. For each text frame: ask autonomous_ws_kind. On request, send back autonomous_ws_reply(frame). On fatal, stop. Ignore the rest.
  4. When the socket drops, redial with backoff under the same runtime id, so attached agents stay attached.

On the bridge: bb token --actor <target> --scopes runtime mints the app's token, and bb serve --target-id <target> --ws HOST:PORT --http HOST:PORT serves it (see websocket.md).

Rules the runtime imposes

  • One thread. Call jolt_library_init and every export from the same thread. The library calls autonomous_host_dispatch on that thread, so a tool may block it; hand long work to your own threads and answer from what they leave behind. (jolt allows other threads through :collect-safe exports; this library does not declare any yet.)
  • Strings. Arguments are copied in. A returned string is valid until the next call into the library: copy what you keep. The result of your dispatcher must stay valid until it returns to the library, which copies it.
  • No eval. As over the websocket from a Clojure app, the bridge reaches only the tools the app declared.

What is still open

  • Bindings: C, Rust, Go and Zig. The Go and Zig examples run in process (autonomous_rpc); only the Rust app dials a bridge (bb cabi-e2e). The ABI takes EDN specs; a JSON spec reaches it through bb tool-spec, not through a JSON export of its own.
  • The host dispatcher sees the args only, not the caller's grant (actor, scopes), which autonomous.js already passes.
  • The library is one process-wide runtime; the per-instance runtime of the surface work could give each embedder several.
  • Size: 30 MB per app. A thin, library-free client (the frame protocol spoken directly, decisions on the bridge) would suit tiny apps; it gives up in-process validation and the journal.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close