Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Writing a Tool Extension

This page is written for an AI coding agent to follow, step by step. A human can hand it over with one line:

Build an agentstacks extension for <tool>. Follow https://raw.githubusercontent.com/airsstack/agentstacks/main/book/src/ch06-00-authoring.md

Everything here is api = 1, which is frozen for every 1.x release: what stays and what a 1.x release may add is on the stability page. If your extension uses something a later 1.x release added, declare the oldest agentstacks it works with in [requires] (step 2).

Read first

Fetch these before writing code. They are the contract; this page only tells you the order of work.

DocumentWhat it gives you
ch06-01-extensions.mdthe api = 1 Lua surface: manifest, [requires], events, operations, coverage, validation, every refusal
ch06-02-hooks.mdthe hook command line, the shared runtime, the neutral event and result, the translator contract
adapter.luathe official Claude Code extension, a complete worked example
demo packa small pack (one skill, one agent, one lua hook, one message hook) to test with

What you build

<tool>/
  extension.toml      manifest: name, version, api = 1, entry, reads, [requires]
  adapter.lua         the only file that calls agentstacks.*; answers `values` and `plan`
  lib/<rule>.lua      pure rules: no agentstacks.*, no require; plain data in, data out
  lib/<rule>_test.lua one test file per rule module, run with `airsl test`

Rules you must not break

  • Propose only. The extension has no file, network or process access. It calls agentstacks.write, copy, skip and require_binary; the core validates, shows and applies.
  • Cover every item. Each skill file, agent, hook, native file and native hook in the plan payload gets a write, a copy or a skip. A missing one refuses the whole plan.
  • Unsupported is visible. What the tool cannot express becomes agentstacks.skip with a reason a user can act on. Never drop an item silently.
  • No grants. Ask for nothing. A shared config file the user also edits is declared in extension.toml reads, read with agentstacks.read, edited with agentstacks.json.edit and written whole. There is no merge operation.
  • Lua only, under airsl. If the tool demands another hook language, write a thin shim that only runs the hook command and forwards the payload and result (hooks.md section 9).

Steps

1. Learn the tool

Write down, with a source for each, where the tool keeps project skills, agents (subagents), commands, rules and hook config; the file format of each; its hook events and payload fields; and how a hook blocks or adds context. List what it cannot express: those become skips.

2. Write extension.toml

[extension]
name        = "mytool"
version     = "0.1.0"
api         = 1
description = "Renders packs into MyTool project config"
entry       = "adapter.lua"
# reads     = [".mytool/settings.json"]   # only for a config file shared with the user

# Only when the extension uses something a later 1.x release added:
# [requires]
# agentstacks = ">=1.2"

api = 1 is required. A [requires] agentstacks range that excludes the running agentstacks is the only refusal returned, so an older agentstacks says to install a newer one instead of failing on a key it does not know (extensions.md section 2).

3. Start from a working adapter

This adapter installs skills and agents, and skips every hook. It was run with airsl 0.1.5 and agentstacks 1.0.0: airsl test mytool gave 1 passed, 0 failed (1 files), and agentstacks ext test ./ext/mytool applied five files in a temporary project, skipped eight items and ended in passed.

-- lib/paths.lua: pure rules; no agentstacks.* here.
local M = {}

M.ROOT = ".mytool/agentstacks"

function M.skill_dir(name)
  return ".mytool/skills/" .. name
end

function M.agent_file(name)
  return ".mytool/agents/" .. name .. ".md"
end

return M
-- adapter.lua: the only file that calls agentstacks.*; the rules live in lib/.
local paths = agentstacks.require("lib.paths")

local NO_HOOKS = "mytool has no hooks"

agentstacks.on("values", function(q)
  local out = { root = paths.ROOT, skills = {}, agents = {}, vars = {} }
  for _, p in ipairs(q.packs) do
    for _, s in ipairs(p.skills or {}) do
      out.skills[p.name .. "/" .. s] = {
        invoke = "/" .. p.name .. "-" .. s,
        dir = paths.skill_dir(p.name .. "-" .. s),
      }
    end
    for _, a in ipairs(p.agents or {}) do
      out.agents[p.name .. "/" .. a] = p.name .. "-" .. a
    end
  end
  return out
end)

agentstacks.on("plan", function(q)
  for _, p in ipairs(q.packs) do
    for _, skill in ipairs(p.skills or {}) do
      for _, f in ipairs(skill.files) do
        agentstacks.copy({ path = skill.dir .. "/" .. f.rel, file = f.id })
      end
    end
    for _, a in ipairs(p.agents or {}) do
      local file = paths.agent_file(p.name .. "-" .. a.name)
      agentstacks.write({ path = file, content = a.text, covers = { a.id } })
    end
    for _, h in ipairs(p.hooks or {}) do
      agentstacks.skip({ item = h.id, reason = NO_HOOKS })
    end
    local native = p.native or {}
    for _, f in ipairs(native.files or {}) do
      agentstacks.copy({ path = ".mytool/" .. f.rel, file = f.id })
    end
    for _, h in ipairs(native.hooks or {}) do
      agentstacks.skip({ item = h.id, reason = NO_HOOKS })
    end
  end
end)

