Skip to content

Conversation

@aryasaatvik
Copy link
Contributor

@aryasaatvik aryasaatvik commented Dec 31, 2025

Problem

Plugin tools could only return a plain string. This prevented:

  • Structured responses with titles, metadata, and attachments
  • Real-time streaming updates during long-running operations
  • Subagent result propagation (tool counts, progress, etc.)

Solution

Add ToolResult interface and metadata() method to plugin SDK:

  • ToolResult interface: { title, metadata, output, attachments? }
  • ctx.metadata(): Emit streaming updates during execution
  • Backward compatible: tools can still return plain strings
  • Core extraction: Tool.Result interface for unified typing

Changes

  • packages/plugin/src/tool.ts - Add ToolResult interface, metadata() method
  • packages/opencode/src/tool/tool.ts - Extract Tool.Result interface
  • packages/opencode/src/tool/registry.ts - Handle structured results in fromPlugin

Usage

import { tool, type ToolResult } from "@opencode-ai/plugin"

export default tool({
  description: "My tool",
  args: { input: z.string() },
  async execute(args, ctx): Promise<ToolResult> {
    ctx.metadata({ title: "Processing...", metadata: { step: 1 } })
    // ... work ...
    return {
      title: "Done",
      metadata: { count: 42 },
      output: "Result text"
    }
  }
})

Related

@aryasaatvik aryasaatvik changed the title fix: preserve plugin tool metadata on completion feat(plugin): support structured tool results with metadata Jan 1, 2026
@aryasaatvik aryasaatvik changed the title feat(plugin): support structured tool results with metadata feat(plugin): add ToolResult type for structured tool responses Jan 4, 2026
@aryasaatvik
Copy link
Contributor Author

@rekram1-node can you take a look?

@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

- Add ToolResult interface to @opencode-ai/plugin
- Update execute signature to Promise<string | ToolResult>
- Enables plugin tools to return {title, metadata, output}
- Add ToolResult interface to @opencode-ai/plugin for structured tool responses
- Update execute signature to accept string | ToolResult
- Add runtime detection in fromPlugin to preserve title and metadata
- Extract Tool.Result interface for clarity in opencode package
- Maintain backward compatibility with string-only results
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.

feat(plugin): add ToolResult type for structured tool responses

1 participant