Skip to main content

Securing MCP tools: identity, rate limits and logs for AI agents

Put an API key in front of the tools an agent calls, give each agent a budget, mask what the calls carry, and see in the audit log who changed the tool list.

By Akif F. Güven Security

What you will have at the end

The orders-mcp Route from OpenAPI to MCP with the rules an agent-facing API needs: every tool call carries the identity of an agent, each agent has a budget of calls per minute, the arguments the calls carry are masked in the request records, and the audit log shows who changed the list of tools and when. Everything below was done on the real Route and measured with an MCP client.

Prerequisites

  • A Route that serves MCP tools: orders-mcp on /mcp/orders with openapi-to-mcp reading http://orders-svc:8080/openapi.json.
  • A Consumer per agent. The article uses orders_assistant with a key-auth credential.
  • Traffic logging turned on under Traffic Explorer › Settings.

Identity: a Consumer per agent

An agent is a client like any other, so it signs in like one. key-auth on the Route turns every tool call into a request by a Consumer; the agent sends its key in the apikey header and the record of each call names the Consumer and the credential that admitted it. A call without a key is refused with 401 and Missing API key in request, a call with a wrong one with 401 and Invalid API key in request, both before any tool runs.

The same Route takes openid-connect instead of key-auth when agents act for a signed-in person: Keycloak and G8 walks through that Plugin on a Route. Whatever the Plugin, the engine runs it before openapi-to-mcp, so the tool conversion only ever sees an authenticated request.

Rate limits: a budget per agent

limit-count on the Route counts tool calls, and with the Key consumer_name it counts them per agent. The Route carried a budget of 100 calls per 60 seconds; for the measurement it was lowered to 10 and Rejected code set to 429, and that save is a Route update in the audit log like any other.

The audit record of the Route update that lowered the limit-count budget on orders-mcp: count 100 before, count 10 and rejected_code 429 after, side by side One budget per Consumer, and the change to it on record. A second agent with its own key gets its own 10, not a share of this one.

Twelve tools/call requests from the agent within a minute:

200 200 200 200 200 200 200 200 200 200 429 429
X-RateLimit-Limit: 10   X-RateLimit-Remaining: 0   X-RateLimit-Reset: 59

The MCP client sees an HTTP 429 and the headers that tell it when the window opens again. Traffic Explorer shows the refused calls with the Consumer’s name and gateway as the node, because the tool never ran.

Traffic Explorer filtered by /mcp/orders: 200 records followed by 429 records, each with the Route orders-mcp and the Consumer orders_assistant The budget ran out mid-second. The refusals carry the agent’s name like the calls that went through.

The Plugin’s default refusal is 503; set 429 so that the agent’s client library treats the refusal as a limit and not as an outage. A limit-count on a Route with several nodes needs the Redis policy: Rate limiting per Consumer with limit-count.

Logs: what a tool call leaves behind

Every MCP message is a request record. By default a record carries the headers, the sizes and the timing, with the apikey header masked by the gateway before the record is stored. The arguments of a tool call are in the request body, which is not stored unless you turn it on.

Traffic Explorer › Settings › Capture turns request bodies on, with a size limit. The page says what that means: bodies can carry card numbers and passwords, viewing one needs its own permission, and every view is written to the audit log.

The Capture section of Traffic Explorer settings: Request body on with an 8 KB limit, Response body off, and the warning that viewing a body is audited Bodies are off by default. Turn them on for the Routes that need them, and know that reading one leaves a trace.

Under Masking › Body fields, a JSON path names the values the gateway replaces before the record is stored. For createOrder the customer id sits at $.params.arguments.requestBody.customer.

The Masking section of Traffic Explorer settings: the header masks of this Environment, the default query masks, and the body field $.params.arguments.requestBody.customer The path follows the MCP message: params, arguments, then the tool’s own input. The first attempt without requestBody in the path masked nothing, so check a record after saving.

A createOrder call after the save, opened in Traffic Explorer with Show body:

The Request tab of a tools/call record: the stored body with the customer field replaced by asterisks and the order lines in the clear The value was replaced on the gateway. The log store never held the customer id.

Opening the body is itself an audit event, as the Capture section warns.

Audit: who changed the tool list

The tools an agent sees are the operations of the document the Plugin reads. Changing that document, or the address it is read from, changes the tool list. In this session the Plugin’s OpenAPI document URL was switched from openapi.json to openapi-v2.json, a version of the document with one more operation. The next tools/list from the agent returned four tools instead of three: listOrders, createOrder, getOrder and the new cancelOrder.

The audit log has the change as a Route update with the two URLs side by side, the actor and the time.

The audit record of the Route update that changed openapi_url from openapi.json to openapi-v2.json, before and after side by side One line changed, and an agent gained a tool. The record says who did it and when.

What the audit log does not see is a change to the document behind an unchanged URL. Treat the document as part of the agent’s permissions: serve it from a place with its own change control, and review it before you point the Plugin at a new version.

What to do next

  • Give each agent its own Consumer and key, so budgets and records stay apart.
  • Keep request bodies off on Routes that do not need them, and mask the fields that identify people on the ones that do.
  • Put the agent’s identity on the company’s identity provider: Keycloak and G8. How G8 serves tools is on AI Gateway › OpenAPI to MCP, and the design for agents on AI agents and MCP.
Written by
Akif F. Güven
Engineer, G8
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.