Securing tools and agents
The moment a model can call tools, your threat model changes from "it might say something wrong" to "it might do something wrong". Agents read untrusted content, plan multi-step actions and call APIs with real credentials. This doc covers the controls I put around every agent I ship: least-privilege identities, scoped tools, sandboxes, a policy layer the model cannot talk its way past, and confirmations that humans actually read.
Why it matters
Excessive Agency is LLM06 in the OWASP Top 10 for LLM Applications 2025, and the OWASP Top 10 for Agentic Applications for 2026 adds agent-specific failures such as tool misuse, identity and privilege abuse, unexpected code execution, memory poisoning and cascading failures across agents. The Model Context Protocol (MCP) has made it trivial to connect agents to dozens of tools, which is great for capability and a large new attack surface: every MCP server is code you run and text your model reads.
The core insight is simple. Assume the model will sometimes be wrong or manipulated (see prompt injection). Then design so that a wrong or manipulated model cannot do much damage.
How it works
Five controls, applied together:
- Agent identity. Each agent runs as its own principal with its own credentials, acting on behalf of a user with delegated, scoped permissions. Never share one powerful service account across agents.
- Least-privilege tools. Expose narrow tools (
create_draft_reply) instead of broad ones (send_any_email,run_sql). Validate every argument server-side. - Sandboxing. Code execution, browsing and file manipulation run in isolated environments (containers, microVMs) with no ambient credentials, restricted network egress and resource limits.
- Policy engine. A deterministic layer between the model and tools decides whether each call is allowed, needs approval, or is denied, based on user identity, tool risk, arguments and data provenance.
- Human confirmation. High-impact actions pause for explicit approval showing exactly what will happen.
Rendering diagram…
Reference architecture
For MCP specifically, the protocol's security best practices call out issues such as confused deputy problems, token passthrough and session hijacking. The pattern below puts a gateway between agents and MCP servers so you have one place to enforce allowlists, authentication and logging.
Rendering diagram…
Gateway responsibilities: only allow registered servers at pinned versions, scan tool descriptions for injected instructions when they change, mint per-user tokens with minimal scopes instead of forwarding the user's token, cap call rates, and give operators a kill switch per agent and per tool.
Code
A small policy engine in TypeScript. The model proposes calls; this code decides. Risk tiers and argument checks are data, so security reviewers can read them without reading prompts.
type Tier = "read" | "write" | "external" | "forbidden";
type ToolCall = { tool: string; args: Record<string, unknown> };
type Ctx = { userId: string; scopes: Set<string>; taintedByUntrusted: boolean };
const POLICY: Record<string, { tier: Tier; scope: string; check?: (a: any) => boolean }> = {
search_tickets: { tier: "read", scope: "tickets:read" },
draft_reply: { tier: "write", scope: "tickets:write" },
send_email: {
tier: "external",
scope: "email:send",
check: (a) => typeof a.to === "string" && a.to.endsWith("@ourcompany.com"),
},
delete_ticket: { tier: "forbidden", scope: "tickets:admin" },
};
export function decide(call: ToolCall, ctx: Ctx): "allow" | "confirm" | "deny" {
const rule = POLICY[call.tool];
if (!rule || rule.tier === "forbidden") return "deny";
if (!ctx.scopes.has(rule.scope)) return "deny";
if (rule.check && !rule.check(call.args)) return "deny";
// after reading untrusted content, anything beyond read needs a human
if (ctx.taintedByUntrusted && rule.tier !== "read") return "confirm";
return rule.tier === "external" ? "confirm" : "allow";
}
The taintedByUntrusted flag is a simple form of data-flow tracking: once the agent has read an email, web page or file from outside, its subsequent writes need approval.
Trade-offs and pitfalls
- Confirmation fatigue. If every step asks for approval, users click yes reflexively. Tier carefully and show diffs, recipients and amounts, not "Allow tool call?".
- Broad tools leak capability. A generic
http_requestorrun_shelltool turns any injection into full compromise. Wrap them in narrow, validated operations. - Ambient credentials in sandboxes. Cloud metadata endpoints, mounted secrets and permissive egress turn a sandbox into a launchpad. Strip them.
- Third-party MCP servers. They are supply chain dependencies with code execution and prompt influence. Review, pin and monitor them like any other dependency.
- Memory poisoning. Long-term agent memory can store injected instructions that trigger later. Treat memory writes as untrusted and make them inspectable.
- Multi-agent cascades. One compromised agent can instruct others. Authenticate inter-agent messages and apply the same policy checks to agent-to-agent calls.
Checklist
- Give each agent its own identity with delegated, user-scoped permissions.
- Replace broad tools with narrow, argument-validated ones.
- Run code and browsing in sandboxes with no ambient credentials and restricted egress.
- Put a deterministic policy engine between the model and every tool.
- Require specific human confirmation for irreversible or external actions.
- Pin and review MCP servers; never pass user tokens through.
- Provide per-agent and per-tool kill switches and full audit logs.