Optional typed hook input: generated converter for mcp_tool placeholder strings
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 功能
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- typescript
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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 theinput_schematypes that Claude Code sends to the model. Capture them as #20 did, withANTHROPIC_BASE_URLpointing to a local logger andENABLE_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 toMcpToolHookInputs.- A generated per-tool, per-field table:
"number" | "boolean" | "json". - A small generated
typedHookInput(server, tool, input)with a generic signature, mappingMcpToolHookInputs[S][T]toMcpToolHookTypedInputs[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-staleinloadProject). A CLI-only--typedflag would make the build and the CLI disagree. Possible shapes:"hookTypes": { "file": "src/hook-types.ts", "converter": true }, or a separatehookTypesConverter: truefield. - No
.d.tstarget with the converter. The converter is runtime code. With it enabled, a.d.tstarget is a newtemplate-invalidcase. - 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 staystring, neverundefined. - 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 inrenderHookInputTypesbehind 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 -psession, 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
McpToolHookTypedInputsandtypedHookInput, and unit tests cover each row of the conversion table. - A
.d.tshook types target combined with the converter reportstemplate-invalid. - The live check in a real
claude -psession shows converted values that match the generated types.
Relations
- Originating PR: #20
- 主要语言
- TypeScript
- 星标
- 0
- 派生
- 0
- 平均合并
- 29 分钟
- 30 天内合并 PR
- 138
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
kwitsch/claude-code-plugin-tooling 的其他 Issue
-
enhancement
难度 2/5 1-3 小时 新手友好度 74/100
kwitsch/claude-code-plugin-tooling#219 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 69/100
kwitsch/claude-code-plugin-tooling#210 ·
维护者通常 1 天内回复
-
enhancement
难度 4/5 3-5 天 新手友好度 54/100
kwitsch/claude-code-plugin-tooling#212 ·
维护者通常 1 天内回复
-
bug
难度 4/5 3-5 天 新手友好度 48/100
kwitsch/claude-code-plugin-tooling#209 ·
维护者通常 1 天内回复
-
bug
难度 4/5 3-5 天 新手友好度 45/100
kwitsch/claude-code-plugin-tooling#208 ·
维护者通常 1 天内回复
查看 kwitsch/claude-code-plugin-tooling 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 67/100
makecindy/cindy#15342 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
no-repo-link tool-submission
难度 1/5 1 小时以内 新手友好度 74/100
BraveOPotato/NoSignups#2618 ·
-
enhancement good first issue
难度 2/5 1-3 小时 新手友好度 85/100
-
bug needs-triage
难度 2/5 1-3 小时 新手友好度 72/100
Priyanshu-byte-coder/devtrack#4067 ·
维护者通常 1 天内回复