[PR #6544] feat(plugin): add ToolResult type for structured tool responses #11965

Open
opened 2026-02-16 18:16:54 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6544

State: open
Merged: No


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

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6544 **State:** open **Merged:** No --- ## 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 ```typescript 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 - Resolves #7590
yindo added the pull-request label 2026-02-16 18:16:54 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11965