AI agent security: protecting tool-calling endpoints from abuse (2026)

AI agents that call tools add three doors to your app: an open endpoint, prompt injection that triggers a tool call, and keys in config files. How to check each.

By Theo · Maker of Tokenwise
LLM Security

Key takeaways

  • A tool-calling endpoint hands out actions. Whoever reaches it gets a model on your bill with your tools attached.
  • The MCP specification makes authorization optional. A remote MCP server with no token check lists and runs its tools for anyone who finds the address.
  • OWASP says it is unclear whether prompt injection can be fully prevented. Limit what a hijacked agent can reach: smallest tool rights, permissions enforced in code, human approval for anything that cannot be undone.
  • GitGuardian counted 24,008 unique secrets in MCP-related configuration files, 2,117 of them valid. Keep agent credentials in server-side secrets, with one key per agent.
  • Abuse shows up as spend. Per-request logs, cost spike alerts, and hard budget caps catch what testing missed.

An AI agent is a model that can act. It calls functions, queries your database, sends email, runs code. Every one of those tools sits behind an endpoint, and every endpoint is a place where a stranger can try to make the agent work for them. When that happens you pay twice: in tokens, and in whatever the tool did.

This guide covers the three ways tool-calling endpoints get abused, with a check and a fix for each: an endpoint that answers anyone, a prompt injection that ends in a tool call, and API keys left in agent config files.

Why tool calling changes the attack surface

A chat endpoint returns text. An agent endpoint returns actions. With function calling, the model picks a tool and writes its arguments. With the Model Context Protocol (MCP), a server publishes a list of tools that a client discovers with tools/list and runs with tools/call.

That gives you three things to protect: the endpoint that accepts tasks, the content the agent reads, and the credentials it holds. Whoever reaches the endpoint gets a model on your bill with your tools attached.

OWASP published its Top 10 for Agentic Applications on December 9, 2025. Its first three entries are Agent Goal Hijack (ASI01), Tool Misuse (ASI02), and Identity and Privilege Abuse (ASI03).

Risk 1: the endpoint answers anyone

The MCP specification says it in one line: "Authorization is OPTIONAL for MCP implementations." A remote MCP server with no token check lists its tools and runs them for anyone who finds the address. A custom /api/agent or /api/chat route has the same problem when the page sits behind a login and the route does not.

The specification does set duties. Its tools page says servers must validate all tool inputs, implement proper access controls, rate limit tool invocations, and sanitize tool outputs. That work is yours.

What is exposedWhat a stranger getsWhat it costs you
The tool listEvery tool name, description, and argumentA map of your internal operations
Read toolsWhatever the tool returns: customers, orders, filesA data leak
Write toolsCreate, update, delete, send, payActions in your system by someone who is not a user
The model behind the routeInference with your tools attachedTokens billed to your provider account

The token bill usually arrives first. OWASP files it under Unbounded Consumption (LLM10:2025). One of its scenarios is Denial of Wallet: an attacker sends a high volume of requests and the pay-per-use bill lands on the owner. Agents raise the stakes, because steps and retries multiply the cost of a single run.

Check it. From a machine with no credentials, send your own agent route or MCP server a request with no cookie and no Authorization header. A 401 is what you want. A model answer or a list of tools means the door is open. Keybrum's MCP security guide has the two curl requests to run against your own server.

You can also have it tested from outside. Keybrum is an automated pentest for web apps that runs 338 attacks against your live app the way a stranger would. It looks for an MCP server next to your app, starts a session with no credentials, lists the tools, and calls each one once with placeholder values. It reports any tool that returns application data or accepts a call from a stranger, with the request that worked. It also checks whether your AI routes answer a caller who is not signed in.

Fix it.

  • Check a token or a session before any method runs, including tools/list.
  • Scope every tool call to the caller's own data, as your REST API does. A tool that only checks that someone is signed in gives every user everyone's records.
  • Rate limit per caller, and cap the tokens and steps of a single task.

Risk 2: prompt injection that ends in a tool call

OWASP puts prompt injection first in its list for LLM applications and writes that "it is unclear if there are fool-proof methods of prevention for prompt injection." In a chatbot, a successful injection produces bad text. In an agent, it produces a tool call made with your permissions.

The injection does not have to come from the user. Indirect prompt injection arrives through content the model reads: a web page, a PDF, a support ticket, the result of another tool. A public example is CVE-2025-32711 in Microsoft 365 Copilot. Microsoft's record describes an AI command injection that "allows an unauthorized attacker to disclose information over a network" and scores it 9.3 out of 10.

