Building MCP tools that do not time out
I recently had the chance to add a few tools to an internal MCP server at work. MCP, or the Model Context Protocol, is the protocol that lets an AI client discover a server's tools, inspect their input schema, and call them during a conversation.
The initial use case sounded straightforward. Someone wanted to ask a chat client which workspaces had a feature enabled. We already had an API that stored the flag, and we already had APIs for getting workspace members. It felt like a matter of putting those two pieces together.
The first version passed its tests. The first real request timed out.
This is the story of what we changed, but it is also a useful distinction to make when building tools for an agent: an MCP tool is not simply an API endpoint with a JSON schema around it.
What a feature flag looks like
First, a little context. A feature flag in this system can be rolled out to part of a population, or it can be forced on and off for particular IDs. The flag name contains the kind of ID it works with. Some flags use an email address, some a domain, some a workspace ID, a user ID, or a Salesforce organization ID.
The stored data is roughly shaped like this:
{
"name": "featureX:workspaceId",
"ratio": 0.25,
"exceptions": {
"workspaceIdA": 0,
"workspaceIdB": 1
}
}
ratio is the percentage rollout. A workspace is bucketed using a hash, and it gets the feature when its bucket is within that percentage. The values in exceptions override that result.
There is one detail worth calling out. In this representation, 0 means that the feature is forced on and 1 means it is forced off. It is not the first thing most people expect when they see those values, so the tool needed to explain it instead of handing the raw document to the model and hoping for the best.
The first approach
The obvious response to a question such as "which workspaces have this enabled?" is not a list of IDs. A person usually needs to know which people are actually affected.
So the first tool did two things. It loaded the flag, then it resolved the IDs in its exception list. When the flag used user IDs, it looked up names and email addresses. When the flag used workspace IDs, it got each workspace and its members.
That is a nice response shape for a human reader. We get the flag details and a readable list of people in one call.
The first real flag we checked was at a 0% rollout, with 24 workspaces force-enabled in the exception list. The tool loaded the flag and made one workspace call after another. It never returned its result. The client gave us MCP error -32000, which is its generic timeout error.
At first I thought we had a dependency problem. Twenty-four calls timing out suggests something slow upstream, so I tested the most basic piece by itself: getting the flag definition.
That endpoint returned in about three seconds, with a 798-byte response. Three seconds is not ideal, but it was not enough to explain a timeout. The actual work looked more like this:
get feature flag 1 request
resolve 24 workspace IDs 24 requests
return the response 0 responses, because the client timed out first
The server had done 25 requests before the model received any information. The tool was trying to save the model one follow-up call, and it had made the original question impossible to answer.
Letting the model make the next call
The fix was not to make each workspace request a little faster. We changed what the first tool was responsible for.
It now returns the flag's rollout information, the exception IDs, counts for each group, and a note that the IDs have not been resolved yet. It also tells the model exactly which tool can resolve them.
{
"facet": "workspaceId",
"ratio": 0,
"forcedOn": { "count": 24, "ids": ["...", "..."] },
"forcedOff": { "count": 0, "ids": [] },
"resolved": false,
"note": "Pass these ids to workspace.members_batch to get names and members."
}
The follow-up tool is workspace.members_batch. It receives up to 100 workspace IDs and returns their memberships.
This means that a question that needs the names now takes two calls instead of one. That sounds worse on paper. In practice, the first call takes 0.55 seconds and returns 2.3 KB. The batch membership call takes 0.75 seconds and returns 16.7 KB for the 24 workspaces. It covers 129 members, with 129 distinct user IDs and no missing email addresses.
So the full answer takes about 1.3 seconds. The original "one-call" design took long enough that it had no answer at all.
The model does not need us to hide the second call from it. It needs the first response to make that next call easy to choose.
Adding a real batch API
There was an important piece behind workspace.members_batch. We could have added an MCP tool that accepted 100 IDs and then looped through the old one-workspace endpoint. That would have changed the tool's signature without fixing the problem.
Instead, we added POST /v1/workspaces/members/batch. The endpoint accepts up to 100 workspace IDs and gets the data with one database aggregation. It reads from a replica and has an maxTimeMS limit of 8,000 milliseconds, so the database stops a query that runs too long.
We then tested the largest request the endpoint allows. One hundred workspaces contained around 16,800 seats. The endpoint returned 14,259 members in about 8.5 seconds, with a 1.06 MB response.
That result was close to the database's eight-second deadline. It is not an issue for a request about 24 workspaces, but it is a number worth keeping. A later tool that decides to inspect every flag in the system can reach that limit very quickly.
The response also has a cap of 1,000 entries per group. We chose it after checking the largest real exception list, instead of choosing a round number. If the cap is reached, the response says that it was truncated. Returning a partial list as if it were complete would leave the model with the wrong picture of the flag.
Testing the aggregation without production access
We did not have production database access for this change, so we wanted another way to check the new query.
The old single-workspace endpoint was useful here. It had been written separately, at another time, and used a different query. We ran both paths for the largest workspace in the batch response and compared them. They returned the same user IDs and the same email addresses.
It is not a formal proof that every workspace will behave the same way. It did give us confidence that the new aggregation was not dropping a membership group or joining the wrong records, which were the mistakes we were worried about.
A couple of confusing details
While testing, we found that the old single-workspace endpoint needs a real caller ID header. Passing a placeholder gives a 400 Invalid workspace response, even when the workspace is valid. The batch endpoint does not have that requirement. We initially spent time looking at the workspace data, when the problem was the identity in the test request.
The name-lookup endpoint also has a batch limit of 15 user IDs. The workspace-members endpoint takes 100. They live in different services, and there is no shared batch-size convention between them.
Finally, MCP clients usually call tools/list when they connect and cache the returned schemas. This means that after a deploy, the server can support a new parameter but an existing client may still send the old shape. A new enum value happened to work for us, because it was still a string. A new array parameter did not. The cached client turned it into a string, and the server correctly rejected it with Expected array, received string.
For that reason, we now keep a small JSON-RPC client that calls the deployed server directly. It lets us tell the difference between a server problem and a client that has not refreshed its tool catalog yet.
Wrapping it up
The main change in how I think about these tools is simple. For an HTTP API, it is often enough to measure the request itself. For an MCP tool, we need to measure the whole conversation step it creates.
If a tool needs to enrich 24 IDs, it is worth asking whether it should do that itself, or whether it should return the IDs and let the model call a second tool designed for enrichment. In our case, splitting that work made the system faster, easier to debug, and more honest about what each call was doing.
The answer to the original question still contains the same information. It just arrives in a way an agent can actually use.