-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathpre_mcp_tool_call_hook.e2e.test.ts
More file actions
132 lines (111 loc) · 4.98 KB
/
pre_mcp_tool_call_hook.e2e.test.ts
File metadata and controls
132 lines (111 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
import { describe, expect, it } from "vitest";
import { approveAll } from "../../src/index.js";
import type { MCPStdioServerConfig, PreMcpToolCallHookInput } from "../../src/types.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const TEST_MCP_META_ECHO_SERVER = resolve(
__dirname,
"../../../test/harness/test-mcp-meta-echo-server.mjs"
);
const TEST_HARNESS_DIR = dirname(TEST_MCP_META_ECHO_SERVER);
describe("pre_mcp_tool_call_hook", async () => {
const { copilotClient: client } = await createSdkTestContext();
it("should set meta via preMcpToolCall hook", async () => {
const hookInputs: PreMcpToolCallHookInput[] = [];
const session = await client.createSession({
onPermissionRequest: approveAll,
mcpServers: {
"meta-echo": {
command: "node",
args: [TEST_MCP_META_ECHO_SERVER],
workingDirectory: TEST_HARNESS_DIR,
tools: ["*"],
} as MCPStdioServerConfig,
},
hooks: {
onPreMcpToolCall: async (input, _invocation) => {
hookInputs.push(input);
return { metaToUse: { injected: "by-hook", source: "test" } };
},
},
});
const message = await session.sendAndWait({
prompt: "Use the meta-echo/echo_meta tool with value 'test-set'. Reply with just the raw tool result.",
});
expect(message).not.toBeNull();
expect(message!.data.content).toContain("injected");
expect(message!.data.content).toContain("by-hook");
expect(hookInputs.length).toBeGreaterThan(0);
expect(hookInputs[0].serverName).toBe("meta-echo");
expect(hookInputs[0].toolName).toBe("echo_meta");
expect(hookInputs[0].workingDirectory).toBeDefined();
expect(hookInputs[0].timestamp).toBeInstanceOf(Date);
await session.disconnect();
});
it("should replace meta via preMcpToolCall hook", async () => {
const hookInputs: PreMcpToolCallHookInput[] = [];
const session = await client.createSession({
onPermissionRequest: approveAll,
mcpServers: {
"meta-echo": {
command: "node",
args: [TEST_MCP_META_ECHO_SERVER],
workingDirectory: TEST_HARNESS_DIR,
tools: ["*"],
} as MCPStdioServerConfig,
},
hooks: {
onPreMcpToolCall: async (input, _invocation) => {
hookInputs.push(input);
return { metaToUse: { completely: "replaced" } };
},
},
});
const message = await session.sendAndWait({
prompt: "Use the meta-echo/echo_meta tool with value 'test-replace'. Reply with just the raw tool result.",
});
expect(message).not.toBeNull();
expect(message!.data.content).toContain("completely");
expect(message!.data.content).toContain("replaced");
expect(hookInputs.length).toBeGreaterThan(0);
expect(hookInputs[0].serverName).toBe("meta-echo");
expect(hookInputs[0].toolName).toBe("echo_meta");
await session.disconnect();
});
it("should remove meta via preMcpToolCall hook", async () => {
const hookInputs: PreMcpToolCallHookInput[] = [];
const session = await client.createSession({
onPermissionRequest: approveAll,
mcpServers: {
"meta-echo": {
command: "node",
args: [TEST_MCP_META_ECHO_SERVER],
workingDirectory: TEST_HARNESS_DIR,
tools: ["*"],
} as MCPStdioServerConfig,
},
hooks: {
onPreMcpToolCall: async (input, _invocation) => {
hookInputs.push(input);
return { metaToUse: null };
},
},
});
const message = await session.sendAndWait({
prompt: "Use the meta-echo/echo_meta tool with value 'test-remove'. Reply with just the raw tool result.",
});
expect(message).not.toBeNull();
expect(message!.data.content).toContain('"meta":null');
expect(message!.data.content).toContain("test-remove");
expect(hookInputs.length).toBeGreaterThan(0);
expect(hookInputs[0].serverName).toBe("meta-echo");
expect(hookInputs[0].toolName).toBe("echo_meta");
await session.disconnect();
});
});