PeaLang / Code Library / Blueprint
Production Blueprint ยท v3.1.36

AI agent tool gateway

Domain: AI Platform / Agent SafetyAsync VMPythonWASM
Verified example. The code below was checked against the local PeaLang v3.1.36 parser, interpreter or async VM. It uses only syntax and built-ins present in that source tree.

Problem statement

An LLM agent can request arbitrary tools. The gateway must allow only an explicit set, record the tool name in a structured error when it is blocked, and remain usable from a cooperative async runtime without embedding provider credentials in the rule layer.

Complete code

function allowed(name) {
  if name == "search" { return true }
  if name == "summarize" { return true }
  if name == "calculator" { return true }
  return false
}

async function call_tool(name, args) {
  await sleep(0.01)
  if allowed(name) == false {
    return error("ValueError", "tool blocked", "E_TOOL", null, {"tool": name})
  }
  return {"tool": name, "args": args, "ok": true}
}

async function run_step(step) {
  let tool = step["tool"]
  let args = step["args"]
  let result = await call_tool(tool, args)
  return {"step": step["name"], "result": result}
}

export run_step
Save as ai-agent-tool-gateway.pea

Host driver (ai_agent_host.py)

The exported async function is driven by the Python async VM. sleep and error are host capabilities registered explicitly; nothing is implicit.

import asyncio
from pealang.async_vm import AsyncVMRuntime
from pealang.error_values import build_error

source = open("ai-agent-tool-gateway.pea", encoding="utf-8").read()
runtime = AsyncVMRuntime()

async def sleep(seconds):
    await asyncio.sleep(seconds)

runtime.register_async_function("sleep", sleep)
runtime.register_async_function("error", build_error)

async def main():
    module = await runtime.load_async(source, "ai-agent-tool-gateway.pea")
    ok = await module.call_async(
        "run_step", {"name": "lookup", "tool": "search", "args": {"q": "PeaLang"}}
    )
    blocked = await module.call_async(
        "run_step", {"name": "unsafe", "tool": "shell", "args": {}}
    )
    print(ok["result"]["ok"])
    print(blocked["result"]["code"])

asyncio.run(main())

Run & verify

pea fmt --check ai-agent-tool-gateway.pea
pea check ai-agent-tool-gateway.pea
python ai_agent_host.py
Always run the formatter and static checker before execution: pea fmt --check ai-agent-tool-gateway.pea && pea check ai-agent-tool-gateway.pea, then pea run ai-agent-tool-gateway.pea (or the host command shown above for async modules).

Why & How

  1. Explicit allow-list. allowed(name) compares against three literal names and returns false otherwise. Bytecode-compatible control flow avoids in, ?? and interpolation, which the interpreter supports but the compiler does not currently emit.
  2. Structured denial. A blocked tool returns error("ValueError", "tool blocked", "E_TOOL", null, {"tool": name}). The agent harness reads code and context.tool without parsing prose.
  3. Async boundary. async function and await are real v3.1.36 syntax, but top-level await is not compiled. Exporting run_step and calling it through AsyncVMModule.call_async is the supported pattern.
  4. Host capabilities. The Python host registers sleep and error with register_async_function. This keeps timers and error construction outside the policy script and makes the permission boundary visible in code review.
  5. Future deployment. The same source can be compiled and wrapped as a PEAWASM artifact with pea wasm-build for browser-side experiments; keep the allow-list rule in the portable layer and provider calls in the host.
  6. Verification. The host prints True for the allowed search tool and E_TOOL for the blocked shell tool, proving both async branches execute.