- What it is:
- JMESPath: A query language specifically designed for JSON. Think of it like SQL, but for JSON data.
- Pruning: The process of cutting away unnecessary parts of the data.
- JMESPath Pruning in HasMCP: HasMCP uses JMESPath to allow you to declaratively filter and reshape JSON responses from your APIs before they are sent to the LLM.
- Why it’s important: APIs often return much more data than is actually needed for a specific task. For example, an API might return a user object with 50 fields, but you only need the user’s name and email address. Sending all 50 fields to the LLM is wasteful.
- How it works: With HasMCP, you can define a JMESPath query that will be applied to the JSON response from your API. This query specifies exactly which fields to keep and which to discard. The “pruning” happens automatically before the data is passed to the LLM.
- Key benefits:
- Declarative and Simple: JMESPath provides a simple, declarative way to specify the data you need. You don’t have to write any procedural code.
- Efficient: JMESPath is highly optimized for JSON data, so the pruning process is very fast.
- Reduces “Noise”: By removing irrelevant data, you can help the LLM focus on the information that is most important for the task at hand.
Example
Let’s say an API returns a comprehensive user object:user.{name: name, email: email}
This will transform the data sent to the LLM into a much smaller, more relevant payload:
PII Redaction Example
JMESPath can also be used for basic PII (Personally Identifiable Information) redaction by selecting only the fields you want to expose. This “allowlisting” approach ensures that sensitive data is not sent to the LLM. Considering the same API response, let’s say you want to provide the LLM with the user’s ID and preferences, but redact their name, email, and address. You can achieve this by explicitly selecting the non-PII fields:user.{id: id, preferences: preferences}
The resulting payload sent to the LLM would be:
List Filtering Example
JMESPath excels at filtering and transforming lists of objects. Consider an API response containing a list of products:products[?in_stock].{name: name, price: price}
This expression filters the products list for items where in_stock is true, and then projects only the name and price fields for the matching items. The pruned output sent to the LLM would be: