Skip to main content

Hook JSON-RPC Protocol Details

All hooks use JSON-RPC 2.0 format, with one JSON message per line, transmitted via stdio.


Basic Protocol Structure​

Request (PicoClaw → Hook)​

{"jsonrpc":"2.0","id":1,"method":"hook.xxx","params":{...}}

Response (Hook → PicoClaw)​

Success:

{"jsonrpc":"2.0","id":1,"result":{...}}

Error:

{"jsonrpc":"2.0","id":1,"error":{"code":-32000,"message":"error message"}}

1. hook.hello (Handshake)​

Handshake must be completed at startup, otherwise the hook process will be terminated.

Request​

{
"jsonrpc": "2.0",
"id": 1,
"method": "hook.hello",
"params": {
"name": "py_review_gate",
"version": 1,
"modes": ["observe", "tool", "approve"]
}
}
FieldDescription
namehook name (from configuration)
versionprotocol version, currently 1
modescapability modes supported by the hook

Response​

{
"jsonrpc": "2.0",
"id": 1,
"result": {
"ok": true,
"name": "python-review-gate"
}
}

2. hook.before_llm​

Triggered before sending request to LLM. Can be used to inject tools.

Request​

{
"jsonrpc": "2.0",
"id": 2,
"method": "hook.before_llm",
"params": {
"meta": {
"AgentID": "agent-1",
"TurnID": "turn-1",
"ParentTurnID": "",
"SessionKey": "session-1",
"Iteration": 0,
"TracePath": "runTurn",
"Source": "turn.llm.request"
},
"model": "claude-sonnet",
"messages": [
{"role": "user", "content": "hello"}
],
"tools": [
{
"type": "function",
"function": {
"name": "echo",
"description": "echo text",
"parameters": {"type": "object"}
}
}
],
"options": {
"temperature": 0.7
},
"channel": "cli",
"chat_id": "chat-1",
"graceful_terminal": false
}
}
FieldDescription
metaevent metadata for tracing
modelrequested model name
messagesconversation history
toolslist of available tool definitions
optionsLLM parameters (temperature, max_tokens, etc.)
channelrequest source channel
chat_idsession ID

Response (Tool Injection Example)​

{
"jsonrpc": "2.0",
"id": 2,
"result": {
"action": "modify",
"request": {
"model": "claude-sonnet",
"messages": [{"role": "user", "content": "hello"}],
"tools": [
{
"type": "function",
"function": {
"name": "echo",
"description": "echo",
"parameters": {}
}
},
{
"type": "function",
"function": {
"name": "my_plugin_tool",
"description": "Plugin injected tool",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"}
}
}
}
}
]
}
}
}
FieldDescription
actiondecision action (see table below)
requestmodified request object

3. hook.after_llm​

Triggered after receiving LLM response. Can modify response content.

Request​

{
"jsonrpc": "2.0",
"id": 3,
"method": "hook.after_llm",
"params": {
"meta": {
"AgentID": "agent-1",
"TurnID": "turn-1",
"SessionKey": "session-1"
},
"model": "claude-sonnet",
"response": {
"role": "assistant",
"content": "Hi!",
"tool_calls": [
{
"id": "tc-1",
"type": "function",
"function": {
"name": "echo",
"arguments": "{\"text\":\"hi\"}"
}
}
]
},
"channel": "cli",
"chat_id": "chat-1"
}
}

Response​

{
"jsonrpc": "2.0",
"id": 3,
"result": {
"action": "continue"
}
}

4. hook.before_tool​

Triggered before tool execution. Can modify tool name and arguments, deny execution, or return result directly.

Request​

{
"jsonrpc": "2.0",
"id": 4,
"method": "hook.before_tool",
"params": {
"meta": {
"AgentID": "agent-1",
"TurnID": "turn-1",
"SessionKey": "session-1"
},
"tool": "echo_text",
"arguments": {
"text": "hello"
},
"channel": "cli",
"chat_id": "chat-1"
}
}
FieldDescription
tooltool name
argumentstool arguments

Response (Modify Arguments)​

{
"jsonrpc": "2.0",
"id": 4,
"result": {
"action": "modify",
"call": {
"tool": "echo_text",
"arguments": {
"text": "modified hello"
}
}
}
}

Response (Deny Execution)​

{
"jsonrpc": "2.0",
"id": 4,
"result": {
"action": "deny_tool",
"reason": "Invalid arguments"
}
}

Response (Return Result Directly - respond)​

{
"jsonrpc": "2.0",
"id": 4,
"result": {
"action": "respond",
"call": {
"tool": "my_plugin_tool",
"arguments": {
"query": "hello"
}
},
"result": {
"for_llm": "Plugin tool executed successfully",
"for_user": "",
"silent": false,
"is_error": false
}
}
}

