Documentation
¶
Index ¶
- Constants
- func ApplyToolResponseFilter(ctx context.Context, filter string, jsonData any) (any, error)
- func CompileToolResponseFilter(filter string) (*gojq.Code, error)
- func CompileToolResponseFilterWithVars(filter string, varNames []string) (*gojq.Code, error)
- func ShouldApplyMiddleware(toolName string) bool
- func WrapToolHandler(...) ...
- func WrapToolHandlerWithFilter(...) ...
- type PayloadMetadata
Constants ¶
const DefaultJqTimeout = 5 * time.Second
DefaultJqTimeout is the default timeout for jq query execution (5 seconds) This prevents malformed queries or large payloads from causing hangs
const PayloadPreviewSize = 500
PayloadPreviewSize is the maximum number of characters to include in the payload preview This controls how much of the original payload is returned inline when a payload is stored to disk
const PayloadTruncatedInstructions = "" /* 373-byte string literal not displayed */
PayloadTruncatedInstructions is the message returned to clients when a payload has been truncated and saved to the filesystem
Variables ¶
This section is empty.
Functions ¶
func ApplyToolResponseFilter ¶ added in v0.3.7
ApplyToolResponseFilter compiles and applies a jq expression to tool response data.
func CompileToolResponseFilter ¶ added in v0.3.7
CompileToolResponseFilter parses and compiles a jq expression used to transform tool response payloads. Compiled code is cached by filter expression string so that identical filters configured on multiple tools are only compiled once.
For parameterized filters that need to incorporate per-call values such as server IDs, session metadata, or user-controlled data, use CompileToolResponseFilterWithVars instead.
func CompileToolResponseFilterWithVars ¶ added in v0.3.27
CompileToolResponseFilterWithVars parses and compiles a jq expression that references the named variables. Variable names must be prefixed with "$" (e.g. "$serverID").
Values are bound at run time by passing them in the same order to code.RunWithContext:
code, _ := CompileToolResponseFilterWithVars(expr, []string{"$serverID", "$sessionID"})
iter := code.RunWithContext(ctx, data, serverID, sessionID)
Binding values at the Go level is injection-safe: they are never interpolated into the jq expression string.
Compiled code is cached by the composite key (filter + variable names). The same (filter, varNames) tuple recurs on every tool invocation, so caching here eliminates repeated gojq.Parse + gojq.Compile calls on the parameterized-filter hot path.
func ShouldApplyMiddleware ¶
ShouldApplyMiddleware determines if the middleware should be applied to a tool Currently applies to all tools, but can be configured to filter specific tools
func WrapToolHandler ¶
func WrapToolHandler( handler func(context.Context, *sdk.CallToolRequest, any) (*sdk.CallToolResult, any, error), toolName string, baseDir string, pathPrefix string, sizeThreshold int, getSessionID func(context.Context) string, ) func(context.Context, *sdk.CallToolRequest, any) (*sdk.CallToolResult, any, error)
WrapToolHandler wraps a tool handler with jqschema middleware This middleware: 1. Generates a random ID for the query 2. Extracts session ID from context (or uses "default") 3. If payload size > sizeThreshold: saves to {baseDir}/{sessionID}/{queryID}/payload.json and returns metadata 4. If payload size <= sizeThreshold: returns original response directly (no file storage) 5. For large payloads: returns first PayloadPreviewSize chars of payload + jq inferred schema 6. Uses pathPrefix to remap returned payloadPath for clients (if configured)
func WrapToolHandlerWithFilter ¶ added in v0.3.7
func WrapToolHandlerWithFilter( handler func(context.Context, *sdk.CallToolRequest, any) (*sdk.CallToolResult, any, error), toolName string, baseDir string, pathPrefix string, sizeThreshold int, getSessionID func(context.Context) string, filter string, ) func(context.Context, *sdk.CallToolRequest, any) (*sdk.CallToolResult, any, error)
WrapToolHandlerWithFilter wraps a tool handler with jq response filtering followed by the standard large-payload jqschema middleware behavior.
Types ¶
type PayloadMetadata ¶ added in v0.0.106
type PayloadMetadata struct {
AgentInstructions string `json:"agentInstructions"`
PayloadPath string `json:"payloadPath"`
PayloadPreview string `json:"payloadPreview"`
PayloadSchema any `json:"payloadSchema"`
OriginalSize int `json:"originalSize"`
QueryID string `json:"-"` // Internal use only, not serialized to clients
}
PayloadMetadata represents the metadata response returned when a payload is too large and has been saved to the filesystem