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.peaHost 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
- Explicit allow-list.
allowed(name)compares against three literal names and returnsfalseotherwise. Bytecode-compatible control flow avoidsin,??and interpolation, which the interpreter supports but the compiler does not currently emit. - Structured denial. A blocked tool returns
error("ValueError", "tool blocked", "E_TOOL", null, {"tool": name}). The agent harness readscodeandcontext.toolwithout parsing prose. - Async boundary.
async functionandawaitare real v3.1.36 syntax, but top-levelawaitis not compiled. Exportingrun_stepand calling it throughAsyncVMModule.call_asyncis the supported pattern. - Host capabilities. The Python host registers
sleepanderrorwithregister_async_function. This keeps timers and error construction outside the policy script and makes the permission boundary visible in code review. - Future deployment. The same source can be compiled and wrapped as a PEAWASM artifact with
pea wasm-buildfor browser-side experiments; keep the allow-list rule in the portable layer and provider calls in the host. - Verification. The host prints
Truefor the allowed search tool andE_TOOLfor the blocked shell tool, proving both async branches execute.