llamaIndex - 💡(How to fix) Fix [Feature Request]: Tool output postprocessing and composable middleware (follow-up from #20386) [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
run-llama/llama_index#21230Fetched 2026-04-08 01:58:04
View on GitHub
Comments
1
Participants
2
Timeline
1
Reactions
0
Author
Participants
Timeline (top)
commented ×1

PR #21228 adds protected_params and DynamicValue to FunctionTool, addressing the input enforcement part of #20386.

This issue tracks the remaining scope from #20386 that was not covered:

Root Cause

PR #21228 adds protected_params and DynamicValue to FunctionTool, addressing the input enforcement part of #20386.

This issue tracks the remaining scope from #20386 that was not covered:

Code Example

agent = FunctionAgent(
    tools=mcp_tools,
    tool_io_middleware=[
        ToolIOMiddleware(
            before=lambda tool_name, args: override_id(args, SESSION_CUSTOMER_ID),
            after=lambda tool_name, out: filter_fields(out, tool_name),
        )
    ],
)
RAW_BUFFERClick to expand / collapse

Context

PR #21228 adds protected_params and DynamicValue to FunctionTool, addressing the input enforcement part of #20386.

This issue tracks the remaining scope from #20386 that was not covered:

1. Output postprocessing

Deterministic filtering/transformation of tool output before it reaches the agent context. The motivating example from #20386: an MCP billing tool returns ~300 fields, but the agent only needs 3. Today this requires per-tool wrapper functions.

2. Composable middleware / hook API

A reusable, cross-cutting pre/post IO transform layer that works across tools (including MCP tools) without requiring per-tool wrappers. The desired ergonomics from the original issue:

agent = FunctionAgent(
    tools=mcp_tools,
    tool_io_middleware=[
        ToolIOMiddleware(
            before=lambda tool_name, args: override_id(args, SESSION_CUSTOMER_ID),
            after=lambda tool_name, out: filter_fields(out, tool_name),
        )
    ],
)

Related

  • Original feature request: #20386
  • Input enforcement PR: #21228
  • Adjacent discussions: #9770, #19023, #19722

extent analysis

TL;DR

Implement a ToolIOMiddleware class to enable composable middleware and hook API for pre/post IO transformations.

Guidance

  • Identify the specific requirements for output postprocessing and middleware functionality needed for the MCP billing tool and other tools.
  • Design a ToolIOMiddleware class that can handle both before and after transformations, allowing for flexible and reusable middleware configurations.
  • Develop a filter_fields function to deterministically filter the output of tools, such as the MCP billing tool, to only include the necessary fields.
  • Consider implementing a registry or factory pattern to manage and compose multiple middleware instances.

Example

class ToolIOMiddleware:
    def __init__(self, before=None, after=None):
        self.before = before
        self.after = after

    def __call__(self, tool_name, args, out=None):
        if self.before:
            args = self.before(tool_name, args)
        if out and self.after:
            out = self.after(tool_name, out)
        return args if out is None else out

Notes

The implementation details of the ToolIOMiddleware class and the filter_fields function will depend on the specific requirements and constraints of the project.

Recommendation

Apply workaround by implementing the ToolIOMiddleware class and associated functions to enable composable middleware and hook API for pre/post IO transformations, as this will allow for more flexible and reusable tool configurations.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING