"The first response doesn't have the data": a client for a two-step API
AEMET's open API first answers with an envelope that points to the data, and you have to ask again to get it. Why we put those two hops in a single client, what that bought us, and which part of the original decision no longer holds up.
You ask AEMET's open API for the Madrid forecast and it comes back with a 200 and not a single temperature. What you get is this:
{
"estado": 200,
"descripcion": "exito",
"datos": "https://opendata.aemet.es/opendata/sh/xxxxxxxx",
"metadatos": "https://opendata.aemet.es/opendata/sh/yyyyyyyy"
}
The real data sits behind a second GET to the URL in datos. The one in metadatos describes the schema, and we don't use it in aemet-mcp, the MCP server (and npm library) we built on top of that API.
This has a practical consequence that's easy to underestimate: everything any query needs (status, errors, retries, cache) exists twice, once per hop. This post is about where we decided to put that duplication.
The decision: one place that knows the mechanics
ADR-0004 in aemet-mcp frames it in two lines. A client that does a single GET returns the envelope and nothing else, so either every tool does both hops, or one client encapsulates them. We went with the client: AemetClient, with a fetchJson(path) method that fetches the envelope, validates the estado, makes the second GET and parses.
For a tool, the consequence fits in one line. This is how it asks for the daily forecast:
const data = await getClient().fetchJson<PrediccionDiariaMunicipio[]>(/* path */);
The tool doesn't know there are two requests. It asks for typed data and gets it, or gets a typed error.
The rejected alternative is the one that would have happened on its own: each tool (daily forecast, hourly, observation, warnings…) making its own two hops. The ADR dismisses it in one line, "duplication and risk of inconsistency", and it's a good summary. It's not that five copies of the same code are ugly. It's that five copies end up behaving five different ways on a 429, and the day you have to change the policy you no longer know where to look.
What having a single place buys us
The ADR sums it up as "a single place to map statuses and apply cache and retries". With the code in front of you, it comes down to three concrete things.
One status, even when it comes from two places. The estado can arrive in the envelope body (with an HTTP 200) or at the HTTP level, depending on the case. The client tries to read the envelope and, if there isn't one, falls back to the HTTP status. Then it maps that to typed errors: 401 is UNAUTHORIZED, 404 is NOT_FOUND, 429 is RATE_LIMITED, everything else is UPSTREAM. The caller never has to decide what a 404 from AEMET means.
Cache, keyed by path. Since every query costs two requests, the cache hangs off the requested path, not the intermediate URLs. A hit saves both hops. And one detail mattered once the same core started being used as a library from backends with concurrency: if two calls ask for the same path at the same time, the second waits for the first instead of firing its own request. Without that, our AEMET quota was multiplied by however many requests happened to overlap. Errors aren't cached, so a one-second network blip doesn't keep returning an error for the whole TTL.
A retry policy you can reason about. This is where having both hops together matters most, and where the first version was wrong.
The loop that multiplied
The first version retried fine on each hop separately: HTTP retries on network failures and 5xx, and retries on estado 429 in the envelope. Each loop was reasonable. Nested, they weren't. The CHANGELOG tells it plainly: with maxRetries: 3, the worst case was 16 requests and, adding up the timeouts, minutes of blocking.
The fix was to change what the option means. maxRetries became a global budget per operation, shared by both hops: with 3, that's at most 4 requests in total, not 4 per hop. There's a test that pins down exactly that: a 429 on the first hop consumes one retry, the second hop always fails with a network error, and with maxRetries: 2 the total is 4 requests. Without a shared budget it would be 6.
It's a fix you can only make in a single client. With the mechanics scattered across the tools, "the operation's budget" isn't a concept that exists anywhere.
What it costs
- Two requests per query, always. The ADR records it as a cost and mitigates it with the per-path cache. It's a real cost against an API with a quota, and the cache only helps with repeated queries.
- A single point of failure in the code. If the client gets it wrong, every tool gets it wrong at once. In exchange, the bug gets fixed in one place. I prefer that asymmetry, but it's a bet, not a law.
- The second hop's URL is dictated by the server. That
datosis an address AEMET hands us, and the second GET carries the API credential. So the client checks it before sending (HTTPS and an expected host) and doesn't follow redirects. There's no mystery here, but it's the kind of thing that never occurs to you when you're "just" doing two chained GETs: if a response tells you where to go, check the destination before taking anything with you.
An ADR with one half still standing and one half not
There's a detail in ADR-0004 I find more instructive than the pattern itself. Its header now reads "Status: Superseded by ADR-0012", with a notice on top. The ADR's title bundles two decisions: the two-step pattern and decoding everything that came back as latin1. The premise of the second, "the data always comes in latin1", turned out to be false in production, and AEMET mixes encodings depending on the resource and the node that answers. The first one still stands as written.
ADR-0012 doesn't rewrite 0004: it supersedes it and points back to it. And the header note clearly separates which part is refuted and which part is still standing. That's a distinction you lose if the note just says "superseded": whoever comes later might throw out the two-step client thinking it fell with the encoding premise.
That second chapter, the one about the charset that lies and the double mojibake, has enough material for another post.
What I take away
When an API makes you take two steps, the problem isn't the two steps: it's where you take them. Spreading them across the tools duplicates status handling, retries and cache. Concentrating them gives you one place to get it wrong and one place to fix it.
A mechanism that composes two retry loops has to decide who's in charge. Each loop looked correct on its own; the flaw was in how they multiplied. The shared budget is how you turn "each hop retries" into "the operation retries."
An ADR that mixes two decisions ages at two speeds. When one falls, the header note has to say which one is still alive.
If you're using an API with a quota and multi-step responses, and you want to know where to put the cache and retries before they multiply, get in touch.