- What it is:
- Goja: An engine that allows you to run JavaScript code within a Go application.
- Goja (JS) Logic in HasMCP: HasMCP embeds the Goja engine, allowing you to write “JavaScript Interceptors.” These are snippets of JavaScript code that can intercept and modify the responses from your APIs before they are sent to the LLM.
- Why it’s important: Sometimes, simple filtering with JMESPath isn’t enough. You might need to perform more complex transformations on the data, such as:
- Combining multiple fields into a single, more descriptive field.
- Formatting dates or numbers.
- Applying conditional logic (e.g., if a field has a certain value, then modify another field).
- Enriching the data with information from other sources.
- How it works: In HasMCP, you can write a JavaScript function that will be executed on the JSON response from your API. This function has access to the full JSON object, and it can return a new, modified JSON object. This new object is then what gets sent to the LLM.
- Key benefits:
- Flexibility: The full power of JavaScript is at your disposal, allowing you to perform almost any data transformation you can imagine.
- Procedural Logic: Unlike the declarative nature of JMESPath, JavaScript allows you to write procedural code, with loops, conditionals, and other control structures.
- Stateful Transformations: You can perform more complex, stateful transformations that are not possible with a simple filtering language.
Example
Consider the same comprehensive user object from an API response as in the JMESPath example:order_history, a simple JMESPath query might not suffice. Here’s how a Goja (JavaScript) interceptor can achieve this: