Repository navigation
Expand file tree
/
Copy pathpre_mcp_tool_call_hook.rs
More file actions
235 lines (214 loc) · 8.29 KB
/
Copy pathpre_mcp_tool_call_hook.rs
File metadata and controls
235 lines (214 loc) · 8.29 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use std::sync::Arc;
use async_trait::async_trait;
use github_copilot_sdk::hooks::{
HookContext, PreMcpToolCallInput, PreMcpToolCallOutput, SessionHooks,
};
use github_copilot_sdk::{IndexMap, McpServerConfig, McpStdioServerConfig};
use serde_json::{Value, json};
use tokio::sync::mpsc;
use super::support::{assistant_message_content, recv_with_timeout};
fn meta_echo_mcp_servers(repo_root: &std::path::Path) -> IndexMap<String, McpServerConfig> {
let harness_dir = repo_root.join("test").join("harness");
let server_path = harness_dir
.join("test-mcp-meta-echo-server.mjs")
.to_string_lossy()
.to_string();
IndexMap::from([(
"meta-echo".to_string(),
McpServerConfig::Stdio(McpStdioServerConfig {
tools: Some(vec!["*".to_string()]),
command: if cfg!(windows) {
"node.exe".to_string()
} else {
"node".to_string()
},
args: vec![server_path],
working_directory: Some(harness_dir.to_string_lossy().to_string()),
..McpStdioServerConfig::default()
}),
)])
}
struct SetMetaHooks {
tx: mpsc::UnboundedSender<PreMcpToolCallInput>,
}
#[async_trait]
impl SessionHooks for SetMetaHooks {
async fn on_pre_mcp_tool_call(
&self,
input: PreMcpToolCallInput,
_ctx: HookContext,
) -> Option<PreMcpToolCallOutput> {
let _ = self.tx.send(input);
Some(PreMcpToolCallOutput {
meta_to_use: Some(json!({"injected": "by-hook", "source": "test"})),
})
}
}
struct ReplaceMetaHooks {
tx: mpsc::UnboundedSender<PreMcpToolCallInput>,
}
#[async_trait]
impl SessionHooks for ReplaceMetaHooks {
async fn on_pre_mcp_tool_call(
&self,
input: PreMcpToolCallInput,
_ctx: HookContext,
) -> Option<PreMcpToolCallOutput> {
let _ = self.tx.send(input);
Some(PreMcpToolCallOutput {
meta_to_use: Some(json!({"completely": "replaced"})),
})
}
}
struct RemoveMetaHooks {
tx: mpsc::UnboundedSender<PreMcpToolCallInput>,
}
#[async_trait]
impl SessionHooks for RemoveMetaHooks {
async fn on_pre_mcp_tool_call(
&self,
input: PreMcpToolCallInput,
_ctx: HookContext,
) -> Option<PreMcpToolCallOutput> {
let _ = self.tx.send(input);
Some(PreMcpToolCallOutput {
meta_to_use: Some(Value::Null),
})
}
}
#[tokio::test]
async fn should_set_meta_via_premcptoolcall_hook() {
super::support::with_shared_e2e_context(&E2E,
"pre_mcp_tool_call_hook",
"should_set_meta_via_premcptoolcall_hook",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let (tx, mut rx) = mpsc::unbounded_channel();
let client = ctx.start_client().await;
let session = client
.create_session(
ctx.approve_all_session_config()
.with_mcp_servers(meta_echo_mcp_servers(ctx.repo_root()))
.with_hooks(Arc::new(SetMetaHooks { tx })),
)
.await
.expect("create session");
let answer = session
.send_and_wait(
"Use the meta-echo/echo_meta tool with value 'test-set'. Reply with just the raw tool result.",
)
.await
.expect("send")
.expect("assistant message");
let content = assistant_message_content(&answer);
assert!(
content.contains("injected"),
"Expected 'injected' in response, got: {content}"
);
assert!(
content.contains("by-hook"),
"Expected 'by-hook' in response, got: {content}"
);
let input = recv_with_timeout(&mut rx, "preMcpToolCall hook").await;
assert_eq!(input.server_name, "meta-echo");
assert_eq!(input.tool_name, "echo_meta");
assert!(!input.working_directory.as_os_str().is_empty());
assert!(input.timestamp > 0.0);
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_replace_meta_via_premcptoolcall_hook() {
super::support::with_shared_e2e_context(&E2E,
"pre_mcp_tool_call_hook",
"should_replace_meta_via_premcptoolcall_hook",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let (tx, mut rx) = mpsc::unbounded_channel();
let client = ctx.start_client().await;
let session = client
.create_session(
ctx.approve_all_session_config()
.with_mcp_servers(meta_echo_mcp_servers(ctx.repo_root()))
.with_hooks(Arc::new(ReplaceMetaHooks { tx })),
)
.await
.expect("create session");
let answer = session
.send_and_wait(
"Use the meta-echo/echo_meta tool with value 'test-replace'. Reply with just the raw tool result.",
)
.await
.expect("send")
.expect("assistant message");
let content = assistant_message_content(&answer);
assert!(
content.contains("completely"),
"Expected 'completely' in response, got: {content}"
);
assert!(
content.contains("replaced"),
"Expected 'replaced' in response, got: {content}"
);
let input = recv_with_timeout(&mut rx, "preMcpToolCall hook").await;
assert_eq!(input.server_name, "meta-echo");
assert_eq!(input.tool_name, "echo_meta");
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_remove_meta_via_premcptoolcall_hook() {
super::support::with_shared_e2e_context(&E2E,
"pre_mcp_tool_call_hook",
"should_remove_meta_via_premcptoolcall_hook",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let (tx, mut rx) = mpsc::unbounded_channel();
let client = ctx.start_client().await;
let session = client
.create_session(
ctx.approve_all_session_config()
.with_mcp_servers(meta_echo_mcp_servers(ctx.repo_root()))
.with_hooks(Arc::new(RemoveMetaHooks { tx })),
)
.await
.expect("create session");
let answer = session
.send_and_wait(
"Use the meta-echo/echo_meta tool with value 'test-remove'. Reply with just the raw tool result.",
)
.await
.expect("send")
.expect("assistant message");
let content = assistant_message_content(&answer);
assert!(
content.contains("\"meta\":null"),
"Expected '\"meta\":null' in response, got: {content}"
);
assert!(
content.contains("test-remove"),
"Expected 'test-remove' in response, got: {content}"
);
let input = recv_with_timeout(&mut rx, "preMcpToolCall hook").await;
assert_eq!(input.server_name, "meta-echo");
assert_eq!(input.tool_name, "echo_meta");
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
static E2E: super::support::SharedE2eGroup =
super::support::SharedE2eGroup::standard("pre_mcp_tool_call_hook", 3);