/claude-code-superpowers:build-tool-factoryfrom claude-code-superpowers
Teaches how to create a new Claude Code tool using the buildTool() factory from src/Tool.ts. Use when adding capabilities to Claude Code.
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.
Factory functions for capability objects solve a common problem: a typed interface has many methods, but any given implementation only needs to override a few. Listing all methods explicitly creates noise and makes the "what does this tool actually do" harder to see. A factory fills in safe defaults and lets the author focus on the non-trivial fields.
The pattern has three parts: (1) a minimal definition type (ToolDef) where defaultable methods are optional, (2) a factory (buildTool) that merges your definition with safe defaults, and (3) lazy schema getters so schemas are built on first access, not at module load time.
// DefaultableToolKeys — methods buildTool fills in with safe defaults
type DefaultableToolKeys =
| 'isEnabled'
| 'isConcurrencySafe'
| 'isReadOnly'
| 'isDestructive'
| 'checkPermissions'
| 'toAutoClassifierInput'
| 'userFacingName'
// ToolDef — what you pass to buildTool (same as Tool but defaultable methods are optional)
export type ToolDef<
Input extends AnyObject = AnyObject,
Output = unknown,
P extends ToolProgressData = ToolProgressData,
> = Omit<Tool<Input, Output, P>, DefaultableToolKeys> &
Partial<Pick<Tool<Input, Output, P>, DefaultableToolKeys>>
// buildTool fills in safe defaults for all DefaultableToolKeys
export function buildTool<D extends ToolDef>(def: D): BuiltTool<D>
| Default key | Safe default value |
|---|---|
isConcurrencySafe | () => false |
isReadOnly | () => false |
isDestructive | () => false |
checkPermissions | { behavior: 'allow' } |
isEnabled | () => true |
userFacingName | derived from name |
Claude Code has ~30 tools. Each shares the same set of safety defaults: assume not concurrent-safe, assume not read-only, assume not destructive, assume checkPermissions passes through. Without a factory, every new tool must explicitly declare all of these even when the intent is "no override". The factory encodes the security-safe posture as the default: new tools are restrictive until explicitly opted out.
The alternative — export const X: Tool<typeof inputSchema> = { ... } — forces the author to repeat every defaultable method for each tool. It also makes new tools opt-in to safety (you must explicitly set isDestructive: () => false) rather than opt-out (the factory sets it for you and you override only when needed).
src/tools/[Name]Tool/[Name]Tool.ts (or .tsx if the tool renders JSX output)inputSchema with lazySchema(() => z.strictObject({...})) — use z.strictObject to reject unknown keysoutputSchema the same way when the tool returns structured data the LLM needs to inspectbuildTool({ name, inputSchema getter, call, ... }) — only override methods that differ from the defaultsisConcurrencySafe and isReadOnly if your tool is side-effect-free (defaults are false)checkPermissions if your tool accesses files, shell, or networksrc/tools.ts// Source: src/tools/FileReadTool/FileReadTool.ts
import { buildTool, type ToolDef } from '../../Tool.js'
import { lazySchema } from '../../utils/lazySchema.js'
import { z } from 'zod/v4'
const inputSchema = lazySchema(() =>
z.strictObject({
file_path: z.string().describe('Absolute path to the file'),
limit: z.number().int().positive().optional().describe('Max lines'),
})
)
type InputSchema = ReturnType<typeof inputSchema>
export type Input = z.infer<InputSchema>
export const FileReadTool = buildTool({
name: FILE_READ_TOOL_NAME,
searchHint: 'read files, images, PDFs, notebooks', // helps ToolSearch find deferred tools
strict: true, // rejects unknown input fields
async description() { return 'Read a file' },
// Lazy getter — inputSchema() is not called at module load time
get inputSchema(): InputSchema { return inputSchema() },
// Overrides the false default — this tool is safe to run concurrently
isConcurrencySafe() { return true },
// Overrides the false default — this tool makes no mutations
isReadOnly() { return true },
// checkPermissions is called by the framework BEFORE call() — not inside call()
async checkPermissions(input, context): Promise<PermissionDecision> {
return checkReadPermissionForTool(FileReadTool, input, context.getAppState().toolPermissionContext)
},
async call({ file_path, limit }, context, _canUseTool?) {
const content = await readFileInRange(file_path, 1, limit)
return { data: { type: 'text' as const, file: { filePath: file_path, content } } }
},
})
Notice isConcurrencySafe and isReadOnly are the only defaultable methods overridden here — everything else (isDestructive, isEnabled, userFacingName, toAutoClassifierInput) is left to buildTool's safe defaults.
Before — a new capability added as a plain function (no schema, no factory, no lazy schema):
// No schema, no permission check, no lazy loading
async function summariseAsset(assetId: string, depth: number): Promise<string> {
const asset = await fetchAsset(assetId)
return buildSummary(asset, depth)
}
After — same capability as a proper buildTool({...}) with lazySchema, checkPermissions, and { data: result } return:
import { buildTool } from '../../Tool.js'
import { lazySchema } from '../../utils/lazySchema.js'
import { z } from 'zod/v4'
const inputSchema = lazySchema(() =>
z.strictObject({
asset_id: z.string().describe('Qualified name or GUID of the asset'),
depth: z.number().int().min(1).max(3).optional().default(1)
.describe('How many relationship hops to include'),
})
)
type InputSchema = ReturnType<typeof inputSchema>
export const SummariseAssetTool = buildTool({
name: SUMMARISE_ASSET_TOOL_NAME,
async description() { return 'Summarise an Atlan asset and its relationships' },
get inputSchema(): InputSchema { return inputSchema() },
// Read-only network call — safe to run concurrently
isConcurrencySafe: () => true,
isReadOnly: () => true,
async checkPermissions(input, context) {
return checkNetworkPermission(SummariseAssetTool, input, context)
},
async call({ asset_id, depth }, context) {
const asset = await fetchAsset(asset_id, context.getAppState().atlanClient)
const summary = buildSummary(asset, depth)
return { data: { summary, assetId: asset_id } }
},
})
export const X: Tool<typeof inputSchema> = { ... } (direct interface implementation — bypasses the factory and forces explicit defaults)inputSchema is a bare z.object({...}) at module scope instead of wrapped in lazySchema() — schema is built at import time, hurting startupz.object instead of z.strictObject — unknown input fields will silently pass through validationbuildTool for test helpers or mock tools — it's for production tools registered in tools.tsoutputSchema for tools that return plain strings or simple unstructured datasearchHint is only needed for large tools likely to be deferred by ToolSearch; small tools don't need ittool-definition — the full Tool interface and what each method doespermission-system — implementing checkPermissions for tools with side effectshot-paths — why lazySchema() matters for startup performancemodule-organisation — where to put the new tool file and how to register itWhat 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.
/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-modelGenerated 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