Build a Node.js CLI that generates package.json via Claude's tool-calling API
Author スシロー walks through building `pkginit`, a reusable Node.js CLI that prompts for three inputs, calls the Claude API using tool-calling to generate a validated `package.json`, and writes it to disk only after schema validation passes.
Score breakdown
Using Claude's tool-calling with a strict `input_schema` eliminates the markdown-fence JSON parsing failure mode that plagues free-text LLM output, making AI-generated config files reliably writable to disk without a fragile `JSON.parse` step.
- 01The `pkginit` CLI asks three questions (package name, description, stack/tools) and calls the Claude API to draft a `package.json`.
- 02Tool-calling with an `input_schema` is used instead of free-text JSON prompting, so `tool_use.input` arrives as a pre-parsed object — no `JSON.parse` required.
- 03Two failure modes are documented: a markdown-fenced response breaking `JSON.parse`, and an infinite retry loop firing back-to-back API calls.
スシロー's tutorial presents `pkginit` as a reusable scaffold for AI micro-tools: a Node.js CLI that collects a package name, one-line description, and stack notes from the user, then calls the Claude API to produce a complete `package.json` with pinned-range dependencies, scripts, an `engines.node` range, and the correct `type`/`exports` shape for ESM. The core architectural decision is using Claude's tool-calling feature — defining a `PKG_TOOL` with a strict `input_schema` — rather than prompting the model to emit raw JSON text. Because `tool_use.input` arrives as a pre-parsed object validated by the SDK, there is no `JSON.parse` of free text anywhere in the pipeline, which the author describes as deleting "an entire category of bug."
The post explicitly names three current model IDs as of mid-2026: `claude-opus-4-8` (strongest), `claude-sonnet-4-6` (balanced, and the recommended choice for this task), and `claude-haiku-4-5-20251001` (cheap/fast). The CLI itself is built with `node:readline/promises` to keep the dependency tree thin, and includes a pre-write validation step that checks for a valid `name` and semver-shaped `version` before touching disk, as well as an overwrite confirmation if a `package.json` already exists. The article also documents two specific failure modes encountered during development — a markdown-fenced model response that broke `JSON.parse`, and an infinite retry loop that fired back-to-back API calls — and frames the overall pattern as "Claude proposes, your code disposes." The second half of the article (truncated in the source) covers repurposing the scaffold for other AI micro-tools.
Key facts
- 01The `pkginit` CLI asks three questions (package name, description, stack/tools) and calls the Claude API to draft a `package.json`.
- 02Tool-calling with an `input_schema` is used instead of free-text JSON prompting, so `tool_use.input` arrives as a pre-parsed object — no `JSON.parse` required.
- 03Two failure modes are documented: a markdown-fenced response breaking `JSON.parse`, and an infinite retry loop firing back-to-back API calls.
- 04Recommended model for this task is `claude-sonnet-4-6`; `claude-opus-4-8` and `claude-haiku-4-5-20251001` are named as the stronger and cheaper alternatives.
- 05The CLI uses `node:readline/promises` (no third-party prompt library) and `@anthropic-ai/sdk@^0.65.0`.
- 06A validation step checks for a valid `name` and semver-shaped `version` before writing to disk, and prompts for confirmation if `package.json` already exists.
- 07The scaffold is presented as a reusable template the author copies when starting new AI micro-tools.
Topics
Summary and scoring are generated automatically from the original article. We always link back to the publisher and never republish images or paywalled content. Last processed Jun 11, 2026 · 08:34 UTC. How this works →