Optional typed hook input: generated converter for mcp_tool placeholder strings

Ouverte
#21 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
5/5
Temps estimé
Plus d'une semaine
Accessibilité débutants
35/100
Type d'issue
Fonctionnalité
Clarté
Clairement spécifiée
Activité
Active
Stack technique
typescript

Piste de recherche

Start with packages/core/src/hook-placeholders.ts, renderHookInputTypes, the hooks template parsing, and the existing generated goldens and fixtures. Read the originating PR #20 for the current hook input types, then trace how loadProject checks generated output against the template. Done means the opt-in converter and typed interface are generated, conversion and invalid .d.ts cases are covered, existing goldens stay unchanged with the option off, and the live Claude Code check passes.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

enhancement help wanted

Problem

Claude Code substitutes every ${...} placeholder in an mcp_tool hook's input as a string, whatever type the hook input field has. A live test with Claude Code 2.1.332 (PostToolUse on Edit|Bash, both an open and a typed MCP inputSchema) gave:

Placeholder Field type Value the MCP tool received
${duration_ms} number "29"
${tool_input.replace_all} boolean "true"
${tool_input.timeout} number "5000"
${tool_input}, ${tool_response} object the object as a JSON string
a field that is not present — ""

A number/boolean/integer in the tool's inputSchema does not change this; there is no coercion. Literal values (5, true) in the template keep their type.

The generated McpToolHookInputs therefore correctly types placeholder values as string (see #20). Every MCP tool has to convert "29", "true" or a JSON string back by hand.

Proposal

An opt-in second generated type plus a converter. An MCP tool then passes its raw arguments through one function and gets correctly typed fields:

import { typedHookInput } from "./hook-types.js";

const input = typedHookInput("plugin:my-plugin:checker", "check_path", args);
// input.duration: number | undefined, input.replace_all: boolean | undefined
Conversion rules

Only a value that is exactly one placeholder can be typed:

Template value Field type Converted to Generated type
"${x}" number Number(v); "" or not finite → undefined number | undefined
"${x}" boolean "true"/"false" → boolean; "" → undefined boolean | undefined
"${x}" object/array JSON.parse(v); "" → undefined unknown
"${x}" string or unknown unchanged string
mixed ("x-${a}", "${a}${b}") any unchanged string
literal (5, true, nested objects) — unchanged literal type, as today
Prerequisite: a type per field

The placeholder allow-list in packages/core/src/hook-placeholders.ts knows field names only. It needs a type per field:

  • Event fields (duration_ms: number, is_interrupt: boolean, stop_hook_active: boolean, …): hand-maintained from the hooks reference, like the current allow-list.
  • tool_input.*: from the input_schema types that Claude Code sends to the model. Capture them as #20 did, with ANTHROPIC_BASE_URL pointing to a local logger and ENABLE_TOOL_SEARCH=false. Examples: timeout: number, run_in_background: boolean, replace_all: boolean, offset/limit: number.
  • Open sub-paths or unknown fields: string.

Both tables record the Claude Code version they were verified against.

Generated output
  • McpToolHookTypedInputs, next to McpToolHookInputs.
  • A generated per-tool, per-field table: "number" | "boolean" | "json".
  • A small generated typedHookInput(server, tool, input) with a generic signature, mapping McpToolHookInputs[S][T] to McpToolHookTypedInputs[S][T].
  • The generated code is self-contained: there is no runtime import from @kwitsch/claude-code-plugin-core, so core stays a build-time package.
Design points
  • The option belongs in the hooks template, not only on the CLI. The build already compares the file with the template output (hook-types-stale in loadProject). A CLI-only --typed flag would make the build and the CLI disagree. Possible shapes: "hookTypes": { "file": "src/hook-types.ts", "converter": true }, or a separate hookTypesConverter: true field.
  • No .d.ts target with the converter. The converter is runtime code. With it enabled, a .d.ts target is a new template-invalid case.
  • Conflicting templates. When a tool is called from several hooks whose templates map the same key to different types, the converter cannot know which hook called it. That key stays string, with a warning issue.
  • Empty strings. A real "" for a string field cannot be told apart from a missing field. String fields therefore stay string, never undefined.
  • Version drift. Like the allow-list, the type table is tied to one Claude Code version and needs an update on version bumps.

Scope

  • core: field-type tables; a field-to-type lookup next to allowed; the converter and typed interface in renderHookInputTypes behind the option; parsing of the new template field.
  • Tests, the cli and core READMEs, and a fixture with the option enabled. Existing goldens stay unchanged, because the option is off by default.
  • Changeset: minor for core and cli.
  • A final live check: a test plugin in a real claude -p session, comparing the converted values with the generated types.

Acceptance

  • With the option off, every existing golden is unchanged.
  • With the option on, a fixture generates McpToolHookTypedInputs and typedHookInput, and unit tests cover each row of the conversion table.
  • A .d.ts hook types target combined with the converter reports template-invalid.
  • The live check in a real claude -p session shows converted values that match the generated types.

Relations

  • Originating PR: #20
Langage dominant
TypeScript
Étoiles
0
Forks
0
Merge moyen
29 min
PR mergées (30 j)
138

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de kwitsch/claude-code-plugin-tooling

Toutes les issues de kwitsch/claude-code-plugin-tooling

Issues similaires

Plus d'issues TypeScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.