Normalize results and block violations with hooks
Pick the Agent SDK hook and placement for a business rule or data problem, and complete the hook so it normalizes tool results or blocks a policy-violating call and redirects the agent.
Scenario
Your Customer Support Resolution Agent handles a return. lookup_order reports delivery at 1717430400, a Unix timestamp, and the customer's return request is dated 2024-06-28T09:00:00Z. The policy allows 30 days. The agent misreads the timestamp as a May date and refuses a return that is well inside the window.
The same week it calls process_refund for $1,800. The system prompt says "never refund more than $500 without a human". It said so on every one of those conversations.
Both problems have the same fix: code that runs between the agent and its tools.
Explanation
A hook is a callback the Agent SDK runs at a fixed point in the agent loop. Two points matter for tool calls:
| Hook | Fires | Can |
|---|---|---|
PreToolUse |
after Claude requests a tool, before it runs | deny the call, ask, allow, or rewrite its input (updatedInput) |
PostToolUse |
after the tool returns, before Claude sees the result | replace the result (updatedToolOutput) or add context (additionalContext) |
The exam guide calls the first one a tool call interception hook. In the SDK it is PreToolUse.
Register hooks in the hooks option: an event name maps to a list of matchers, and each matcher holds callbacks. The matcher is tested against the tool name. MCP tools are named mcp__<server>__<tool>, so mcp__support__process_refund targets one tool and mcp__support__.* (a regular expression) targets every tool on that server.
The callback receives the event input (tool_name, tool_input, and for PostToolUse tool_response), a tool use ID and a context. It returns an object: {} lets the call through unchanged; event-specific decisions go in hookSpecificOutput.
Normalize data with PostToolUse. Different MCP tools return dates as Unix seconds or ISO 8601, and statuses as numeric codes. A PostToolUse hook converts them to one format with updatedToolOutput, so the model reasons over consistent data. The value must keep the tool's output shape. Older docs and exam items may name updatedMCPToolOutput: the earlier field, which replaces MCP tool output only; the docs now prefer updatedToolOutput, which works for every tool. The tool has already run, so PostToolUse can never stop a side effect.
Enforce a rule with PreToolUse. Return permissionDecision: "deny" with a permissionDecisionReason. The reason goes back to the model, so write it as the redirect: "Refunds over $500 need a human: call escalate_to_human."
Hook or prompt? A prompt instruction is followed most of the time. A hook runs every time. When a rule involves money, compliance or data integrity, put it in a hook and keep the prompt line only to explain the policy.
Worked example
Before: the refund limit lives in the system prompt.
Never issue a refund above $500. Escalate those to a human instead.
The agent follows it in most conversations. In a long dispute with several orders it adds up the amounts and refunds $1,800 in one call.
After: a PreToolUse hook enforces the limit in code.
const capRefund: HookCallback = async (input) => {
const pre = input as PreToolUseHookInput;
const args = pre.tool_input as { amount: number };
if (args.amount <= 500) return {};
return {
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "deny",
permissionDecisionReason:
"Over $500: call escalate_to_human instead.",
},
};
};
And it is registered next to a normalizing hook for every support tool:
hooks: {
PreToolUse: [{
matcher: "mcp__support__process_refund",
hooks: [capRefund],
}],
PostToolUse: [{
matcher: "mcp__support__.*",
hooks: [normalizeDates],
}],
}
Now a $1,800 refund never runs. The agent reads the reason and escalates with a handoff, and every date it sees is ISO 8601.