@macrograph/cli
v0.0.0-dev-0
Published
MacroGraph CLI and self-hosted server.
Readme
MacroGraph self-hosted server
The server runs the collaborative browser editor, editor RPC, module settings RPC, and local runtime in one Node.js process. The current persistence model is one project in one SQLite file.
The browser workspace has Editor, Events, and Settings views. Edits and module configuration apply to the running project automatically; there is no deployment step. Events streams recent module events, execution node status, timings, and errors to signed-in owners/admins. Activity retains up to 100 events and the latest 200 nodes per event, with bounded payload/error previews. It is held only in memory, so restarting the server clears it. Event payloads may contain sensitive data and are not available to anonymous read-only viewers.
Local development
pnpm --filter @macrograph/cli devOpen http://localhost:5174. Vite serves and hot-reloads only the browser client, and proxies
/health, /auth, /api, /rpc, /rpc-ws and /module to the local channel's background
service (see Channels), which it finds or starts like the CLI does. That service runs
from source: Vite bundles src/index.ts with the release build's configuration into
node_modules/.cache/macrograph-dev/index.mjs and runs its serve --service with source maps.
When the bundle's sources change (the server or any workspace package it uses), it is rebuilt and
the service restarted, which the terminal reports; a failed build keeps the old service running.
The service's own output is not shown.
Several pnpm dev processes can run at once: each serves the client on the next free port, and all
proxy to the one service. Only one of them builds, watches and restarts it, holding a lock in
node_modules/.cache/macrograph-dev; when it exits, another takes over within a second, and
starting or taking over never restarts the service unless the bundle changed.
Interrupting pnpm dev stops only Vite: the service keeps running for local agents, and the next
pnpm dev reuses it if its bundle is unchanged. Stop or restart it with
MACROGRAPH_CHANNEL=local macrograph service stop|restart. It keeps its data in
$XDG_DATA_HOME/macrograph-local unless MACROGRAPH_DATA_DIR is set; data from the old
in-Vite backend in apps/server/project.db is not moved. The browser signs in as usual; the setup
link for a new data directory is printed by pnpm dev.
pnpm --filter @macrograph/cli dev:inlinedev:inline instead runs the backend inside Vite's server environment and hot-reloads it when
server or workspace dependencies change, disposing the old runtime before starting the next one.
Migrations are read directly from packages/persistence-sqlite/drizzle, and data stays in
MACROGRAPH_DATA_DIR or apps/server. It also serves the local channel, registering Vite's URL;
each reload overwrites the registration rather than removing it, so local agents never find it
missing, and requests during a reload wait for the backend. It refuses to start while another
server holds the channel (stop it with MACROGRAPH_CHANNEL=local macrograph service stop). When
it is interrupted its registration stays behind, and clients treat it as stale because its process
no longer answers.
Both load apps/server/.env (and Vite's mode-specific env files), with existing environment
variables taking precedence.
Production:
pnpm --filter @macrograph/cli build
PORT=3001 MACROGRAPH_DATA_DIR=./data pnpm --filter @macrograph/cli startstart runs macrograph serve, the foreground server configured by the environment below
(--port and --hostname override PORT and HOST).
PORT and HOST apply to production only: in development the backend's port is the local
channel's, or a private one with dev:inline. In production, the server and client share PORT. GET /health/live reports process liveness and
GET /health/ready reports readiness after persistence and module initialization. GET /health
is a compatibility alias for readiness.
Background service and local agents
The build is also the macrograph CLI (dist/esm/index.js, the package's bin). Run
macrograph to start a per-user background service if one isn't running and print its URL. The
service listens on loopback and keeps its data in $XDG_DATA_HOME/macrograph
(~/.local/share/macrograph) unless MACROGRAPH_DATA_DIR is set. While it runs, it describes
itself in $XDG_STATE_HOME/macrograph/service.json (~/.local/state, mode 0600), including a
local token that acts as the server's owner, even before setup. Deleting that file stops the
service. Until the server is set up, macrograph service status shows its setup link.
macrograph service start|stop|restart|statusmanages the service.macrograph service get|set|unset <hostname|port|token> [value]changes its settings in$XDG_CONFIG_HOME/macrograph/service.json(~/.config). Changes stop the service, which restarts with them on next use; unsettingtokenrotates it.macrograph mcpis a stdio MCP server for local agents. It starts the service if needed and relays each message to its/api/mcpwith the local token.macrograph mcp configprints how to connect OpenCode, Claude Code, Codex, and file-based clients;--client <id>prints one.--server <url>onmcpandservice statususes that server instead, with the token inMACROGRAPH_TOKEN, without discovering or starting anything. For example,MACROGRAPH_TOKEN="$(macrograph service get token)" macrograph mcp --server http://127.0.0.1:3170.
Channels
Each channel has its own service: registration, config, token, data and default port. Builds are
the latest channel (build with MACROGRAPH_CHANNEL=<channel> to bake in another), and
MACROGRAPH_CHANNEL at runtime picks a channel for any command.
| Channel | Registration and config | Data | Port |
| -------- | ------------------------ | -------------------------------------------------------------- | ----------------------------------- |
| latest | service.json | $XDG_DATA_HOME/macrograph | 3170 |
| local | service-local.json | $XDG_DATA_HOME/macrograph-local (dev:inline keeps its own) | 3171 (Vite's port for dev:inline) |
| other | service-<channel>.json | $XDG_DATA_HOME/macrograph-<channel> | from the name, 20000-29999 |
pnpm dev (or dev:inline) serves the local channel, so an agent can work against the
development server:
MACROGRAPH_CHANNEL=local macrograph mcp
macrograph mcp config --channel local # client configuration with that environmentOnly pnpm dev starts a local service: MACROGRAPH_CHANNEL=local macrograph mcp fails straight
away when none is running, so the built CLI never runs source on its own. A service pnpm dev
started registers the command that runs it, so service start, restart and setting changes on
the local channel restart it with that command; after service stop only pnpm dev starts it
again. Vite proxies to the port the service had when pnpm dev started, so restart pnpm dev
after changing it.
Configuration
First-time setup
On an unconfigured server, startup prints MACROGRAPH_SETUP_KEY <key> and a setup link that
already includes the key (based on MACROGRAPH_PUBLIC_ORIGIN). Open the link, or open the server
in your browser and enter the key, then approve the code it shows at macrograph.app/device. For Docker, find the key with docker logs macrograph.
The server registers itself with macrograph.app as an OAuth client the first time it is used, and
everyone signs in to it with the same device code flow (see apps/cloud/OAUTH.md). The client
registration and the owner's grant are stored in MACROGRAPH_DATA_DIR/macrograph-auth.json.
The approving account becomes the server administrator and supplies its cloud-managed credentials.
The browser is signed in automatically and remembers its session. Other visitors cannot claim the
server without the key, and signing in as another account does not grant edit access unless its
user ID is included in MACROGRAPH_ADMIN_IDS.
The key is invalidated after setup and changes if an unconfigured server restarts. Treat logs as
sensitive, and use HTTPS when accessing the server remotely. Ownership is persisted separately in
MACROGRAPH_DATA_DIR/macrograph-owner.json, with the same file protections as authorization data.
When the owner's connection ends (for example after removing the server under Account → Apps on
macrograph.app), setup does not reopen: the owner signs in and reconnects from Settings, and
only their account is accepted. Servers set up before the OAuth flow keep their owner but must
reconnect. Keep the owner file along with the authorization files when backing up or moving the
server.
REST API and MCP
The server exposes the same project editing REST API as MacroGraph Cloud under /api (the
OpenAPI document is at /api/openapi.json) and an MCP server at /api/mcp. The server's single
project has the ID local. Both authenticate with Authorization: Bearer <api key>.
The owner and admins create and revoke API keys under Settings → API keys, which also shows
the endpoints and an MCP client configuration snippet. A key is shown once, acts as the person who
created it, and can only edit while that person is the owner or an admin. Edits appear live in
connected editors. Keys are stored as SHA-256 hashes in MACROGRAPH_DATA_DIR/macrograph-api-keys.json,
with the same file protections as the authorization files.
Modules
Every module, including the built-ins, is loaded at runtime from a folder. Built-ins ship in
dist/modules next to the server bundle. Modules you add go in
MACROGRAPH_DATA_DIR/modules/local/<publisher>/<name>/ and load as local/<publisher>/<name>;
installs from registries go under MACROGRAPH_DATA_DIR/modules/installed/. Admins manage modules
under Settings → Modules: reload, enable or disable, rescan, browse registries and install. Changing
a local module's files marks it stale; nothing reloads until an admin reloads it. The dev server
(pnpm dev) instead reloads a changed local module as soon as its folder is complete again, so
macrograph-module dev --watch rebuilds take effect without a manual reload.
Each module is served under /module/<namespace>/<publisher>/<name>/: rpc (editors only), and
content-hashed ui/ and assets/ files.
Environment
PORTandHOST: production listener, default3001and0.0.0.0.MACROGRAPH_DATA_DIR: persistent SQLite directory, default current directory; mount/datain the image.MACROGRAPH_ISSUER: the MacroGraph OAuth issuer, defaulthttps://macrograph.app.MACROGRAPH_SERVER_NAME: the name users see when approving this server, defaultMacroGraph server on <hostname>.MACROGRAPH_ADMIN_IDS: optional comma-separated MacroGraph user IDs that may edit this server's project in addition to the account that registered the server.MACROGRAPH_BASE_PATH: public path such as/macrograph. Set it at both image build and runtime.MACROGRAPH_PUBLIC_ORIGIN: externally visible HTTP(S) origin, without a path, used by the browser security policy. Set this when running behind a reverse proxy.MACROGRAPH_REGISTRY_URL: official module registry, defaulthttps://registry.macrograph.app.MACROGRAPH_REGISTRY_KEYS: the official registry's Ed25519 public keys, comma-separated base64, each optionally prefixed with a key id (<id>:<base64>). Without keys, installing from the official registry is refused because releases can't be verified.MACROGRAPH_MODULES_DIR: built-in modules folder, defaultdist/modulesnext to the bundle.OTEL_EXPORTER_OTLP_ENDPOINT: optional server OTLP/HTTP collector base URL, such ashttp://localhost:4318. The server appends/v1/traces; existing URLs ending in/v1/tracescontinue to work.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: full trace URL, used as-is instead of the general endpoint.OTEL_EXPORTER_OTLP_HEADERS: optional comma-separatedname=valuecollector headers. Values may be percent-encoded, e.g.Authorization=Bearer%20token.OTEL_EXPORTER_OTLP_TRACES_HEADERS: trace-specific headers, replacing the general headers.OTEL_SERVICE_NAME: server trace service name, defaultmacrograph-server.AXIOM_TOKEN: optional Axiom API token with ingest permission. When set, traces are also sent to Axiom, alongside any OTLP collector.AXIOM_DATASET: Axiom traces dataset, defaultmacrograph-traces.AXIOM_OTLP_ENDPOINT: Axiom base URL for edge deployments, defaulthttps://api.axiom.co.VITE_OTEL_EXPORTER_OTLP_ENDPOINT: optional browser OTLP/HTTP trace endpoint, set at build time.MACROGRAPH_BROWSER_OTLP_ENDPOINT: browser collector URL used by the runtime CSP; the image sets this from the browser build argument.
Browser tracing is disabled unless its endpoint is configured. Cross-origin collectors must allow
the application origin, POST, and the Content-Type request header. The server CSP adds only the
configured collector and public WebSocket origins to connect-src; reverse proxies that replace
CSP must preserve those sources. Do not put collector credentials in the browser endpoint URL.
Server Tracing
Server tracing is disabled unless a collector endpoint is configured. It exports existing Effect
spans, including HTTP/RPC and graph execution spans, using OTLP/HTTP JSON (application/json).
The collector must accept HTTP OTLP, not gRPC (typically port 4318, not 4317). Traces are
batched every second and pending spans are flushed during graceful shutdown.
Every engine event dispatched to the executor produces an Executor.handleEvent span, even when
no graph matches. Twitch chat messages are identified by macrograph.module.id=twitch and
macrograph.event.type=channel.chat.message attributes. Independent events start separate traces;
events with an existing parent retain it, even if the parent has ended. Twitch's WebSocket listener
detaches connection-setup context before processing notifications. The span includes project, module, and event-type
attributes, not the event payload, and contains any spans created during graph execution.
The shared executor adds Executor.matchEvent, Executor.executeEventNode, Executor.runNode,
and Executor.resolveInput spans. Graph, node, schema, and input details remain in attributes. Node
spans cover resource/property resolution, input evaluation (including pure nodes), execution-driver
work, and output validation. Each actual schema invocation gets a Schema.run <module>.<schema-id>
span, such as Schema.run util.Print, with module/schema/node identity also in attributes. It wraps
the callback and its returned Effect; module-created sub-operation spans nest beneath it. Cache
hits that skip the callback do not produce a schema-run span. Input spans
identify connections or default sources without recording input/output values. These boundaries
apply to every runtime using the shared executor, not just Cloudflare workflows.
Effect logs emitted inside a span, such as Print node output, are recorded as span events on that
span (for example Schema.run util.Print). No separate OTLP log export is used. Each trace payload
is serialized once and sent to every destination, so trace and span IDs match across the collector
and Axiom. Each destination receives only its own headers.
For example, set these in apps/server/.env for development or production startup:
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_SERVICE_NAME=macrograph-server
# Optional for authenticated collectors:
# OTEL_EXPORTER_OTLP_HEADERS=Authorization=Bearer%20your-token
# Also send traces to Axiom:
# AXIOM_TOKEN=xaat-...For Docker, pass these as container environment variables and use a collector address reachable
from inside the container, such as http://otel-collector:4318 on a shared Docker network.
Collector headers stay server-side and are not sent to the browser. Traces can contain request
metadata, errors, and execution attributes; send them only to a trusted collector.
Container
The Server image GitHub Actions workflow publishes
ghcr.io/brendonovich/macrograph-server:beta on pushes to main (or a manual run on main).
It includes linux/amd64 and linux/arm64 images and publishes only the beta tag.
Pull requests build both architectures without publishing.
The old server's main, latest, and release tags are left untouched.
Because this reuses the old server's GHCR package, grant this repository write access under
the package's Settings > Manage Actions access before the first publish. The workflow
authenticates with GITHUB_TOKEN; no registry secret is needed.
docker run -d --name macrograph --restart unless-stopped \
-p 3001:3001 -v macrograph-beta-data:/data \
ghcr.io/brendonovich/macrograph-server:betaOpen http://localhost:3001. Use a separate data volume from the old server; this image is not
an in-place migration of its persisted data. To update, pull the image and recreate the container
with the same volume.
To build locally, run from the repository root:
docker build -f apps/server/Dockerfile -t macrograph:beta .
docker run --rm -p 3001:3001 -v macrograph-beta-data:/data macrograph:betaThe image runs as UID/GID 1000:1000. Ensure a bind-mounted data directory is writable by that
identity; named volumes receive the image's /data ownership automatically.
MacroGraph authorization is stored separately from project data in
MACROGRAPH_DATA_DIR/macrograph-auth.json, which is atomically replaced with mode 0600 in a
mode 0700 directory. Opaque signed-in browser sessions are stored with the same protections in
MACROGRAPH_DATA_DIR/macrograph-client-auth.json. Anonymous browsers have read-only editor access;
only the signed-in server owner and configured admins may modify the project. Module configuration,
including OBS passwords, is stored in the ordinary project SQLite engine storage.
For a path prefix, build with --build-arg MACROGRAPH_BASE_PATH=/macrograph and run with the same
MACROGRAPH_BASE_PATH. Forward WebSocket upgrades for rpc-ws, preserve the public prefix, and
set MACROGRAPH_PUBLIC_ORIGIN to the external scheme and host. Do not rely on forwarded headers;
the server intentionally uses the explicit origin when generating public URLs.
Releases
@macrograph/cli is published from this package. Pushes to main run
.github/workflows/publish-cli.yml, which builds the dev channel and publishes
0.0.0-dev-<run> under the npm dev dist-tag. Install it with
npm install --global @macrograph/cli@dev. pnpm --filter @macrograph/cli run pack:npm
writes the dependency-free package to apps/server/dist/npm, with esm, client and
modules at its root.
The workflow publishes with npm trusted publishing over OIDC, so it stores no token. The package
must exist before a trusted publisher can be configured. For the first publish, create the
@macrograph npm organization and run:
node apps/server/scripts/set-version.mjs 0.0.0-dev-0
MACROGRAPH_CHANNEL=dev pnpm --filter @macrograph/cli build
pnpm --filter @macrograph/cli run pack:npm
cd apps/server/dist/npm && npm publish --tag dev --access publicThen add this repository with workflow publish-cli.yml as a trusted publisher in the package's
npm settings. set-version.mjs updates both package.json and the bundled Version.ts.
