/claude-code-superpowers:tool-definitionfrom claude-code-superpowers
Teaches the complete pattern for defining a Claude Code Tool: schema-first design with Zod, the buildTool factory for safe defaults, checkPermissions for side-effect gates, progress callbacks for streaming, and returning { data: T } from call().
Ships in claude-code-superpowers. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
A Tool is defined by calling buildTool({ ... }), which fills in safe defaults for isEnabled, isConcurrencySafe, isReadOnly, isDestructive, checkPermissions, and other housekeeping methods. The Input is a Zod schema exposed via a get inputSchema() getter. The Output is the TypeScript type of data inside the result. These compose: the Zod schema drives runtime validation AND TypeScript types simultaneously, eliminating a class of bugs where validation and types diverge.
The key insight: buildTool factory + checkPermissions method + typed throw for errors. Permissions are not checked inside call() — the framework calls checkPermissions(input, context) before dispatching to call(). Errors are thrown (not returned), and the framework formats them for the LLM.
The LLM produces tool calls as JSON objects. Claude Code must: (1) validate the JSON against a schema, (2) check if the user permits this call, (3) execute the tool, (4) return a structured result back to the LLM. These four steps must be explicit and in order.
If a tool checked permissions after execution (or not at all), security would be broken. If validation happened after permission checking, the LLM could construct inputs that bypass validation for permitted tools. The pattern enforces the correct order: schema validation happens automatically when the framework calls the tool; checkPermissions is called by the framework before call() is ever reached; call() can assume permission is already granted.
Using Zod as the schema source of truth means the LLM's JSON schema (shown in the system prompt) and the runtime validator are the same artifact. When you change one, you change both.
inputSchema using lazySchema(() => z.strictObject({ ... })). Add .describe() to each field — these become the LLM's parameter documentation.isConcurrencySafe(input) and isReadOnly(input). These are called with the actual input, so you can make them input-dependent (e.g., a bash tool is concurrent-safe only for read commands).checkPermissions() for side-effect tools — the framework calls it before call(). Return a PermissionDecision indicating whether execution is allowed.{ data: result } from call() on success; throw for errors (the framework formats thrown errors for the LLM).onProgress to stream partial results to the UI. Call it with typed progress objects as work proceeds.// Source: src/tools/FileReadTool/FileReadTool.ts (real file, real pattern)
import { buildTool, type ToolDef } from '../../Tool.js'
import { lazySchema } from '../../utils/lazySchema.js'
const inputSchema = lazySchema(() =>
z.strictObject({
file_path: z.string().describe('The absolute path to the file to read'),
offset: z.number().int().nonnegative().optional().describe('Line number to start reading from'),
limit: z.number().int().positive().optional().describe('Number of lines to read'),
})
)
type InputSchema = ReturnType<typeof inputSchema>
export const FileReadTool = buildTool({
name: FILE_READ_TOOL_NAME,
searchHint: 'read files, images, PDFs, notebooks',
strict: true,
async description() { return DESCRIPTION },
// Zod schema exposed as a getter — evaluated lazily on first access
get inputSchema(): InputSchema { return inputSchema() },
isConcurrencySafe() { return true },
isReadOnly() { return true },
// Framework calls this BEFORE call() — tool never needs to re-check
async checkPermissions(input, context): Promise<PermissionDecision> {
return checkReadPermissionForTool(FileReadTool, input, appState.toolPermissionContext)
},
async call({ file_path, offset = 1, limit }, context, _canUseTool?) {
// Permission already granted by the time we reach here
const content = await readFile(file_path, { offset, limit })
// Return { data: T } on success — throw on error
return { data: { type: 'text', file: { filePath: file_path, content, numLines: limit } } }
},
})
Notice that description() is async and returns a string rather than a static property — this lets the UI show "Read src/main.tsx" instead of "Read a file". _canUseTool in call() is available only for forwarding to sub-tools; most tools ignore it entirely.
Before — capability implemented as an unstructured function:
// No schema, no permission check, throws on error, no streaming
async function searchFiles(pattern: string, dir: string): Promise<string[]> {
const results = await glob(pattern, { cwd: dir })
if (results.length === 0) {
throw new Error(`No files matching ${pattern}`)
}
return results
}
After — same capability as a proper Tool:
import { z } from 'zod'
import { buildTool } from '../../Tool.js'
import { lazySchema } from '../../utils/lazySchema.js'
const inputSchema = lazySchema(() =>
z.strictObject({
pattern: z.string().describe('Glob pattern to match (e.g. **/*.ts)'),
path: z.string().optional().describe('Directory to search in. Defaults to cwd.'),
})
)
type InputSchema = ReturnType<typeof inputSchema>
export const GlobTool = buildTool({
name: GLOB_TOOL_NAME,
searchHint: 'find files by glob pattern',
async description(input) {
return `Find files matching ${input.pattern}${input.path ? ` in ${input.path}` : ''}`
},
get inputSchema(): InputSchema { return inputSchema() },
// Glob is pure read — safe to run concurrently with other read operations
isConcurrencySafe: () => true,
isReadOnly: () => true,
// Read-only tools typically need no permission check; buildTool supplies a permissive default.
// For a tool with side effects, implement checkPermissions() here instead.
async call(args, context) {
// By the time call() runs, permissions are already settled
const cwd = args.path ?? context.getAppState().cwd
const results = await glob(args.pattern, { cwd })
// Return empty array on no matches — not an error; let the LLM decide
// Throw on unexpected failures — framework formats the error for the LLM
return { data: results }
},
})
process.env or global singletons instead of context.getAppState()canUseTool inside call() instead of implementing checkPermissions()isConcurrencySafe always returns false without examining the inputcall() implementations don't need schemasisDestructive for every tool — only tools that perform irreversible mutationsdomain-model — explains where Tools fit in the full systempermission-system — how checkPermissions evaluates and what happens after denialasync-concurrency — how isConcurrencySafe affects the execution schedulererror-handling — full error propagation philosophy for tool resultsWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
16 skills in this plugin
/async-concurrencyTeaches Claude Code's concurrency model: isConcurrencySafe() for parallel tool execution, AbortController propagation for cancellation, Promise.all() for parallel initialization, and the StreamingToolExecutor for concurrent progress streams. Use this when writing tools that could benefit from parallel execution, need to respect cancellation, or must stream partial results. Read-only tools that don't declare concurrency safety are needlessly serialized.
/build-tool-factoryTeaches how to create a new Claude Code tool using the buildTool() factory from src/Tool.ts.
Use this whenever adding a new capability to Claude Code — buildTool is the only correct entry
point. It fills in safe defaults for isConcurrencySafe, isReadOnly, isDestructive,
checkPermissions, and userFacingName so you only implement what your tool actually needs.
/creating-skillsUse when extracting an engineering pattern from a codebase and packaging it as a skill for an AI assistant. Triggered when: converting a recurring code pattern into a SKILL.md, wanting your AI to apply a specific pattern automatically, or validating whether an existing skill is good enough to ship. Covers structure, the real-code requirement, the two-layer generic/specific design, and six quality tests.
/domain-modelTeaches Claude Code's core domain model: Tool, Message, Task, ToolUseContext, AppState, and Command — the six concepts every part of the system is built around. Use this before writing any new tool, command, or feature that touches the conversation loop. Essential for understanding how Claude's capability system is structured and why the abstractions exist.
/error-handlingTeaches Claude Code's error philosophy: tool call() throws typed errors, the framework catches them and formats them for the LLM as structured tool result content, and custom error types carry structured context at system boundaries. Use this whenever writing a tool, utility function, or service that can fail. The distinction between "errors the LLM should see" and "errors that crash the session" is the core insight.
/hot-pathsTeaches Claude Code's startup and runtime performance patterns: lazySchema() for deferred Zod parsing, feature() flags for build-time dead code elimination, memoize() for expensive repeated computations, parallel initialization with Promise.all(), and profileCheckpoint() for tracking load times. Use this whenever writing code that runs at startup, on every turn, or in a hot loop. Startup latency is Claude Code's most user-visible performance metric.
Generated from the plugin's indexed repository (techymt/claude-code-superpowers) — no native marketplace selected
npx claudepluginhub p/techymt-claude-code-superpowersOther plugins' skills for the same job