Skip to content

Commit 7e1e0b2

Browse files
Copilotedburns
andauthored
Update documentation for preMcpToolCall hook
Add preMcpToolCall hook section to hooks.md with input/output tables and usage example. Update advanced.md quick-start example and hook count references. Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
1 parent af06f96 commit 7e1e0b2

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

src/site/markdown/advanced.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,12 @@ var hooks = new SessionHooks()
827827
System.out.println("Tool: " + input.getToolName());
828828
return CompletableFuture.completedFuture(PreToolUseHookOutput.allow());
829829
})
830+
.setOnPreMcpToolCall((input, invocation) -> {
831+
System.out.println("MCP Tool: " + input.getServerName() + "/" + input.getToolName());
832+
// Set metadata on the MCP request
833+
JsonNode meta = mapper.valueToTree(Map.of("source", "my-app"));
834+
return CompletableFuture.completedFuture(PreMcpToolCallHookOutput.withMeta(meta));
835+
})
830836
.setOnPostToolUse((input, invocation) -> {
831837
System.out.println("Result: " + input.getToolResult());
832838
return CompletableFuture.completedFuture(null);
@@ -837,7 +843,7 @@ var session = client.createSession(
837843
).get();
838844
```
839845

840-
📖 **[Full Session Hooks documentation →](hooks.html)** for all 5 hook types, inputs/outputs, and examples.
846+
📖 **[Full Session Hooks documentation →](hooks.html)** for all 6 hook types, inputs/outputs, and examples.
841847

842848
---
843849

@@ -1406,7 +1412,7 @@ See [CloudSessionOptions](apidocs/com/github/copilot/sdk/json/CloudSessionOption
14061412
## Next Steps
14071413

14081414
- 📖 **[Documentation](documentation.html)** - Core concepts, events, streaming, models, tool filtering, reasoning effort
1409-
- 📖 **[Session Hooks](hooks.html)** - All 5 hook types with inputs, outputs, and examples
1415+
- 📖 **[Session Hooks](hooks.html)** - All 6 hook types with inputs, outputs, and examples
14101416
- 📖 **[MCP Servers](mcp.html)** - Local and remote MCP server integration
14111417
- 📖 **[Setup & Deployment](setup.html)** - OAuth, backend services, scaling, configuration reference
14121418
- 📖 **[API Javadoc](apidocs/index.html)** - Complete API reference

src/site/markdown/hooks.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

124172
Called **after** a tool executes. Use this to:

0 commit comments

Comments
 (0)