The respond action allows hooks to return tool results directly, skipping actual tool execution. Use cases:

  1. Plugin tool injection: External hooks can implement tools without registering in ToolRegistry
  2. Tool result caching: Return cached results for repeated calls
  3. Tool mocking: Return mock results during testing
FieldDescription
actionmust be respond
callmodified call information (optional)
resulttool result to return directly

5. hook.after_tool​

Triggered after tool execution completes. Can modify the result returned to LLM.

Request​

{
"jsonrpc": "2.0",
"id": 5,
"method": "hook.after_tool",
"params": {
"meta": {
"AgentID": "agent-1",
"TurnID": "turn-1",
"SessionKey": "session-1"
},
"tool": "echo_text",
"arguments": {
"text": "hello"
},
"result": {
"for_llm": "echoed: hello",
"for_user": "",
"silent": false,
"is_error": false,
"async": false,
"media": [],
"artifact_tags": [],
"response_handled": false
},
"duration": 15000000,
"channel": "cli",
"chat_id": "chat-1"
}
}
FieldDescription
result.for_llmcontent returned to LLM
result.for_usercontent sent to user
result.silentwhether silent (not sent to user)
result.is_errorwhether it's an error
result.asyncwhether executed asynchronously
result.medialist of media references
result.artifact_tagslocal artifact path tags
result.response_handledwhether response has been handled
durationexecution time (nanoseconds)

Response​

{
"jsonrpc": "2.0",
"id": 5,
"result": {
"action": "continue"
}
}

6. hook.approve_tool​

Approval hook for deciding whether to allow execution of sensitive tools.

Request​

{
"jsonrpc": "2.0",
"id": 6,
"method": "hook.approve_tool",
"params": {
"meta": {
"AgentID": "agent-1",
"TurnID": "turn-1",
"SessionKey": "session-1"
},
"tool": "bash",
"arguments": {
"command": "rm -rf /"
},
"channel": "cli",
"chat_id": "chat-1"
}
}

Response (Approved)​

{
"jsonrpc": "2.0",
"id": 6,
"result": {
"approved": true
}
}

Response (Denied)​

{
"jsonrpc": "2.0",
"id": 6,
"result": {
"approved": false,
"reason": "Dangerous command, execution denied"
}
}

7. hook.runtime_event (notification)​

Runtime observer event, broadcast only, no response required. id is 0 or absent.

{
"jsonrpc": "2.0",
"method": "hook.runtime_event",
"params": {
"kind": "agent.tool.exec_start",
"source": {
"component": "agent",
"name": "agent-1"
},
"scope": {
"agent_id": "agent-1",
"session_key": "session-1",
"turn_id": "turn-1",
"channel": "cli",
"chat_id": "chat-1"
},
"payload": {
"Tool": "echo_text",
"Arguments": {"text": "hello"}
}
}
}

A second example shows a non-agent event (mcp.server.connected). MCP events carry no agent scope; source is mcp with the server name:

{
"jsonrpc": "2.0",
"method": "hook.runtime_event",
"params": {
"kind": "mcp.server.connected",
"source": {
"component": "mcp",
"name": "my-mcp-server"
},
"scope": {},
"payload": {
"server": "my-mcp-server",
"type": "stdio",
"command": "/usr/local/bin/my-mcp-server",
"tool_count": 5
}
}
}

The kind field can be any kind declared in pkg/events. Every kind family is observable; agent.* is the most commonly used but hooks can also observe channel.*, bus.*, gateway.*, and mcp.* events. The full catalog with payloads and severity levels is in Runtime Events.

Representative kind values by family:

FamilyExample kinds
agent.*agent.turn.start, agent.turn.end, agent.llm.request, agent.llm.response, agent.llm.delta, agent.llm.retry, agent.tool.exec_start, agent.tool.exec_end, agent.tool.exec_skipped, agent.steering.injected, agent.follow_up.queued, agent.interrupt.received, agent.context.compress, agent.session.summarize, agent.subturn.spawn, agent.subturn.end, agent.subturn.result_delivered, agent.subturn.orphan, agent.error
channel.*channel.lifecycle.started, channel.lifecycle.stopped, channel.message.outbound_sent, channel.message.outbound_failed, channel.rate_limited
bus.*bus.publish.failed, bus.message.dropped, bus.close.started, bus.close.completed
gateway.*gateway.start, gateway.ready, gateway.shutdown, gateway.reload.started, gateway.reload.completed, gateway.reload.failed
mcp.*mcp.server.connected, mcp.server.connecting, mcp.server.failed, mcp.tool.discovered, mcp.tool.call.start, mcp.tool.call.end

