Skip to content

fix(session): fire tool.execute.after hook after MCP output assembly - #21150

Closed
YumaKakuya wants to merge 1 commit into
anomalyco:devfrom
sorted-ai:fix/mcp-tool-execute-after-timing
Closed

YumaKakuya wants to merge 1 commit into
anomalyco:devfrom
sorted-ai:fix/mcp-tool-execute-after-timing

Conversation

@YumaKakuya

Copy link
Copy Markdown

Issue for this PR

Closes #21149

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

tool.execute.after fires for MCP tools before the output text is assembled from the raw CallToolResult.content[] array. Plugins receive the raw SDK result instead of the {title, output, metadata} shape declared in the Hooks type definition.

Any plugin that tries to transform output.output gets undefined, and the text assembly at L502-536 rebuilds the output from result.content independently of any hook mutations. The hook is effectively a no-op for MCP tool output transformation.

This moves the trigger call to after text assembly and truncation, passing an assembled object that matches the declared type contract. The return statement references the assembled object so plugin mutations are reflected in the final tool output.

How did you verify your code works?

  • Typecheck: all packages pass (the npm-package-arg error in shared.ts is pre-existing on dev)
  • The change is limited to MCP tool execution in prompt.ts. Native tools (bash, read, etc.) are unaffected — they have their own hook paths
  • Confirmed that the assembled object shape matches the Hooks type definition in packages/plugin/src/index.ts

Screenshots / recordings

N/A — no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

The tool.execute.after hook fires for MCP tools before the output text
is assembled from the raw CallToolResult.content[] array. Plugins
receive the raw SDK result instead of the {title, output, metadata}
shape declared in the Hooks type definition.

Any plugin that transforms output.output (e.g. masking sensitive data)
has no effect because:
1. The output property does not exist on the raw result at hook time
2. The text assembly at L502-536 rebuilds output from result.content
   independently of any hook mutations

This moves the trigger call to after text assembly and truncation,
passing an assembled object that matches the declared type contract.
The return statement references the assembled object so plugin
mutations are reflected in the final tool output.

Closes #21149
@rekram1-node

Copy link
Copy Markdown
Collaborator

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tool.execute.after hook receives raw MCP result instead of assembled output

2 participants