MonoClaw

Hooks

Hooks let you run custom code at key points in Mona's lifecycle. They are the event-driven backbone of MonoClaw's plugin system.

Hook events

EventWhen it firesCommon uses
pre_messageBefore Mona processes a user messageLogging, filtering, rate limiting
post_responseAfter Mona generates a responseAnalytics, notifications
pre_tool_callBefore a tool is executedApproval gates, logging
post_tool_callAfter a tool returnsResult transformation, caching
session_startWhen a new session beginsContext loading, greetings
session_endWhen a session endsCleanup, summary generation
gateway_messageWhen a gateway receives a messageRouting, filtering
cron_triggerWhen a cron job firesPre-job setup, validation
errorWhen an error occursAlerting, fallback handling

Creating hooks

As a plugin:

# my_plugin/hooks.py
from monoclaw.plugins import Hook

class SlackAlertHook(Hook):
    event = "error"

    async def run(self, error: Exception, context: dict) -> None:
        import httpx
        await httpx.AsyncClient().post(
            "https://hooks.slack.com/services/...",
            json={"text": f"Mona error: {str(error)}"}
        )

def register_hooks():
    return [SlackAlertHook()]

Inline hooks

For simple one-off hooks, use the CLI:

monoclaw hook add pre_tool_call --command "echo 'Tool: {tool_name}' >> ~/.monoclaw/logs/tools.log"

Hook ordering

Multiple hooks can fire for the same event. Control execution order:

# ~/.monoclaw/config.yaml
hooks:
  pre_message:
    - my_plugin.logging_hook    # priority: 1
    - my_plugin.filter_hook     # priority: 2

Lower numbers run first.

Hook middleware

Hooks can modify data as it passes through:

class CapitalizeHook(Hook):
    event = "post_response"

    async def run(self, response: str) -> str:
        # Transform the response
        return response.upper()

Disabling hooks

Disable specific hooks:

monoclaw hook disable my_plugin.slack_alert

Or disable all hooks for an event:

hooks:
  post_response:
    enabled: false

Best practices

  • Keep hooks fast — Slow hooks delay responses
  • Handle errors gracefully — A failing hook shouldn't crash the agent
  • Be selective — Don't log everything; focus on actionable events
  • Use async — Hooks should be async to avoid blocking
  • Test thoroughly — Hooks run on every matching event

Built-in hooks

MonoClaw ships with several built-in hooks:

  • Disk cleanupsession_end
  • Session analyticssession_end
  • Auto-savepre_message
  • Security scannerpre_tool_call

See the Built-in Plugins guide for details.