Setting observe to ["*"] or ["all"] (or the empty string [""]) subscribes to every runtime event kind. This is equivalent to listing every kind individually and is useful during development to capture all activity.

The following legacy short aliases are accepted in observe and normalized to their canonical runtime event kind:

AliasCanonical kind(s)
turn_startagent.turn.start
turn_endagent.turn.end
turn_completedagent.turn.end (filtered to successfully completed turns only)
llm_requestagent.llm.request
llm_deltaagent.llm.delta
llm_responseagent.llm.response
llm_retryagent.llm.retry
context_compressagent.context.compress
session_summarizeagent.session.summarize
post_compactionagent.context.compress and agent.session.summarize
tool_exec_startagent.tool.exec_start
tool_exec_endagent.tool.exec_end
tool_exec_skippedagent.tool.exec_skipped
steering_injectedagent.steering.injected
follow_up_queuedagent.follow_up.queued
interrupt_receivedagent.interrupt.received
subturn_spawnagent.subturn.spawn
subturn_endagent.subturn.end
subturn_result_deliveredagent.subturn.result_delivered
subturn_orphanagent.subturn.orphan
erroragent.error

Canonical kind strings (e.g. agent.turn.end) are also accepted directly. New process hook notifications use hook.runtime_event.


Action Options​

actionApplicable hooksEffect
continueAll interceptor typesPass through without modification
modifybefore_llm, before_tool, after_llm, after_toolModify request/response and pass through
respondbefore_toolReturn tool result directly, skip actual execution. Note: AfterTool is NOT called (design decision - respond provides final answer).
deny_toolbefore_toolDeny tool execution
abort_turnAll interceptor typesAbort current turn, return error
hard_abortAll interceptor typesForce stop entire agent loop

Complete Flow Example​

{"jsonrpc":"2.0","id":1,"method":"hook.hello","params":{"name":"my_hook","version":1,"modes":["tool","approve"]}}
{"jsonrpc":"2.0","id":1,"result":{"ok":true,"name":"my_hook"}}
{"jsonrpc":"2.0","id":2,"method":"hook.before_llm","params":{"model":"claude-sonnet","messages":[{"role":"user","content":"hello"}],"tools":[]}}
{"jsonrpc":"2.0","id":2,"result":{"action":"continue"}}
{"jsonrpc":"2.0","id":3,"method":"hook.before_tool","params":{"tool":"bash","arguments":{"command":"ls"}}}
{"jsonrpc":"2.0","id":3,"result":{"action":"continue"}}
{"jsonrpc":"2.0","id":4,"method":"hook.approve_tool","params":{"tool":"bash","arguments":{"command":"ls"}}}
{"jsonrpc":"2.0","id":4,"result":{"approved":true}}
{"jsonrpc":"2.0","id":5,"method":"hook.after_tool","params":{"tool":"bash","arguments":{"command":"ls"},"result":{"for_llm":"file1.txt\nfile2.txt"},"duration":5000000}}
{"jsonrpc":"2.0","id":5,"result":{"action":"continue"}}
{"jsonrpc":"2.0","id":6,"method":"hook.after_llm","params":{"model":"claude-sonnet","response":{"role":"assistant","content":"Files listed"}}}
{"jsonrpc":"2.0","id":6,"result":{"action":"continue"}}

Plugin Tool Injection via before_llm and before_tool​

Standard flow for plugin tool injection:

  1. In before_llm, inject tool definition to let LLM know the tool is available
  2. In before_tool, use respond action to return tool execution result directly

before_llm Inject Tool Definition​

def handle_before_llm(params: dict) -> dict:
tools = params.get("tools", [])

# Add plugin tool definition
tools.append({
"type": "function",
"function": {
"name": "my_plugin_tool",
"description": "Plugin provided tool",
"parameters": {
"type": "object",
"properties": {
"input": {"type": "string", "description": "Input content"}
},
"required": ["input"]
}
}
})

return {
"action": "modify",
"request": {
"model": params["model"],
"messages": params["messages"],
"tools": tools,
"options": params.get("options", {})
}
}

before_tool Return Execution Result​

def handle_before_tool(params: dict) -> dict:
tool = params.get("tool", "")

if tool == "my_plugin_tool":
# Implement tool logic here
args = params.get("arguments", {})
input_text = args.get("input", "")

# Return result directly, no need to register in ToolRegistry
return {
"action": "respond",
"result": {
"for_llm": f"Plugin tool executed successfully, input: {input_text}",
"silent": False,
"is_error": False
}
}

return {"action": "continue"}

This way, external hooks can fully implement plugin tools without registering any tool implementation inside PicoClaw.