Turn Internal APIs into MCP Tools with Least Privilege
An internal API may offer hundreds of actions. An agent needs only a few. If you wrap the whole API as one tool, a small task can gain access to production writes, private records, and admin routes it never asked for. St
An internal API may offer hundreds of actions. An agent needs only a few. If you wrap the whole API as one tool, a small task can gain access to production writes, private records, and admin routes it never asked for. Start with one read-only operation. Make the allowed path easy to inspect, and make every other path fail closed.
Pick one job and one read-only operation
Suppose a support agent needs to read a ticket summary before it drafts a reply. The job does not need to edit tickets, send messages, or list every customer record. Define one narrow tool that accepts a ticket ID and returns only the fields needed for the reply. Name the owner of that operation and the environment it may reach.
This is an example design, not a claim that Axiom ships a ready-made ticket tool. The design starts from a single operation in your own OpenAPI description. The OpenAPI specification lets an operation declare security requirements and scopes. Review that contract with the API owner before an MCP server exposes it to agents.
MCP tools have names and input schemas that a model can discover. The model may choose to call a tool, but the tool server and gateway must decide whether the call is allowed. The MCP tool specification recommends a human denial path for sensitive work. A prompt that says βread onlyβ is not an access control.
A narrow tool contract
- One purpose: read a ticket summary for an approved support task.
- One input: a ticket ID with a strict type and length limit.
- One output: selected fields, with secrets and unrelated customer data removed.
- One identity: the user and agent run that requested the lookup.
- One scope: permission to read that ticket in the allowed environment.
Keep the tool name and description plain. Tell the model what the tool returns and when it may call it. Do not put a bearer token in the description, prompt, or arguments. The server should obtain credentials through trusted code.
Put policy between the agent and the API
The safe code flow is short: identify the caller, check the task and scope, validate the input, call the one API operation, filter the response, and write an audit event. If a check fails, stop before the API call. Record the denial without storing secrets.
Policy checks should stop a blocked call before it reaches the internal API.
Here is a small local policy example. It is not a product API or a complete authorization system. It shows the deny-by-default rule that should sit outside the model.
# policy.py β illustrative local decision helper
def may_read_ticket(scopes: set[str], environment: str) -> bool:
return environment == "test" and "tickets:read" in scopes
assert may_read_ticket({"tickets:read"}, "test")
assert not may_read_ticket({"tickets:read"}, "production")
assert not may_read_ticket(set(), "test")
In a real service, verify token audience, expiry, issuer, user, and task as well. A string scope alone is not enough. The MCP authorization specification describes resource-server checks and scope handling. Keep authorization in trusted server or gateway code, not in a model instruction.
Where each check belongs
| Check | What it protects | Fail-closed result |
|---|---|---|
| Identity and scope | Who may use the tool | Deny before API call |
| Input validation | Which record may be requested | Reject bad or broad input |
| Rate limit | How much load an agent can add | Stop or slow excess calls |
| Output filter | What leaves the API | Remove fields outside the task |
| Audit event | What happened and why | Keep a safe decision record |
The model can still ask for a blocked action. That is normal. The policy layer must return a clear denial rather than silently trying another route. Axiom Studioβs MCP Gateway page describes a central control point with access rules, rate limits, traffic filters, and audit trails. The page does not replace your APIβs own authorization checks.
Make the input smaller than the API
An API may accept search syntax, filters, page sizes, and include flags. A tool does not need to expose all of them. If the task is βread one ticket,β accept one ticket ID. Set a maximum response size. Do not allow a wildcard or an empty ID to turn a lookup into a full export.
Validate both shape and meaning. A string may be valid JSON but still ask for a record outside the userβs project. Check the record owner after the API returns it, or use an API credential that is already scoped to the right project. Filter the response before it reaches the model. Error text should not echo a token or private payload.
Use timeouts and a bounded retry rule. A model can repeat a failed tool call many times if the server only says βtry again.β Return a useful error class: invalid input, permission denied, not found, rate limited, or upstream unavailable. Tell the agent when it should stop and hand off to a person.
Decide which actions need approval
The first tool should be read-only. Later, a team may add a write tool such as βadd a ticket comment.β That is a new risk, not a small update to the read tool. Give it a separate name, scope, policy, and test set. Require a person to approve the exact action and arguments where the impact calls for it.
Do not reuse one broad service token across all agents. Bind each run to a user, task, environment, and short lifetime. If your MCP client uses OAuth, follow the current MCP authorization flow and validate the token for the protected resource. A token that was issued for one server must not be accepted by another just because it looks valid.
Axiom Studioβs guide to MCP tool policy shows how agent, user, task, and environment can shape an allow or deny decision. Use the same facts in tests. The gateway can enforce a common rule, while the API remains responsible for its own record-level checks.
Test the path that should fail
A successful lookup is only one test. Try a missing scope, expired identity, wrong environment, overlong ID, request for another project, repeated calls over the limit, and a blocked write request. The expected result is no upstream action. Check the audit event for a clear reason, without raw secrets.
Use a safe test environment and a small set of known records. Count allowed calls, denied calls, API errors, and time spent in human review. Do not call a denied request safe until the upstream API confirms it was never reached. A dashboard that only shows the modelβs final answer can miss an unsafe tool attempt.
A short rollout sequence
- Publish one read-only tool in a test environment.
- Run allowed and denied-action tests with a named owner.
- Review logs, response fields, rate limits, and error messages.
- Grant production read scope to a small group with a revoke switch.
- Add any write tool later as a separate reviewed change.
The emergency revoke path matters. A team should be able to disable the tool, revoke the agent identity, and rotate a leaked credential without editing every agent prompt. Rehearse that path before rollout. Keep the policy version and server version in the audit record so a later reviewer knows what rules applied.
MCP can make useful API work available to an agent. The safe version is deliberately narrow. Start with one operation, prove that blocked calls stop before the API, and widen access only when the team has evidence it needs the next action. For a broader view of how agents chain approved tools, see Axiom Studioβs guide to MCP tools in development work.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.