For agents, OWASP's related entry is Excessive Agency (LLM06:2025), with three root causes: excessive functionality, excessive permissions, and excessive autonomy. You cannot count on the model to refuse, so you limit what a hijacked model can reach.

  • Separate reading from acting. The agent that reads untrusted content should hold no tool that can send, delete, or pay.
  • Give each tool the smallest rights that work. A tool that answers questions gets a read-only database role.
  • Enforce permissions in code. Each tool checks the caller's rights itself. A sentence in the system prompt does not count as access control.
  • Put a human on what cannot be undone. The MCP specification says there "SHOULD always be a human in the loop with the ability to deny tool invocations." Apply that to deletes, payments, outbound email, and deploys.
  • Treat arguments as untrusted input. Tool arguments written by a model never reach a shell or a raw SQL query without validation.

An outside test covers part of this. Keybrum sends each AI route two more prompts, one asking the model to ignore its instructions and one asking it to quote its system prompt, and reports what came back. It does not test injection through the pages, emails, or tool results your agent reads, and it does not review your agent's tool permissions. That part is a design review, and the list above is where I'd start.

Risk 3: API keys in agent configs

Agents need credentials to use their tools, and those credentials end up in files: mcp.json, .env, a config block in the repo. GitGuardian's State of Secrets Sprawl 2026 counted 24,008 unique secrets exposed in MCP-related configuration files, 2,117 of them valid credentials. The same report counted 1,275,105 leaked AI service secrets, up 81% in a year.

OWASP's MCP Top 10, still in beta, opens with this risk: Token Mismanagement and Secret Exposure (MCP01). A leaked provider key does not need your agent at all. It works from any script, on your account.

  • Keep keys out of anything that ships to the browser, including variables prefixed NEXT_PUBLIC_ or VITE_.
  • Keep keys out of prompts and tool descriptions. Assume a user can get the model to repeat anything it can see.
  • Give each agent its own key with the narrowest scope, so you can revoke it alone.
  • Never commit mcp.json or .env with live tokens. Load them from the environment or a secret store.

Keybrum's free check covers the browser side. Paste your app's address and in about ten seconds it shows the backend your app talks to, the keys its JavaScript exposes, and the tables it names. It needs no account and sends nothing to your backend. For the full walk-through on keys, read how to detect leaked LLM API keys and how to secure LLM API keys in production.

Abuse shows up as spend

Every path above ends in tokens. An open endpoint, a hijacked loop, and a stolen key look the same on the invoice: a spike you cannot explain. That makes spend one of the earliest signals you have.

  • Log every model call with its route, model, token counts, and key.
  • Alert on cost spikes. Tokenwise compares spend with a 7-day baseline and emails you when it jumps.
  • Set hard caps. Tokenwise Pro enforces daily and monthly budget caps at the proxy, so requests stop when the cap is reached. A stolen provider key bypasses your proxy, so set a spend limit with the provider too.

A pentest tells you which doors are open today: Keybrum's one-time report costs €49 for one app, and its plans start at €29 a month and attack again on every deploy. Spend monitoring tells you when someone found a door you missed. The full bill of a breach on AI infrastructure is the reason to run both.

Try this week

  • Call your agent route and your MCP server from outside with no credentials. Anything other than a 401 or 403 goes on the fix list.
  • List every tool your agent can call and mark the ones that write, send, delete, or pay. Each needs a caller check in code and a human approval.
  • Search your repo and your built files for provider keys. Move any you find to server-side secrets and rotate them.
  • Set a spend limit at each provider and a cost spike alert on your LLM traffic. The token cost calculator helps you size the limit.
  • Run a web app security scanner such as Keybrum on your live app before the next launch, and retest after each fix.

Frequently asked questions

What is AI agent security?
AI agent security is the work of controlling who can send tasks to an agent, what content can steer it, and what its tools are allowed to do. An agent acts with the access you gave it, so the limits on its tools and credentials matter more than the model.
Does an MCP server need authentication?
Any MCP server reachable over HTTP should require a token unless everything it offers is public by design. The MCP specification makes authorization optional, so a server answers anyone until you add the check. A local server started over stdio reads its credentials from the environment instead.
Can prompt injection be fully prevented in an AI agent?
Not today. OWASP writes that it is unclear whether fool-proof prevention methods exist. Plan for an injection that succeeds: limit the tools of any agent that reads untrusted content and keep irreversible actions behind a human approval.
How do I know if someone is abusing my agent endpoint?
Look at spend and traffic first. Calls at hours your users sleep, a model your app does not use, or a cost spike with no matching sign-ups all point to abuse. Per-request logs and cost spike alerts let you see it the same day.
What does Keybrum test on AI agents?
Keybrum tests what is exposed on your own app: an MCP server that lists or runs tools without credentials, AI routes that answer a signed-out caller, and provider keys in your JavaScript. It does not test injection through content your agent reads, and it does not review the agent's tool permissions. The free check at keybrum.com/check shows what a browser already sees.

More guides

Add this to your app in one line

Point your OpenAI baseURL at Tokenwise and every call is logged, priced, and optimizable — no SDK rewrite, no LangChain required.