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
INPUT_SCHEMA =
{
  type: 'object',
  properties: {
    behaviour_name: {
      type: 'string',
      description: 'Behaviour to generate hook for (default: all enforceable)'
    },
    rule_id: {
      type: 'string',
      description: 'Specific rule ID to generate hook for'
    },
    scope: {
      type: 'string',
      enum: ['global', 'local'],
      description: 'Installation scope (default: global)'
    },
    confirm: {
      type: 'boolean',
      description: 'Must be true to actually install — safety gate (default: 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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rosett_ai/mcp/tools/hook_install_tool.rb', line 66

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