Filtering MCP Tools with jq (continued)
In March, I wrote about building MCP tools that could be filtered by jq expressions, but I did little to validate the idea. Recently, Scott Blum and I have been looking into turning many existing gRPC services into MCP tools, somewhat automatically, based on the obvious realization that imperfect access to a data source is far better than none. The catch is that some of these endpoints return an unwieldy blob of JSON because they weren't designed for an agent. But as I said in March, coding agents are amazing at filtering JSON on the command line, and there's no reason we can't lean on that when the data comes by way of an MCP tool. So the jq filtering idea is back in my head, and I wanted to either prove or disprove its utility.
Again, why?
It's worth revisiting why we want this. A valid criticism of MCP tools is that they return too much data into the agent's context. It's very hard to design a tool that returns only what the agent needs to answer a particular question. Most just return a JSON object, and the parts the agent doesn't need still burn input tokens. It would be much better if the agent could ask for only the parts of the object it needs, which is exactly what jq is for. This context bloat has led many to suggest that agents should just run command-line tools, jq included, in a sandbox. But that ignores the complexity of authenticating to a remote service. MCP, for all its faults, can use OAuth to authenticate to remote services. When the MCP client handles the token outside the agent's sandbox, the agent can't read it directly and accidentally leak it to the LLM provider, who just might upload it to an S3 bucket to share with a third party and cause a security incident—hypothetically. So as I said in March, I don't like much about MCP, except that it's practical for users, so I'm inclined to make it work.
Again, what?
Right, let me remind you of the idea. When a coding agent like Claude Code uses command-line tools, it often pipes the output into other tools (e.g., jq, grep, and sed) to filter it down to only what's needed. That lets the agent use even those tools that return too much data. Most MCP tools return structured JSON, often more than the agent needs. So why not give agents a tool built for filtering JSON on those responses? I tested two approaches, jq expression parameters and a jq meta tool. To explain both, we need an example tool, find_nearest_stations.
This tool accepts a latitude, a longitude, and the number of results to return, k. Here's a typical call and its response.
mcp_find_nearest_stations({
"k": 1,
"latitude": 34.42711,
"longitude": -77.54608
})
{
"stations": [
{
"distance": 8328.841265849984,
"station": {
"country": "US",
"elevation": 21,
"icao": "",
"id": "USA00749069",
"location": {
"lat": 34.5,
"lng": -77.567
},
"name": "CAMP DAVIS AAF",
"state": "NC",
"wmo": ""
}
}
]
}
The most common use for this tool is to find station IDs near a named place. For that, the agent only needs the id field from each station.
jq Expression Parameters
The first approach adds an optional jq_expression parameter. The agent supplies an expression, and the tool applies it before returning the result. Here's what the call looks like.
mcp_find_nearest_stations({
"k": 1,
"latitude": 34.42711,
"longitude": -77.54608,
"jq_expression": ".. | objects | (.id // .station_id // empty)"
})
{
"result": [
"USA00749069"
]
}
You'll notice that the agent has reduced the result to an array containing only station IDs.
jq Meta Tool
The second approach leaves existing tools unmodified and adds jq as a callable tool. The jq tool accepts the name and parameters for another tool call. It makes that call and transforms the results. Here's what the call looks like.
mcp_jq({
"tool_name": "find_nearest_stations",
"tool_args": {
"k": 1,
"latitude": 34.42711,
"longitude": -77.54608
},
"jq_expression": ".. | objects | .id? // .station_id? // empty"
})
{
"result": [
"USA00749069"
]
}
Results
I wanted to see whether either strategy could substantially reduce input tokens while preserving accuracy. To test this, I created a set of quantitative weather questions that require the use of tools that can look up information in NOAA's GHCNd and SSOD v2 datasets. Here's one of the test prompts.
List the five dates with the highest reported daily maximum temperatures at Phoenix Sky Harbor International Airport in July 2023. Return the date and temperature in °C for each, ordered by temperature descending and then date ascending.
I computed and validated the expected answer for each prompt. I then tested two models, Claude Opus 5.5 and GPT 6.2 Sol.
Accuracy
Both models arrived at the expected answer for every prompt under all three strategies. As the charts below show, both jq strategies took more tool calls and still used substantially fewer input tokens.
Efficiency
Across all prompts, the jq parameter and jq tool reduced total input tokens by 85.5% and 81.9% for Opus and by 91.7% and 93.3% for Sol. Median savings per prompt were 82% with the jq parameter and 74% with the jq tool for Opus, and 88% and 90%, respectively, for Sol.
jq Use
Opus used jq on about half its calls in both jq strategies. I'm particularly impressed that Sol used jq on every call when the parameter was available and on 86% of calls when jq was a separate tool.
Conclusion
This worked better than I expected, especially for Sol. Both models used both strategies effectively and saved a lot of input tokens. The implementation complexity of both strategies is about the same. The jq tool has to be able to call other tools, which requires some plumbing. The parameter approach can wrap existing tools and handle the jq transformation in the wrapper.
It is also worth acknowledging as I did in my earlier post that there is a more general form of this pattern where you provide a sandboxed runtime for a general-purpose language, like Python or TypeScript, but hosting such a runtime is fairly complex. Also, do keep in mind that jq does not impose any time complexity guarantees on its filters, so even with this simpler approach, you should probably enforce some runtime limits.
Will we be using one of these strategies? Quite likely.
As always, the code is on GitHub.