npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

tool-wrap

v0.1.0

Published

Runtime repair layer for Anthropic tool-calling failures

Downloads

156

Readme

tool-wrap

Runtime repair layer for Anthropic tool-calling failures.

LLM tool-call failures are overwhelmingly a harness problem, not a model capability problem. This library sits between the model's raw output and your tool implementation: it validates input with Zod, attempts a small set of well-known shape repairs, and returns a model-readable error string when repair is not possible.

Install

npm install tool-wrap
# peer dependency
npm install zod

Usage

import Anthropic from "@anthropic-ai/sdk"
import { z } from "zod"
import { wrap, pathString } from "tool-wrap"

// Default: Anthropic format
const readFile = wrap({
  name: "read_file",
  description: "Read the contents of a file",
  schema: z.object({
    path: pathString(),           // unwraps markdown auto-links
    limit: z.number().optional(),
    offset: z.number().optional(),
  }),
  execute: async ({ path, limit, offset }) => {
    // your implementation
  },
  relationalDefaults: [
    { ifPresent: "limit", thenDefault: { offset: 0 } },
  ],
  logger: console,
})

// OpenAI / DeepSeek format — pass openaiAdapter
import { openaiAdapter } from "tool-wrap"
import OpenAI from "openai"

const readFileOAI = wrap({
  name: "read_file",
  description: "Read the contents of a file",
  schema: z.object({ path: pathString(), limit: z.number().optional() }),
  execute: async ({ path, limit }) => { /* your implementation */ },
  adapter: openaiAdapter,
})

// Pass definition to Anthropic
const client = new Anthropic()
const response = await client.messages.create({
  model: "claude-opus-4-7",
  tools: [readFile.definition],   // Anthropic.Tool shape
  messages: [...],
})

// Or pass to DeepSeek / OpenAI
const deepseek = new OpenAI({ baseURL: "https://api.deepseek.com", apiKey: "..." })
const response2 = await deepseek.chat.completions.create({
  model: "deepseek-chat",
  tools: [readFileOAI.definition],  // OpenAI ChatCompletionTool shape
  messages: [...],
})

// Handle tool_use blocks
for (const block of response.content) {
  if (block.type === "tool_use" && block.name === "read_file") {
    const result = await readFile.execute(block.input)
    // result is your tool's return value, or a model-readable error string
  }
}

What it repairs

Four shape errors cover ~90% of tool-call failures across open models:

| # | Failure | Example | Repair | |---|---------|---------|--------| | 2 | JSON array emitted as string | "[\"a\",\"b\"]" | parse stringified JSON | | 4 | Bare string where array expected | "foo" | wrap to ["foo"] | | 1 | null for optional field | { limit: null } | drop the key | | 3 | Single object where array expected | { id: 1 } | wrap to [{ id: 1 }] |

Repairs run in the order shown (#2 before #4 so stringified arrays are parsed, not re-wrapped). Valid inputs are never touched.

API

wrap(options)

wrap({
  name: string
  description: string
  schema: ZodObject           // Zod schema; input_schema is derived from this
  execute: (input) => Promise<unknown>
  relationalDefaults?: Array<{ ifPresent: string; thenDefault: Record<string, unknown> }>
  logger?: { info(...): void; warn(...): void }  // optional; pino, winston, console, etc.
  adapter?: ToolAdapter<TDef> // optional; defaults to anthropicAdapter
}): {
  definition: TDef            // shape determined by adapter (Anthropic.Tool by default)
  execute: (rawInput: unknown) => Promise<unknown>
}

Two built-in adapters are exported:

import { anthropicAdapter, openaiAdapter } from "tool-wrap"
// anthropicAdapter → { name, description, input_schema }
// openaiAdapter    → { type: "function", function: { name, description, parameters } }

Custom adapters implement ToolAdapter<TDef>:

import type { ToolAdapter, BaseDefinition } from "tool-wrap"

const myAdapter: ToolAdapter<MyProviderTool> = (base: BaseDefinition) => ({
  // map base.name, base.description, base.inputSchema to your provider's format
})

On unrecoverable failure, execute returns a model-readable string (no Error: prefix) so the model can self-correct and retry.

pathString()

A Zod type that unwraps markdown auto-links before validation:

"[notes.md](http://notes.md)"  →  "notes.md"

Use it for any field that accepts a file path.

Logging

Pass any logger with .info() and .warn() methods. Two events are emitted:

| Event | Level | When | |-------|-------|------| | tool_input_repaired | info | repair succeeded; includes { tool, repairs } | | tool_input_invalid | warn | unrecoverable; includes { tool, issues } | | tool_relational_defaults_applied | info | defaults were injected; includes { tool, defaults } |