Repository navigation
feat(openhands): add OpenHands target for hooks and global MCP - #4001
Merged
Merged
Conversation
Add a narrow `openhands` target for the two OpenHands surfaces that are
not already covered by `agentsmd` and `agentsskills`:
- Hooks: `.openhands/hooks.json` (project only) with the documented
snake_case event keys and no wrapper. Six events are mapped; import
also accepts PascalCase keys and the `{"hooks": {...}}` wrapper.
Matchers are translated to OpenHands tool names (`Bash` -> `terminal`,
`Write`/`Edit` -> `file_editor|apply_patch`, ...). A `preToolUse`
guard with an untranslatable name falls back to the `*` matcher with a
warning instead of never matching. Handlers with a non-integer
timeout are dropped, since OpenHands rejects the whole file.
`preserveUnownedHooks` is supported.
- MCP: `~/.openhands/mcp.json` (global only). Stdio and http/sse
servers, `disabled` as `enabled: false`; other top-level keys and
unmanaged keys of existing servers are kept, and the file is never
deleted.
Closes #3411
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
- Map Read to file_editor, read_file and planning_file_editor, and Write/Edit/MultiEdit to file_editor, apply_patch, write_file, edit and planning_file_editor (the GPT-5, Gemini and planning presets), plus LS to list_directory, so a write guard no longer fails open there. - Import a tool as every canonical name it covers (file_editor becomes Read|Write|Edit|MultiEdit) and unwrap the explicit /regex/ form. - Fall back to the "*" matcher for a preToolUse guard naming a canonical mcp__ tool, which never matches OpenHands' MCP tool names. - Skip, with a warning, an imported non-tool-event group whose matcher never runs in OpenHands instead of importing it as an always-on hook. - Round a fractional timeout up instead of dropping the hook. - Document that the SDK only falls back to ~/.openhands/hooks.json when the project has no hooks file. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
…ooks
- Warn at generate time when `$OH_PERSISTENCE_DIR/hooks.json` (default
`~/.openhands/hooks.json`) exists: the OpenHands SDK reads it only
when the project has no `.openhands/hooks.json`.
- Remove `.openhands/hooks.json` instead of rewriting it as `{}` once no
hooks remain, through a new opt-in `AiFile.shouldRemoveWhenPayloadEmpty`
honored by the write phase (never creating such a file empty either).
- Treat lowercase variants of canonical names that are not OpenHands
tools (`bash`, `(?i)write`) as unmatchable, so a guard falls back to
`*` with a warning.
- Warn on import about OpenHands-native names (`browser_*`,
`<server>_<tool>`) carried into canonical matchers untranslated.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
…enhands-target # Conflicts: # src/generated/docs-content.ts
…file - Remove an empty-payload file through removeFileWithoutFollowingSymlinks (no symlink below the output root is followed) and report a failure as a warning instead of counting it as removed. - Add AiFile.describeRemovalRefusal so a file can veto that removal; the OpenHands project hooks file refuses when it is the user-level `~/.openhands/hooks.json` itself (generate run from the home directory), and the hidden-user-hooks warning skips that case. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
…enhands-target # Conflicts: # src/generated/docs-content.ts
…enhands-target # Conflicts: # docs/reference/file-formats.md # src/generated/docs-content.ts
…enhands-target # Conflicts: # src/generated/docs-content.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a narrow
openhandstarget for the two OpenHands surfaces thatagentsmd+agentsskillsdo not already reach: project hooks (.openhands/hooks.json) and global MCP servers (~/.openhands/mcp.json). OpenHands reads rules fromAGENTS.mdand skills from.agents/skills/(skills docs), so the target does not write those again; the docs say to useagentsmd/agentsskillsalongside it.Changes
src/features/hooks/openhands-hooks.ts(+ tests):.openhands/hooks.json, project only.preToolUse→pre_tool_use,postToolUse→post_tool_use,beforeSubmitPrompt→user_prompt_submit,stop,sessionStart→session_start,sessionEnd→session_end. Other events are skipped with the usual warning.command,timeout(seconds) andasync.{"hooks": {...}}wrapper, since OpenHands accepts both.src/features/mcp/openhands-mcp.ts(+ tests):~/.openhands/mcp.jsonmcpServers, global only.types/hooks.ts(override keyopenhands.hooks, event maps), the tool-target tuples, tool display, and the hooks/mcp processors. Also updated cspell and.gitignore(viapnpm dev gitignore).file-formats.md: hooks note, a column in the event table, the MCP note, and the override-key andpreserveUnownedHookslists.supported-tools.md: an "OpenHands coverage" section.configuration.mdand theconfig.tscomment: added OpenHands to the list of toolspreserveUnownedHookscovers.Design decisions
Tool-name translation (verified against the SDK source): OpenHands names its tools from the class name (
TerminalTool→terminal, ...). It matches a matcher exactly, or withre.fullmatchwhen the matcher contains regex characters (openhands-sdk/openhands/sdk/hooks/config.py). Every agent preset inopenhands-tools/.../preset/is covered:Bashterminal(the only shell-executing tool)Readfile_editor,read_file,planning_file_editorWrite,Edit,MultiEditfile_editor,apply_patch,write_file,edit,planning_file_editorLSlist_directoryGlob,Grepglob,grepTodoWritetask_trackerTasktaskapply_patchcomes from the GPT-5 preset,read_file/write_file/edit/list_directoryfrom the Gemini preset, andplanning_file_editorfrom the planning preset (it edits PLAN.md). The three write names share one set, because every write tool there can create a file as well as edit one.Import widens: each tool is imported as every canonical name it covers.
file_editorbecomesRead|Write|Edit|MultiEdit, andapply_patchbecomesWrite|Edit|MultiEdit. A regenerated matcher is therefore never narrower than the imported one. The explicit/regex/form is unwrapped on import, since OpenHands full-matches both forms while other tools would read the slashes literally.Fail closed on guards: if a
preToolUsematcher names a capitalized tool with no translation (e.g.WebFetch,Web.*) or a canonicalmcp__...name, the hook is written with the*matcher and a warning, so it runs on every tool instead of never. OpenHands calls an MCP tool by its own name, or<server>_<tool>with several servers (MCPToolDefinition.with_server_prefix). OnpostToolUsesuch names are written as-is with a warning.Matchers on non-tool events (
stop,user_prompt_submit,session_*) are dropped with a warning on generate. OpenHands runs such a hook only when its matcher is*or absent. On import, a*there is stripped silently. A group with any other matcher never runs in OpenHands, so import skips it with a warning rather than turning it into a hook that runs on every event.Invalid timeouts: OpenHands validates
timeoutas an int and would reject the whole file. A fractional positivetimeoutis rounded up. Any other non-integer value is left out, so OpenHands' 60s default applies. Either way there is a warning, and the hook itself is kept.Shared hooks file: by default the generated hooks replace the file, like
.codex/hooks.json.supportsPreserveUnownedis on, so withpreserveUnownedHookshand-written handlers are kept, and PascalCase or wrapped keys are folded into the snake_case ones before merging. A handler identical to a generated one, as happens after import then generate, is not written twice (covered by a test).No global hooks, and user hooks are never hidden silently: the docs list no user-level hooks file, and OpenHands does not search parent folders. The SDK loader does fall back to
$OH_PERSISTENCE_DIR/hooks.json(default~/.openhands/hooks.json), but only when the project has no hooks file.{}, which would keep hiding the user file. WithpreserveUnownedHooks, it is removed only when no hand-written handler is left either.AiFile.shouldRemoveWhenPayloadEmpty(), which the shared write phase honors. It reports the removal in--dry-run/--check, and never creates such a file empty. Every other file defaults tofalse, so their behavior is unchanged.Case variants and native names: in a
preToolUsematcher, a lowercase variant of a canonical name that is not an OpenHands tool (bash,(?i)write) falls back to*with a warning.glob,grep,editandtaskare real OpenHands tools and are kept. On import, OpenHands-native names with no canonical counterpart (browser_*, an MCP<server>_<tool>) are kept as written with a warning.MCP fields:
command/args/env(the documented manual format).url+transport(http, withstreamable-httpfolded into it, orsse),headersandauth: "oauth". These are the fieldsopenhands mcp add --transport/--header/--authstores (checked in the OpenHands-CLImcp_utils.py, which uses FastMCP'sRemoteMCPServer).disabled: truemaps toenabled: false, the flagopenhands mcp disablesets.cwdand WebSocket servers are dropped with a warning.${VAR}references are written literally with a warning, since OpenHands does not document expanding them.MCP file preservation:
mcpServersis replaced. Other top-level keys, and keys rulesync does not manage on a same-named existing server, are kept. An unparseable file is refused rather than overwritten. The file is never deleted, becauseopenhands mcpedits it too.Skipped items
paths:rules-as-skills (optional idea in the issue): skipped. It overlapsagentsskillsand would change skill semantics.hooks/hooks.jsonin OpenHands plugins): out of scope.prompt/agenthook types: the SDK has them, but the user docs say onlycommandis supported.Closes #3411
🤖 Generated with Claude Code
https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4