Then replace each placeholder with the tool's real format:

ItemStartMake it real
skillcopy keeps the neutral frontmatterwrite SKILL.md with the tool's frontmatter built from skill.meta, covers = { <SKILL.md id> }; copy the other files
agentbody onlyprepend the tool's frontmatter built from a.meta; skip with a reason when a field cannot map
varsnonereturn a value for each name in q.vars ({{var}} tokens), for example $TMPDIR
hooksskippedstep 5

4. Put rules in lib/ and test them

Keep every decision (frontmatter fields, event mapping, quoting) in a lib/ module that takes plain values and returns plain values. The adapter passes data in; a module never calls agentstacks.*.

A test file sits beside its module, requires it by bare name and returns a table of test functions:

-- lib/paths_test.lua
local paths = require("paths")

return {
  skill_dir_is_under_mytool = function()
    assert(paths.skill_dir("demo-greet") == ".mytool/skills/demo-greet")
  end,
}

Run airsl test <tool>. It must report 0 failed. agentstacks ext test (step 6) runs the same tests when the extension holds a *_test.lua file.

5. Hooks

Skip hooks until skills and agents work. Then, per hooks.md:

  1. Command. Each lua hook arrives with command, an argv. Turn it into the tool's command string with the tool's own quoting, run from the project directory. Example: claude extension, cd "$CLAUDE_PROJECT_DIR" && "airsl" "run" ….
  2. Filter by the hook's match and agents in the tool's own config. The runtime does not filter.
  3. Message hooks (kind = "message") never reach the runtime: render them natively, for example as a command that prints the tool's context output.
  4. translate.lua. When any lua hook is covered by a write, write <root>/translate.lua. It returns M.event(payload) (the tool's payload to the neutral event, a list of neutral events when one tool call holds several, or nil) and M.result(result, payload) (the neutral result to the tool's output string, or nil).
  5. Blocking. A hook result { block = "<reason>" } from a hook declared may_block must become the tool's own deny output. Map { context = "<text>" } to the tool's context output.
  6. Unsupported events become skips with a reason.

6. Run it for real

Run the check. It builds a temporary project from your extension and a built-in conformance pack, runs your Lua tests, plans, applies and plans again, and writes nothing into your directory:

agentstacks ext test ./ext/mytool                      # the conformance pack only
agentstacks ext test ./ext/mytool --pack ./packs/demo  # and your own pack too

The last line must be passed, with second plan: nothing to change above it. failed or no verdict exits 1. The report and every refusal are in the command line.

Then, in a scratch directory holding a copy of your extension and a pack (the demo pack works), use it like a user would:

agentstacks init --tool ./ext/mytool --pack ./packs/demo --yes   # writes agentstacks.toml, applies
agentstacks plan                                                  # 0 to write
agentstacks doctor                                                # healthy

Then fire each generated hook command the way the tool would: its JSON payload on stdin, from the project directory. Check that context is added and a may_block hook really blocks. Finally, run the tool itself on the scratch project and confirm it loads the skills, agents and hooks.

7. Before you call it done

  • airsl test <tool> reports 0 failed.
  • agentstacks ext test ./ext/<tool> ends in passed.
  • agentstacks plan on the demo pack has no refusal; every item is written, copied or skipped.
  • Every skip reason tells the user what the tool cannot do.
  • agentstacks apply --yes, then agentstacks plan shows 0 to write; doctor says healthy.
  • A user's own entries in a shared config file survive apply, and removing the tool removes only your entries.
  • Each hook fires in the real tool, and a may_block hook blocks.

8. Share it

A user adds your extension to a project with agentstacks ext add (see the command line): a git URL with --tag or --rev and --subdir for the directory inside the repository, or a directory of the project.

Common refusals

You seeFix
mytool/adapter.lua: demo/hook/0 got no operation (copy it, write it, or skip it with a reason)cover that item with write, copy or skip
mytool/adapter.lua: lua hooks need .mytool/agentstacks/translate.lua (write it)write <root>/translate.lua (step 5)
mytool/adapter.lua:12: write: path ".x" is already proposed at adapter.lua:9two operations target one path; merge them into one write
mytool/adapter.lua:9: read: ".env" is not in extension.readsadd the path to reads in extension.toml
mytool/adapter.lua:1: require: invalid module name "lib/x" (use dots: lib.x)agentstacks.require("lib.x")
mytool/extension.toml: requires.agentstacks: ">=99" does not match agentstacks 1.0.0 (install a newer agentstacks)install a newer agentstacks, or lower the range to the oldest release your extension works with
mytool/extension.toml: extension.api: 0 is from agentstacks 0.x (this agentstacks speaks 1; set api = 1, or use a newer release of it)set api = 1 (and check the extension against the API)

The full refusal list is in extensions.md sections 9 to 12.