Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/opencode/src/tool/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Log } from "@/util/log"
import { LspTool } from "./lsp"
import { Truncate } from "./truncation"
import { PlanExitTool, PlanEnterTool } from "./plan"
import { ToolSearchTool } from "./tool-search"

export namespace ToolRegistry {
const log = Log.create({ service: "tool.registry" })
Expand Down Expand Up @@ -108,6 +109,7 @@ export namespace ToolRegistry {
WebSearchTool,
CodeSearchTool,
SkillTool,
ToolSearchTool,
...(Flag.OPENCODE_EXPERIMENTAL_LSP_TOOL ? [LspTool] : []),
...(config.experimental?.batch_tool === true ? [BatchTool] : []),
...(Flag.OPENCODE_EXPERIMENTAL_PLAN_MODE && Flag.OPENCODE_CLIENT === "cli" ? [PlanExitTool, PlanEnterTool] : []),
Expand Down
45 changes: 45 additions & 0 deletions packages/opencode/src/tool/tool-search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import z from "zod"
import { Tool } from "./tool"
import { ToolRegistry } from "./registry"

export const ToolSearchTool = Tool.define("tool_search", {
description: `Search for available tools by name or description.
Use this when you need to find a tool for a specific task.
Returns a list of matching tool names that you can then use.`,
parameters: z.object({
query: z.string().describe("Regex pattern to search tool names and descriptions"),
}),
async execute(params, ctx) {
const tools = await ToolRegistry.tools("", ctx.extra?.agent)
const regex = new RegExp(params.query, "i")

const matches: { id: string; description: string }[] = []
for (const tool of tools) {
if (tool.id === "tool_search" || tool.id === "invalid") continue
if (regex.test(tool.id) || regex.test(tool.description)) {
matches.push({
id: tool.id,
description: tool.description.slice(0, 100),
})
}
}

if (matches.length === 0) {
return {
title: "No tools found",
metadata: { matches: 0 },
output: `No tools found matching "${params.query}"`,
}
}

const output = matches
.map((m) => `- ${m.id}: ${m.description}`)
.join("\n")

return {
title: `Found ${matches.length} tools`,
metadata: { matches: matches.length, tools: matches.map((m) => m.id) },
output: `Found ${matches.length} tool(s) matching "${params.query}":\n${output}`,
}
},
})
Loading