Skip to content
Ahmad Raza
Menu
All posts

5 min read

Audit Logs for Every MCP Call (Attribution Included)

How to design audit logs for every MCP call: agent id, tool, args, decision, and result, plus attribution so you know which agent drove which order.

Audit Logs for Every MCP Call (Attribution Included)
On this page

Audit logs for MCP calls are what turn an AI agent integration from a black box into something you can operate. When an agent adds items to a cart, prepares a refund, or searches your catalog, you want a record of who did it, what they asked for, what your server decided, and what happened. And when agents start driving real orders, you want to know which agent deserves the credit.

This post covers the fields I put in every MCP audit log entry, how to handle sensitive arguments, and how to attribute orders to the agents that produced them.

Why application logs are not enough

Most stores already have logs. They are usually written for debugging: stack traces, timings, the occasional "user updated cart." They rarely answer the questions you get after something goes wrong with an agent:

  • Which agent called this tool?
  • On behalf of which shopper or staff member?
  • With which arguments?
  • Was the call allowed, denied, or sent for approval?
  • What did the tool return?

An audit log is designed to answer those questions consistently, for every call, in a format you can query.

The core fields

Every MCP tool call produces one structured entry. I use JSON with a fixed schema:

{
  "ts": "2026-11-11T09:42:13.512Z",
  "request_id": "req_7f3a2c",
  "agent_id": "int_shopassist_01",
  "agent_version": "2.4.0",
  "principal": { "type": "shopper", "id": "cus_18820" },
  "session_id": "sess_a91e",
  "tool": "checkout_prepare",
  "args": { "cart_id": "cart_5521", "shipping_method": "standard" },
  "scopes": ["cart:write", "checkout:initiate"],
  "decision": "pending_approval",
  "decision_reason": "order_value_above_agent_cap",
  "result": { "status": "ok", "confirmation_id": "conf_3310" },
  "latency_ms": 184
}

What each piece gives you:

  • agent_id and agent_version. The integration that acted, and which build of it. Versions help when a new release starts behaving differently.
  • principal. The shopper, staff member, or system the agent acted for.
  • request_id and session_id. Tie together the calls in one conversation or flow.
  • tool and args. What was asked, after validation.
  • scopes. What the token allowed at that moment.
  • decision and decision_reason. Allowed, denied, rate limited, or pending approval, with a machine readable reason.
  • result. A summary of the outcome, not necessarily the full payload.

Log the decision, not just the call

The decision field is the one teams most often skip, and it is the most useful. Your server makes policy choices on every call: scope checks, argument validation, rate limits, caps, confirmation requirements. Recording the outcome and reason makes those policies observable.

With decisions logged you can answer questions like "how many refund prepares were denied this week for exceeding caps" or "which agent hits rate limits most often" without digging through code.

For confirm-then-execute flows, log both halves and link them with the confirmation id. The execute entry should also record who approved it and when.

Handle sensitive arguments carefully

Audit logs should be detailed, but not a second copy of your most sensitive data.

  • Never log payment details. Log a token reference or nothing.
  • Mask personal data such as full addresses, phone numbers, and emails. Keep enough to correlate, like a hashed value or last few characters.
  • Truncate free text. Search queries and messages can be logged in short form, with care for anything that might contain personal information.
  • Define retention. Keep audit logs as long as you need them for disputes and compliance, then expire them on schedule.
  • Restrict access. Audit logs are themselves sensitive. Limit who can read them and log that access too.

A simple approach is a per-tool redaction map that lists which argument fields are kept, masked, or dropped. Apply it in one place before writing the entry.

Make logs tamper resistant

An audit log that anyone can edit is not much of an audit log. Practical steps:

  • Write to an append-only store or a log service with immutability settings.
  • Ship logs off the application server quickly.
  • Keep write credentials separate from the app's normal database access.

You do not need anything exotic. The goal is that a bug or a compromised service cannot quietly rewrite history.

Attribution for agent-driven orders

Once agents are driving sales, you will want to know which ones. Attribution comes almost for free if your logs are structured well.

  1. Carry the agent id through the funnel. When cart_add or checkout_prepare runs, record the agent id on the cart.
  2. Stamp the order. When the order is created, copy the agent id, the session id, and the confirmation id onto the order record.
  3. Link back to the catalog calls. The session id ties the order to the earlier search_catalog and get_product calls, so you can see which recommendations led to a purchase.
  4. Report per agent. Orders, revenue, refunds, and cancellations grouped by agent id.

This answers practical business questions. Which integration brings real buyers? Which one generates refunds? Is a new agent version converting better or worse? Without attribution you are guessing.

Be clear about attribution rules when several touchpoints exist. A shopper might discover a product through one agent and buy through your site directly. Decide whether you credit first touch, last touch, or both, and keep the raw events so you can change your mind later.

Put logs to work

Logs are only valuable if someone looks at them. A few habits I recommend:

  • A weekly review of denied and pending decisions per agent
  • Alerts on unusual spikes in calls, errors, or cap rejections for one integration
  • A simple dashboard of agent-driven orders and refunds
  • A standard query for support: "show every call for this order"

Checklist

  • One structured entry per MCP call
  • agent_id, version, principal, session, tool, args, scopes
  • decision and reason on every entry
  • Linked prepare and execute entries with approver
  • Redaction map for sensitive fields
  • Append-only storage with restricted access
  • Agent id stamped on carts and orders
  • Per-agent reporting and alerts

Wrapping up

Good audit logs make every other control visible. Scopes, limits, caps, and confirmations all leave a trace, and agent-driven orders can be credited accurately. It is one of the least glamorous parts of an agent integration and one of the first you will be grateful for.

If you are building MCP servers, AI agents, or a Next.js eCommerce checkout that agents can use, and want logging and attribution designed in properly, I would be glad to help. Reach out through my contact section or at ahmadraza365pro@gmail.com.

  • 5 min read

    A 30-Day Agentic Commerce Rollout for a Next.js Store

    A practical, week by week plan to make a Next.js store ready for AI shopping agents: clean the data, ship a read only catalog MCP server, add carts, hand off to confirmed checkout, then log and cap everything.

  • 5 min read

    Fraud and Abuse When Agents Shop Your Checkout

    AI shopping agents look a lot like bots, and bots look a lot like agents. Here is how to tell them apart, where to put bot challenges, and why hosted checkout like Shop Pay is your best trust boundary.

  • 4 min read

    Hydrogen SDK vs Calling Storefront API Yourself in 2026

    Shopify's Hydrogen SDK now reaches beyond Remix and React Router into other frameworks. Here is what it gives you for agent ready storefronts, what a thin Next.js client gives you, and when each one pays off.