Class: RosettAi::Mcp::Tools::HookInstallTool

Inherits:
Object
  • Object
show all
Defined in:
lib/rosett_ai/mcp/tools/hook_install_tool.rb

Overview

MCP tool: install enforcement hook + register in settings.json.

MUTATION tool — writes hook script to disk and updates Claude Code settings.json. Auto-backs up affected files before writing. Confirmation is required by default.

Author:

  • hugo

  • claude

Constant Summary collapse

TOOL_NAME =
'rai_hook_install'
DESCRIPTION =
'Install enforcement hook: generate script, write to disk, register in settings'
ANNOTATIONS =
{
  'readOnlyHint' => false,
  'destructiveHint' => true,
  'idempotentHint' => true,
  'openWorldHint' => false
}.freeze
HOOK_DIR_NAME =
'hooks'
HOOK_FILENAME =
'rai-enforce.rb'
BACKUP_SUFFIX =
'.rai-backup'

Instance Method Summary collapse

Instance Method Details

#call(behaviour_name: nil, rule_id: nil, scope: 'global', confirm: false) ⇒ Hash

Installs the enforcement hook.

Parameters:

  • behaviour_name (String, nil) (defaults to: nil)

    specific behaviour

  • rule_id (String, nil) (defaults to: nil)

    specific rule

  • scope (String) (defaults to: 'global')

    installation scope ('global' or 'project')

  • confirm (Boolean) (defaults to: false)

    must be true to proceed (default false)

Returns:

  • (Hash)

    installation result with paths and backup info



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rosett_ai/mcp/tools/hook_install_tool.rb', line 43

def call(behaviour_name: nil, rule_id: nil, scope: 'global', confirm: false)
  return confirmation_required_response unless confirm

  preview = HookPreviewTool.new.call(
    behaviour_name: behaviour_name, rule_id: rule_id, scope: scope
  )
  return preview if preview[:error]

  hook_path = resolve_hook_path(scope)
  backup_path = backup_existing(hook_path)
  write_hook(hook_path, preview[:script])
  settings_diff = register_in_settings(hook_path, scope)

  {
    installed: true,
    hook_path: hook_path.to_s,
    backup_path: backup_path&.to_s,
    rules_count: preview[:rules_count],
    behaviours: preview[:behaviours],
    scope: scope,
    settings_updated: settings_diff[:updated],
    message: "Hook installed at #{hook_path} with #{preview[:rules_count]} rule(s)"
  }
rescue StandardError => e
  ResponseHelper.error("Hook install failed: #{e.message}")
end