How do you enforce a required tool call order in an MCP server?
The protocol gives you nothing here, which is the part people miss when they go looking for a setting. tools/list is a flat set with no native dependency graph — a client (really, the model picking from that list) is free to call tool B before tool A unless your server code stops it. There are two real mechanisms, and they solve different halves of the problem. The first is state-gated execution: key an in-memory (or persisted, for long-lived sessions) record by the transport's session identifier, have tool B check that its precondition was satisfied before doing any work, and return a normal tool error — not a crash — naming exactly what's missing ('call tool_a first'), so the model can recover inside the same turn. The second is dynamic exposure: actually remove the gated tool from tools/list until its prerequisite is met, then emit notifications/tools/listChanged so a spec-compliant client re-fetches the list — this stops the model from ever seeing the wrong tool rather than rejecting it after the fact. Client support for listChanged varies, so treat it as a defense-in-depth layer on top of the state check, never a replacement for it. A tool description that says 'call this only after X' is worth writing but is advisory — the model can still ignore it — so it belongs alongside real enforcement, not instead of it.
Related questions
Does the MCP spec itself support declaring a required tool order?
No. tools/list exposes a flat set of tools with no dependency graph, prerequisite field, or ordering hint the protocol enforces for you. Any ordering has to be built into the server's own logic — the spec treats every tool as independently callable at any time.
What's the simplest way to enforce order without touching the protocol layer?
State-gated execution: store a flag or status per session, and have the later tool check it before doing any work, returning a normal tool error (not a crash) if the precondition isn't met. This needs no protocol feature — it's ordinary server-side logic — and it's the one technique that works regardless of which client is connected.
Should I hide the gated tool entirely, or just let the call fail with a clear error?
Ideally both. Removing the tool from tools/list and emitting notifications/tools/listChanged stops the model from attempting the wrong call in the first place, but not every client refetches promptly on that notification. Keep the state-gated error as the fallback that always works, and treat hiding the tool as an added layer, not a replacement.
MCP servers are often stateless between calls — how do I even know 'where' a session is?
Key your tracking off whatever session or connection identifier your transport exposes (stdio gives you one process per connection; HTTP-based transports typically carry a session header) and keep an in-memory map from that key to workflow state. Don't rely on the client to remember or replay the order for you — the whole reason this question gets asked is that clients don't guarantee that.
Can I rely on the tool's description to stop the model from calling it early?
No — a description that says 'call this only after X' is a hint the model usually follows, not a constraint it must follow. It's worth writing because it reduces how often the out-of-order call happens, but it has to sit on top of a real state check, since an occasional ignored hint is exactly the failure mode a required order is meant to prevent.
Does AgentKit Works' MCP Debug Kit help enforce tool call order?
Not directly — the free MCP Debug Kit's mcp-server-hardening skill covers pre-release hygiene (stdout discipline, schema validation, capability declarations, cross-client testing) and its mcp-doctor skill diagnoses connection and registration failures, but neither implements workflow-order enforcement, which is server-logic you write yourself. The hardening checklist's capability-declaration step is the closest overlap: get that right and listChanged notifications — one of the two mechanisms above — actually work the way the spec intends.
THE TOOL FOR THIS JOB
MCP Debug Kit
Free: a reference for MCP servers whose tools don't show up — diagnosis order, error atlas, 5-client config matrix.
2 skills · 10 failure modes
View spec
Get free →