@@ -9,6 +9,7 @@ Session hooks allow you to intercept and modify tool execution, user prompts, an
99| Hook | When It's Called | Can Modify |
1010| ------| ------------------| ------------|
1111| [ Pre-Tool Use] ( #Pre-Tool_Use_Hook ) | Before a tool executes | Tool arguments, permission decision |
12+ | [ Pre-MCP-Tool-Call] ( #Pre-MCP-Tool-Call_Hook ) | Before an MCP tool call is dispatched | MCP request metadata (` _meta ` ) |
1213| [ Post-Tool Use] ( #Post-Tool_Use_Hook ) | After a tool executes | Tool result, additional context |
1314| [ User Prompt Submitted] ( #User_Prompt_Submitted_Hook ) | When user sends a message | Nothing (observation only) |
1415| [ Session Start] ( #Session_Start_Hook ) | When session begins | Nothing (observation only) |
@@ -119,6 +120,53 @@ var hooks = new SessionHooks()
119120
120121---
121122
123+ ## Pre-MCP-Tool-Call Hook
124+
125+ Called ** before** an MCP tool call is dispatched to an MCP server. Use this to:
126+ - Inspect or log MCP tool calls
127+ - Set, replace, or remove MCP request metadata (` _meta ` )
128+
129+ ### Input
130+
131+ | Field | Type | Description |
132+ | -------| ------| -------------|
133+ | ` getSessionId() ` | ` String ` | Runtime session ID of the session that triggered the hook |
134+ | ` getTimestamp() ` | ` long ` | Unix timestamp in milliseconds |
135+ | ` getCwd() ` | ` String ` | Current working directory |
136+ | ` getServerName() ` | ` String ` | Name of the MCP server being called |
137+ | ` getToolName() ` | ` String ` | Name of the MCP tool being called |
138+ | ` getArguments() ` | ` JsonNode ` | Arguments for the MCP tool call |
139+ | ` getToolCallId() ` | ` String ` | Tool call ID (may be null) |
140+ | ` getMeta() ` | ` Map<String, JsonNode> ` | Existing MCP request metadata (may be null) |
141+
142+ ### Output
143+
144+ Return ` null ` from the handler to preserve existing ` _meta ` (no-op). Otherwise, return a ` PreMcpToolCallHookOutput ` :
145+
146+ | Factory Method | Effect |
147+ | ----------------| --------|
148+ | ` PreMcpToolCallHookOutput.withMeta(jsonNode) ` | Replace ` _meta ` with the given JSON object |
149+ | ` PreMcpToolCallHookOutput.removeMeta() ` | Remove ` _meta ` from the request |
150+
151+ ### Example: Inject metadata into MCP requests
152+
153+ ``` java
154+ var hooks = new SessionHooks ()
155+ .setOnPreMcpToolCall((input, invocation) - > {
156+ System . out. println(" MCP call: " + input. getServerName() + " /" + input. getToolName());
157+
158+ // Inject custom metadata into the MCP request
159+ var mapper = new ObjectMapper ();
160+ JsonNode meta = mapper. valueToTree(Map . of(
161+ " source" , " my-application" ,
162+ " requestId" , UUID . randomUUID(). toString()
163+ ));
164+ return CompletableFuture . completedFuture(PreMcpToolCallHookOutput . withMeta(meta));
165+ });
166+ ```
167+
168+ ---
169+
122170## Post-Tool Use Hook
123171
124172Called ** after** a tool executes. Use this to:
0 commit comments