Zod stops bad client API data before your agent ever sees it
The MCP TypeScript SDK already requires Zod, and one schema at the entry point both checks data at runtime and generates types, so the two never drift apart.
In brief
- Zod validates data at runtime and infers TypeScript types from the same schema.
- safeParse returns a result object instead of throwing, which suits unpredictable client payloads.
- Anyone building agent tools with the MCP TypeScript SDK will be using Zod, because the SDK requires it.
If you are building an MCP server in TypeScript for a client, Zod is in the project before you have had a chance to pick a library. The official documentation for the MCP TypeScript SDK states that the SDK requires zod as a mandatory peer dependency for schema validation.
That detail says a lot about how agent tooling is built today: before data reaches an agent, it has to pass through a schema. For an FDE, the most important entry point is the client’s API. It is often a legacy system, thinly documented, and it sometimes returns things nobody anticipated.
Errors of this kind rarely crash the program on the spot. They slip quietly into the prompt, into a calculation, into the agent’s decisions, and surface three days later as a wrong answer that nobody can trace. Zod lets you stop the error at the door instead of hunting for it at the far end of the pipeline.
Zod does one job, from both sides
The official site, zod.dev, describes Zod as a TypeScript-first schema validation library with static type inference. It was created by Colin McDonnell, is MIT-licensed and has no external dependencies. Its API is immutable and its core is very small.
“From both sides” works like this. At runtime, .parse checks the input and returns a typed deep clone, so unvalidated data becomes typed data only after it has passed through the schema.
At compile time, z.infer derives the TypeScript type from that same schema. You no longer have to maintain a hand-written interface alongside a separate validation function and worry about the two drifting apart.
An order payload passes through the schema
Take a client that runs a retail chain, where your agent reads orders to answer questions from customer service staff. The API documentation says amount is a number. Then one day the response comes back as { "id": "A-1029", "amount": "12.5", "status": "paid" }, with amount as a string.
import { z } from "zod";
const Order = z.object({
id: z.string(),
amount: z.number(),
status: z.string(),
});
type Order = z.infer<typeof Order>;
const result = Order.safeParse(rawFromCustomerApi);
if (!result.success) {
for (const issue of result.error.issues) {
console.warn(issue.path, issue.code, issue.message);
}
} else {
runAgent(result.data); // result.data is typed as Order
}
There are two options here. .parse throws a ZodError when the input is invalid. safeParse does not throw; it returns a result object containing either the parsed data or a ZodError. With client data, where errors are routine, safeParse is usually the more sensible default.
A ZodError carries plenty of detail: each issue has a code, a path and a message. In the example above, path points straight at amount. Instead of telling the client “the agent gave a wrong answer”, you can say “the orders endpoint returns amount as a string”, and their team knows immediately where to look.
Three traps, and how to fix the string amount
The most common trap is calling .parse directly on raw client data inside the agent loop without a try/catch. A single bad payload throws a ZodError and the whole run stops. At this entry point, safeParse is safer.
The next trap is coercing types yourself to “make it work” without asking the client. Turning "12.5" into 12.5 looks harmless, but you do not yet know whether it is a temporary bug, a new format or a different unit. Silent coercion hides the very signal the schema just caught.
And do not keep a hand-written interface in parallel with a schema. Sooner or later they will diverge; z.infer brings them back to one source.
For the amount case, there are two correct fixes, depending on what the client says. If the client confirms amount should be a number, keep z.number(): safeParse rejects the payload, you log the path and pass the error to their team. If the client confirms the endpoint now returns a string on purpose, record that in the schema and convert in one clearly marked place:
const OrderRaw = z.object({
id: z.string(),
amount: z.string(), // confirmed by client: amount is returned as a string
status: z.string(),
});
const r = OrderRaw.safeParse(rawFromCustomerApi);
if (r.success) {
const amount = Number(r.data.amount);
if (Number.isNaN(amount)) {
// string cannot be read as a number: reject, do not pass it to the agent
} else {
runAgent({ ...r.data, amount });
}
}
The difference is that the conversion is now a decision agreed with the client, commented and kept in one place in the code, rather than a silent patch nobody knows about.
Where Zod cannot help
Zod checks the shape of data. Its meaning is outside its reach. If amount is the number 1250, the schema accepts it whether the client counts in cents or in whole currency units. To find that out, you have to sit down with the client’s engineers.
Nor does Zod decide for you what happens when data is wrong: reject it, log it and skip, or hand it to a person. That is a product decision, so settle it with the client early. One more obvious limit: Zod lives in the TypeScript ecosystem, so if the client’s pipeline is written in another language, you can use Zod only in the TypeScript parts.
Then there is the question of versions. Zod 4 has been announced as stable. The release notes say Zod 4 parses up to 14.71 times faster than Zod 3 in their benchmarks, and that the core is about 57% smaller (2.3 times).
If the client’s codebase is still on Zod 3, treat the upgrade as a separate change with its own tests, and do not fold it into a bug-fix commit.
What to learn first, and how to show the skill
A sensible order is z.object with a few basic types, then safeParse, then reading ZodError, and finally z.infer. Those four are enough to build an entry-point validation layer for most integrations. After that, read how the MCP TypeScript SDK uses Zod to describe tool inputs, because that is where the schema meets the agent.
When reading FDE job descriptions, if a listing mentions building MCP servers or agent tools in TypeScript, mention your Zod experience in the cover letter itself: the MCP TypeScript SDK requires Zod, so it is a skill you will use from week one.
On your CV, do not just write “knows Zod”. Describe outcomes, for example: put schemas at the entry point for client APIs, grouped errors by field, and cut the time needed to find the cause when the agent answered wrongly.
Many people assume a strong FDE is someone who writes good prompts. Yet once an agent goes into production at a client site, what keeps it stable is often a schema that runs before the prompt.
Was this article useful?
Thanks for the feedback!