A toolbox for your agent, tested
For engineers
Tool design for agents and MCP, tool-use APIs and code graders that prove the tools work.
The one-sentence idea: an agent is only as good as its tools, so design each tool for the model that will call it, wire the tool-use loop so failures come back as results, and test the whole thing with code graders before you trust it.
The case
Northgate Parcel runs a support assistant. It must answer "where is my parcel?", send delay notices and, with approval, issue refunds. The first version wraps the company's REST API: fourteen endpoint-shaped tools with names like get and list_events, fields such as shipment_uuid, and stack traces for errors. In a first trial, 27 of 40 test tasks succeed and the median run takes six tool calls.
Why it matters
When an agent fails, people blame the model. Often the tools are at fault. A model chooses a tool from its name and description, fills arguments from its schema, and reads whatever comes back. Claude, ChatGPT, Gemini and open-weight models all work this way. A vague name, an opaque id or a raw error costs a wrong call, and each wrong call costs tokens and time.
How to do it
1. Decide how much autonomy you need. Most "where is my parcel?" tickets follow one path: find the order, read the events, write a reply. That is a fixed workflow with model steps. Only the long tail, such as damaged parcels or customs holds, needs a loop, because the next step depends on what each lookup returns. Start simple and add autonomy where the task demands it.
2. Shape tools around tasks. Four endpoints that are always called in the same order become one northgate_track_shipment tool. Use a namespace prefix so the name stays unambiguous next to other servers. Describe the tool like a note to a new colleague: when to use it, what the input looks like, what comes back. Constrain parameters with a pattern or enum and add an example.
3. Keep outputs small and readable. Return status, ETA and the last three events with place names, not hub ids. Offer detail: "full" and say so in the result when you cut something. Keep write tools separate from read tools: a lookup must never send an email.
4. Make failures useful. Validate input in the handler. For a bad order number, return an error result that says what is wrong, what a valid value looks like and which tool to try next. In MCP, set isError: true in the tool result so the model sees it. In a hand-written loop over a model API, catch handler exceptions and return a tool_result with is_error, matched to the call by its id. Return all results for parallel calls in one reply.
5. Gate side effects. For tools that send or pay, require an idempotency key so a retry does not repeat the action, offer a dry run, and let code, not the prompt, decide when a human must approve.
6. Test with code graders. Write realistic tasks and grade what code can grade: the tool name and normalized arguments, whether the final output matches a JSON Schema, and a few precise assertions. Run the same tasks after each change and compare success rate, calls per task and tokens. Read transcripts for the failures that recur.
Before and after
Before: tool get(id), "Gets data", a 4,900-token response, and "Request failed with status 404" plus a trace. After: northgate_track_shipment(order_number) with a pattern, a concise default response, and "No shipment found for NP-8821. Order numbers are NP- plus 5 digits." In the second trial, the same 40 tasks pass 36 times and the median run uses two calls. These numbers are from the team's own test, not a benchmark.
What goes wrong
- Grader gaps. A contains-check for "deliver" passes "has not been delivered". Code graders measure format and exact facts. They do not measure tone or whether a stated ETA is true, so add a comparison with the tool result and sample replies by hand or with a judge model.
- Hidden side effects. Merging tools to save calls can add a default that emails customers on every lookup.
- Unbounded loops. An agent with no step cap, token budget or handoff to a person can run up cost on one hard ticket.
- Over-trusting a pass rate. 97% on weak graders is not 97% correct.
Steps
Pick the lightest design that works
Skill: agent-workflow-or-agent
Northgate measured 100 tracking tickets. A two-step workflow was nearly as accurate as an agent for a fraction of the cost, so the common path stays a workflow and only damaged-parcel and customs tickets get a loop with a step limit and a handoff to a person.
Same 100 tickets, three designs Design Tokens Median seconds Correct replies Single call 900 2 41 Fixed workflow 3,100 4 94 Autonomous agent 14,800 13 95 Rewrite the tool definition
Replace
get(id)with a namespaced, task-level tool. The description says when to use it, the parameter has a pattern and an example, and write tools stay separate.northgate_track_shipment { "name": "northgate_track_shipment", "description": "Status, ETA and last 3 events for one order. Use for 'where is my parcel'. Not for refunds or address changes.", "inputSchema": { "type": "object", "properties": { "order_number": { "type": "string", "pattern": "^NP-[0-9]{5}$", "description": "e.g. NP-88213" }, "detail": { "enum": ["concise", "full"], "default": "concise" } }, "required": ["order_number"] }}Return results the agent can use
Cut the 4,900-token response to status, ETA and three events with place names. When there is more, say how to get it. On failure, return
isError: truewith a message that names the cause and the next tool to try.Close the loop in code
The handler validates arguments and turns any exception into an error result with the matching call id. Parallel requests run concurrently and their results go back in one reply. The loop has a step cap, and
refund_orderreturns a pending-approval result until a person confirms.Grade with code
Run the 40 tasks after every change. Grade the tool name and the normalized order number exactly, check the final JSON against a schema, and add assertions such as no card numbers and an ETA that matches the tool result. Keep a small human or judge-model sample for tone.
Read the results, change one thing, rerun
Eleven failures shared one cause: an order number passed in a customer lookup's
idfield. Renaming it tocustomer_emailwith an example is a one-line change that the next run can confirm.
Skills this scenario uses
Where to study
- Writing effective tools for AI agents (Anthropic) (opens in a new tab)ArticleFree
Tool consolidation, naming, concise output and evaluating tools with agent runs.
- Building effective agents (Anthropic) (opens in a new tab)ArticleFree
When to use a workflow versus an agent, and why to start simple.
- MCP specification: Tools (opens in a new tab)StandardFree
How MCP tools report results and errors, including isError.
- Tool use overview (Claude docs) (opens in a new tab)DocsFree
The tool-use loop, tool results, parallel calls and tool choice.
- promptfoo: Assertions and metrics (opens in a new tab)DocsFree
Code-based assertions: equals, contains, JSON schema checks and more.
- JSON Schema (opens in a new tab)StandardFree
The format for checking the shape of model output and tool arguments.