Skip to content

feat(openhands): add OpenHands target for hooks and global MCP - #4001

Merged
dyoshikawa merged 9 commits into
mainfrom
resolve-issue-3411-openhands-target
Nov 26, 2026
Merged

dyoshikawa merged 9 commits into
mainfrom
resolve-issue-3411-openhands-target

Conversation

@dyoshikawa

@dyoshikawa dyoshikawa commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds a narrow openhands target for the two OpenHands surfaces that agentsmd + agentsskills do not already reach: project hooks (.openhands/hooks.json) and global MCP servers (~/.openhands/mcp.json). OpenHands reads rules from AGENTS.md and skills from .agents/skills/ (skills docs), so the target does not write those again; the docs say to use agentsmd/agentsskills alongside it.

Changes

  • src/features/hooks/openhands-hooks.ts (+ tests): .openhands/hooks.json, project only.
    • Native snake_case event keys, no wrapper: 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.
    • Handlers are command-only, with command, timeout (seconds) and async.
    • Import also accepts PascalCase keys and the {"hooks": {...}} wrapper, since OpenHands accepts both.
  • src/features/mcp/openhands-mcp.ts (+ tests): ~/.openhands/mcp.json mcpServers, global only.
  • Registered in types/hooks.ts (override key openhands.hooks, event maps), the tool-target tuples, tool display, and the hooks/mcp processors. Also updated cspell and .gitignore (via pnpm dev gitignore).
  • Docs:
    • file-formats.md: hooks note, a column in the event table, the MCP note, and the override-key and preserveUnownedHooks lists.
    • supported-tools.md: an "OpenHands coverage" section.
    • configuration.md and the config.ts comment: added OpenHands to the list of tools preserveUnownedHooks covers.
    • Regenerated the tables and docs-content.
  • E2E: hooks generate, orphan and import cases, plus the MCP global matrix.

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 with re.fullmatch when the matcher contains regex characters (openhands-sdk/openhands/sdk/hooks/config.py). Every agent preset in openhands-tools/.../preset/ is covered:

    Canonical OpenHands
    Bash terminal (the only shell-executing tool)
    Read file_editor, read_file, planning_file_editor
    Write, Edit, MultiEdit file_editor, apply_patch, write_file, edit, planning_file_editor
    LS list_directory
    Glob, Grep glob, grep
    TodoWrite task_tracker
    Task task

    apply_patch comes from the GPT-5 preset, read_file/write_file/edit/list_directory from the Gemini preset, and planning_file_editor from 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_editor becomes Read|Write|Edit|MultiEdit, and apply_patch becomes Write|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 preToolUse matcher names a capitalized tool with no translation (e.g. WebFetch, Web.*) or a canonical mcp__... 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). On postToolUse such 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 timeout as an int and would reject the whole file. A fractional positive timeout is 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. supportsPreserveUnowned is on, so with preserveUnownedHooks hand-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.

    • When that user file exists, generate warns that the project file makes OpenHands ignore it.
    • Once no OpenHands hooks remain, the project file is removed instead of being rewritten as {}, which would keep hiding the user file. With preserveUnownedHooks, it is removed only when no hand-written handler is left either.
    • The removal goes through a new opt-in 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 to false, so their behavior is unchanged.
  • Case variants and native names: in a preToolUse matcher, a lowercase variant of a canonical name that is not an OpenHands tool (bash, (?i)write) falls back to * with a warning. glob, grep, edit and task are 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:

    • Stdio servers get command/args/env (the documented manual format).
    • Remote servers get url + transport (http, with streamable-http folded into it, or sse), headers and auth: "oauth". These are the fields openhands mcp add --transport/--header/--auth stores (checked in the OpenHands-CLI mcp_utils.py, which uses FastMCP's RemoteMCPServer).
    • disabled: true maps to enabled: false, the flag openhands mcp disable sets.
    • cwd and 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: mcpServers is 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, because openhands mcp edits it too.

Skipped items

  • paths: rules-as-skills (optional idea in the issue): skipped. It overlaps agentsskills and would change skill semantics.
  • Plugin hooks (hooks/hooks.json in OpenHands plugins): out of scope.
  • prompt/agent hook types: the SDK has them, but the user docs say only command is supported.

Closes #3411

🤖 Generated with Claude Code

https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4

dyoshikawa and others added 9 commits November 26, 2026 17:44
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
@dyoshikawa
dyoshikawa merged commit ccdda53 into main Nov 26, 2026
10 checks passed
@dyoshikawa
dyoshikawa deleted the resolve-issue-3411-openhands-target branch November 26, 2026 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Propose an OpenHands target: .openhands/hooks.json and ~/.openhands/mcp.json

1 participant