Skip to content

feat: add pre & post processing hooks for tools#58

Merged
stenalpjolly merged 23 commits into
googleapis:mainfrom
stenalpjolly:stenalpjolly_feature_pre_post_processors
Jun 25, 2026
Merged

feat: add pre & post processing hooks for tools#58
stenalpjolly merged 23 commits into
googleapis:mainfrom
stenalpjolly:stenalpjolly_feature_pre_post_processors

Conversation

@stenalpjolly

@stenalpjolly stenalpjolly commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR introduces full support for Tool Pre and Post-processing execution hooks (ToolPreProcessor and ToolPostProcessor) in the Java SDK, fully integrated with the decoupled transport architecture and updated to align with the new thread-safety and token resolution updates.

Expectation & Implementation

  • Processor Pipeline: Adds the ability to register ToolPreProcessor and ToolPostProcessor instances on McpToolboxClientBuilder. The client propagates these hooks to all loaded tools during loadToolset and loadTool.
  • Pre-Processing Hooks: Executed sequentially before tool execution, allowing arguments to be transformed, supplemented, or validated.
  • Post-Processing Hooks: Executed sequentially after the transport layer returns a tool result, allowing the output to be mutated, audited, or wrapped.
  • Thread-Safety Defenses: Fully aligned with the concurrent token resolution mechanism (AuthResolver) introduced in PR refactor: resolve multiple auth tokens concurrently and safely in Tool.execute #65. In addition, local tool execution maps (finalArgs and extraHeaders) are wrapped in Collections.synchronizedMap(...) to prevent any concurrent modifications during async processing.
  • Backward Compatibility: Preserved all five deprecated constructors (delegating them to the new primary transport constructor) to ensure perfect backward compatibility for existing library callers.

Acceptance criteria

The PR must satisfy the following functional and integration requirements to be accepted:

  1. Pre/Post-Processor Execution:
    • Registered pre-processors must execute and be able to mutate arguments before the tool call is made.
    • Registered post-processors must execute and be able to mutate the result after the transport returns it.
  2. Hook Propagation:
    • Processors registered on the client builder must successfully propagate to all loaded tools via both loadToolset and loadTool.
  3. Concurrency Defenses:
    • Thread-safe mapping of resolved tokens to execution parameters and headers must prevent race conditions during asynchronous credential retrieval.
  4. Backward Compatibility & Quality Safeguards:
    • Preserved all five deprecated constructors to support legacy caller configurations.
    • Compilation and checkstyle checks must pass cleanly with 0 violations.
    • All 115 unit tests must pass with 100% success.
    • All modified and new classes must have 100.00% instruction and branch coverage.

Test cases

  • Integration Propagation Verification: Added testClientPrePostProcessorsPropagation in McpToolboxClientImplTest to verify that pre/post-processors registered on the client are successfully propagated to individual tools loaded via both loadToolset and loadTool.
  • 100% Coverage Target:
    • Added testProcessorsConfiguration in McpToolboxClientBuilderTest to verify non-null and null pre/post-processor configuration flows.
    • Added testDefaultValueDeepCloning_withList, testValidateAndSanitizeArgs_requiredParameterProvided, and testValidateAndSanitizeArgs_nullTypeWithNonNullValue in ToolTest to cover nested structures and parameter boundary checks.
    • Achieved 100% instruction and branch coverage for the modified Tool, McpToolboxClientBuilder, ResolvedAuth, and McpToolboxClientImpl classes with 115 passing unit tests.

Breaking changes

None.

@stenalpjolly stenalpjolly requested a review from a team as a code owner April 7, 2026 05:42
@stenalpjolly stenalpjolly force-pushed the stenalpjolly_feature_pre_post_processors branch from 6f1dbf4 to 207591b Compare April 7, 2026 05:49
@anubhav756

Copy link
Copy Markdown
Contributor

/gcbrun

5 similar comments
@stenalpjolly

Copy link
Copy Markdown
Contributor Author

/gcbrun

@stenalpjolly

Copy link
Copy Markdown
Contributor Author

/gcbrun

@stenalpjolly

Copy link
Copy Markdown
Contributor Author

/gcbrun

@stenalpjolly

Copy link
Copy Markdown
Contributor Author

/gcbrun

@stenalpjolly

Copy link
Copy Markdown
Contributor Author

/gcbrun

@anubhav756 anubhav756 self-assigned this Jun 19, 2026
Comment thread src/main/java/com/google/cloud/mcp/Tool.java Outdated

@anubhav756 anubhav756 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might cause conflicts with #65.

@stenalpjolly

Copy link
Copy Markdown
Contributor Author

I have addressed the PR review comment:

  • Wrapped the finalArgs and extraHeaders maps in Collections.synchronizedMap during tool execution to guarantee thread safety when resolving async OAuth/OIDC credentials concurrently.

@stenalpjolly stenalpjolly force-pushed the stenalpjolly_feature_pre_post_processors branch 4 times, most recently from fa6f3c9 to b6fbb40 Compare June 21, 2026 05:50
@stenalpjolly stenalpjolly force-pushed the stenalpjolly_feature_pre_post_processors branch from b6fbb40 to a2a3a56 Compare June 22, 2026 09:13
@stenalpjolly stenalpjolly changed the title feat: add pre and post processing hooks for tools feat: add pre & post processing hooks for tools Jun 24, 2026
@stenalpjolly stenalpjolly changed the title feat: add pre & post processing hooks for tools feat: add pre and post processing hooks for tools. Jun 24, 2026
@stenalpjolly stenalpjolly changed the title feat: add pre and post processing hooks for tools. feat: add pre and post processing hooks for tools Jun 24, 2026
@stenalpjolly stenalpjolly reopened this Jun 24, 2026
@anubhav756

Copy link
Copy Markdown
Contributor

/gcbrun

@stenalpjolly stenalpjolly reopened this Jun 25, 2026
@stenalpjolly stenalpjolly changed the title feat: add pre and post processing hooks for tools feat: add pre & post processing hooks for tools Jun 25, 2026
@stenalpjolly

Copy link
Copy Markdown
Contributor Author

/gcbrun

@stenalpjolly stenalpjolly merged commit a998703 into googleapis:main Jun 25, 2026
26 checks passed
@stenalpjolly stenalpjolly deleted the stenalpjolly_feature_pre_post_processors branch June 25, 2026 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants