Skip to content

PreToolUse:Bash hook - OPC script directory guard #148

@udhaya10

Description

@udhaya10

Problem

When running /research-external, Claude can deviate from documented commands (cd $CLAUDE_OPC_DIR && uv run python scripts/mcp/...) and run from ~/.claude/ instead. This causes uv run to miss opc/pyproject.toml and its dependencies, resulting in ModuleNotFoundError.

Solution

Create a PreToolUse:Bash hook that:

  1. Detects scripts/(mcp|core)/ references in Bash commands
  2. Blocks execution unless prefixed with cd $CLAUDE_OPC_DIR &&
  3. Returns a deny message with the corrected command

Files

  • ~/.claude/hooks/src/mcp-directory-guard.ts — hook implementation
  • ~/.claude/settings.json — register PreToolUse:Bash matcher

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions