forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcpAndAgentsTest.java
More file actions
422 lines (338 loc) · 18.1 KB
/
McpAndAgentsTest.java
File metadata and controls
422 lines (338 loc) · 18.1 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.github.copilot.sdk.generated.AssistantMessageEvent;
import com.github.copilot.sdk.json.CustomAgentConfig;
import com.github.copilot.sdk.json.DefaultAgentConfig;
import com.github.copilot.sdk.json.McpServerConfig;
import com.github.copilot.sdk.json.McpStdioServerConfig;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.PermissionHandler;
import com.github.copilot.sdk.json.ResumeSessionConfig;
import com.github.copilot.sdk.json.SessionConfig;
import com.github.copilot.sdk.json.ToolDefinition;
/**
* Tests for MCP Servers and Custom Agents functionality.
*
* <p>
* These tests use the shared CapiProxy infrastructure for deterministic API
* response replay. Snapshots are stored in test/snapshots/mcp_and_agents/.
* </p>
*/
public class McpAndAgentsTest {
private static E2ETestContext ctx;
@BeforeAll
static void setup() throws Exception {
ctx = E2ETestContext.create();
}
@AfterAll
static void teardown() throws Exception {
if (ctx != null) {
ctx.close();
}
}
// Helper method to create an MCP stdio server configuration
private McpStdioServerConfig createLocalMcpServer(String command, List<String> args) {
return new McpStdioServerConfig().setCommand(command).setArgs(args).setTools(List.of("*"));
}
// ============ MCP Server Tests ============
/**
* Verifies that MCP server configuration is accepted on session create.
*
* @see Snapshot:
* mcp_and_agents/should_accept_mcp_server_configuration_on_session_create
*/
@Test
void testShouldAcceptMcpServerConfigurationOnSessionCreate() throws Exception {
ctx.configureForTest("mcp_and_agents", "should_accept_mcp_server_configuration_on_session_create");
var mcpServers = new HashMap<String, McpServerConfig>();
mcpServers.put("test-server", createLocalMcpServer("echo", List.of("hello")));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(
new SessionConfig().setMcpServers(mcpServers).setOnPermissionRequest(PermissionHandler.APPROVE_ALL))
.get();
assertNotNull(session.getSessionId());
// Simple interaction to verify session works
AssistantMessageEvent response = session.sendAndWait(new MessageOptions().setPrompt("What is 2+2?")).get(60,
TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains("4"),
"Response should contain 4: " + response.getData().content());
session.close();
}
}
/**
* Verifies that MCP server configuration is accepted on session resume.
*
* @see Snapshot:
* mcp_and_agents/should_accept_mcp_server_configuration_on_session_resume
*/
@Test
void testShouldAcceptMcpServerConfigurationOnSessionResume() throws Exception {
ctx.configureForTest("mcp_and_agents", "should_accept_mcp_server_configuration_on_session_resume");
try (CopilotClient client = ctx.createClient()) {
// Create a session first
CopilotSession session1 = client
.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
String sessionId = session1.getSessionId();
session1.sendAndWait(new MessageOptions().setPrompt("What is 1+1?")).get(60, TimeUnit.SECONDS);
// Resume with MCP servers
var mcpServers = new HashMap<String, McpServerConfig>();
mcpServers.put("test-server", createLocalMcpServer("echo", List.of("hello")));
CopilotSession session2 = client.resumeSession(sessionId, new ResumeSessionConfig()
.setMcpServers(mcpServers).setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
assertEquals(sessionId, session2.getSessionId());
AssistantMessageEvent response = session2.sendAndWait(new MessageOptions().setPrompt("What is 3+3?"))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains("6"),
"Response should contain 6: " + response.getData().content());
session2.close();
}
}
/**
* Verifies that multiple MCP servers can be configured.
*
* @see Snapshot:
* mcp_and_agents/should_accept_mcp_server_configuration_on_session_create
*/
@Test
void testShouldHandleMultipleMcpServers() throws Exception {
// Use same snapshot as single MCP server test since it doesn't depend on server
// count
ctx.configureForTest("mcp_and_agents", "should_accept_mcp_server_configuration_on_session_create");
var mcpServers = new HashMap<String, McpServerConfig>();
mcpServers.put("server1", createLocalMcpServer("echo", List.of("server1")));
mcpServers.put("server2", createLocalMcpServer("echo", List.of("server2")));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(
new SessionConfig().setMcpServers(mcpServers).setOnPermissionRequest(PermissionHandler.APPROVE_ALL))
.get();
assertNotNull(session.getSessionId());
session.close();
}
}
// ============ Custom Agent Tests ============
/**
* Verifies that custom agent configuration is accepted on session create.
*
* @see Snapshot:
* mcp_and_agents/should_accept_custom_agent_configuration_on_session_create
*/
@Test
void testShouldAcceptCustomAgentConfigurationOnSessionCreate() throws Exception {
ctx.configureForTest("mcp_and_agents", "should_accept_custom_agent_configuration_on_session_create");
List<CustomAgentConfig> customAgents = List.of(new CustomAgentConfig().setName("test-agent")
.setDisplayName("Test Agent").setDescription("A test agent for SDK testing")
.setPrompt("You are a helpful test agent.").setInfer(true));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(new SessionConfig().setCustomAgents(customAgents)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
assertNotNull(session.getSessionId());
// Simple interaction to verify session works
AssistantMessageEvent response = session.sendAndWait(new MessageOptions().setPrompt("What is 5+5?")).get(60,
TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains("10"),
"Response should contain 10: " + response.getData().content());
session.close();
}
}
/**
* Verifies that custom agent configuration is accepted on session resume.
*
* @see Snapshot:
* mcp_and_agents/should_accept_custom_agent_configuration_on_session_resume
*/
@Test
void testShouldAcceptCustomAgentConfigurationOnSessionResume() throws Exception {
ctx.configureForTest("mcp_and_agents", "should_accept_custom_agent_configuration_on_session_resume");
try (CopilotClient client = ctx.createClient()) {
// Create a session first
CopilotSession session1 = client
.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
String sessionId = session1.getSessionId();
session1.sendAndWait(new MessageOptions().setPrompt("What is 1+1?")).get(60, TimeUnit.SECONDS);
// Resume with custom agents
List<CustomAgentConfig> customAgents = List
.of(new CustomAgentConfig().setName("resume-agent").setDisplayName("Resume Agent")
.setDescription("An agent added on resume").setPrompt("You are a resume test agent."));
CopilotSession session2 = client.resumeSession(sessionId, new ResumeSessionConfig()
.setCustomAgents(customAgents).setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
assertEquals(sessionId, session2.getSessionId());
AssistantMessageEvent response = session2.sendAndWait(new MessageOptions().setPrompt("What is 6+6?"))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains("12"),
"Response should contain 12: " + response.getData().content());
session2.close();
}
}
/**
* Verifies that custom agents can be configured with tools.
*
* @see Snapshot:
* mcp_and_agents/should_accept_custom_agent_configuration_on_session_create
*/
@Test
void testShouldAcceptCustomAgentWithToolsConfiguration() throws Exception {
// Use same snapshot as create test since this just verifies configuration
// acceptance
ctx.configureForTest("mcp_and_agents", "should_accept_custom_agent_configuration_on_session_create");
List<CustomAgentConfig> customAgents = List.of(new CustomAgentConfig().setName("tool-agent")
.setDisplayName("Tool Agent").setDescription("An agent with specific tools")
.setPrompt("You are an agent with specific tools.").setTools(List.of("bash", "edit")).setInfer(true));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(new SessionConfig().setCustomAgents(customAgents)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
assertNotNull(session.getSessionId());
session.close();
}
}
/**
* Verifies that custom agents can be configured with MCP servers.
*
* @see Snapshot:
* mcp_and_agents/should_accept_both_mcp_servers_and_custom_agents
*/
@Test
void testShouldAcceptCustomAgentWithMcpServers() throws Exception {
// Use combined snapshot since this uses both MCP servers and custom agents
ctx.configureForTest("mcp_and_agents", "should_accept_both_mcp_servers_and_custom_agents");
var agentMcpServers = new HashMap<String, McpServerConfig>();
agentMcpServers.put("agent-server", createLocalMcpServer("echo", List.of("agent-mcp")));
List<CustomAgentConfig> customAgents = List.of(new CustomAgentConfig().setName("mcp-agent")
.setDisplayName("MCP Agent").setDescription("An agent with its own MCP servers")
.setPrompt("You are an agent with MCP servers.").setMcpServers(agentMcpServers));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(new SessionConfig().setCustomAgents(customAgents)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
assertNotNull(session.getSessionId());
session.close();
}
}
/**
* Verifies that multiple custom agents can be configured.
*
* @see Snapshot:
* mcp_and_agents/should_accept_custom_agent_configuration_on_session_create
*/
@Test
void testShouldAcceptMultipleCustomAgents() throws Exception {
// Use same snapshot as create test
ctx.configureForTest("mcp_and_agents", "should_accept_custom_agent_configuration_on_session_create");
List<CustomAgentConfig> customAgents = List.of(
new CustomAgentConfig().setName("agent1").setDisplayName("Agent One").setDescription("First agent")
.setPrompt("You are agent one."),
new CustomAgentConfig().setName("agent2").setDisplayName("Agent Two").setDescription("Second agent")
.setPrompt("You are agent two.").setInfer(false));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(new SessionConfig().setCustomAgents(customAgents)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
assertNotNull(session.getSessionId());
session.close();
}
}
// ============ Combined Configuration Tests ============
/**
* Verifies that both MCP servers and custom agents can be configured.
*
* @see Snapshot:
* mcp_and_agents/should_accept_both_mcp_servers_and_custom_agents
*/
@Test
void testShouldAcceptBothMcpServersAndCustomAgents() throws Exception {
ctx.configureForTest("mcp_and_agents", "should_accept_both_mcp_servers_and_custom_agents");
var mcpServers = new HashMap<String, McpServerConfig>();
mcpServers.put("shared-server", createLocalMcpServer("echo", List.of("shared")));
List<CustomAgentConfig> customAgents = List.of(new CustomAgentConfig().setName("combined-agent")
.setDisplayName("Combined Agent").setDescription("An agent using shared MCP servers")
.setPrompt("You are a combined test agent."));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(new SessionConfig().setMcpServers(mcpServers)
.setCustomAgents(customAgents).setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
assertNotNull(session.getSessionId());
AssistantMessageEvent response = session.sendAndWait(new MessageOptions().setPrompt("What is 7+7?")).get(60,
TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains("14"),
"Response should contain 14: " + response.getData().content());
session.close();
}
}
// ============ DefaultAgent Tests ============
/**
* Verifies that sessions can be created with defaultAgent configuration and
* excludedTools hides tools from the default agent.
*
* @see Snapshot: mcp_and_agents/should_hide_excluded_tools_from_default_agent
*/
@Test
void testShouldHideExcludedToolsFromDefaultAgent() throws Exception {
ctx.configureForTest("mcp_and_agents", "should_hide_excluded_tools_from_default_agent");
try (CopilotClient client = ctx.createClient()) {
// Register a secret_tool and exclude it from the default agent — the LLM
// should report it has no access to the tool.
Map<String, Object> parameters = new HashMap<>();
parameters.put("type", "object");
parameters.put("properties", Map.of("input", Map.of("type", "string")));
parameters.put("required", List.of("input"));
ToolDefinition secretTool = ToolDefinition.create("secret_tool",
"A secret tool hidden from the default agent", parameters,
invocation -> CompletableFuture.completedFuture("SECRET"));
SessionConfig config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setTools(List.of(secretTool))
.setDefaultAgent(new DefaultAgentConfig().setExcludedTools(List.of("secret_tool")));
CopilotSession session = client.createSession(config).get();
assertNotNull(session.getSessionId());
AssistantMessageEvent response = session
.sendAndWait(new MessageOptions()
.setPrompt("Do you have access to a tool called secret_tool? Answer yes or no."))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().toLowerCase().contains("no"),
"Response should indicate that secret_tool is not accessible: " + response.getData().content());
session.close();
}
}
/**
* Verifies that defaultAgent configuration is accepted on session resume.
*
* @see Snapshot:
* mcp_and_agents/should_accept_defaultagent_configuration_on_session_resume
*/
@Test
void testShouldAcceptDefaultAgentConfigurationOnSessionResume() throws Exception {
ctx.configureForTest("mcp_and_agents", "should_accept_defaultagent_configuration_on_session_resume");
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client
.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
assertNotNull(session.getSessionId());
String sessionId = session.getSessionId();
// Do not call session.close() here — that invokes session.destroy on the
// server,
// which removes the session and causes the subsequent resumeSession to fail
// with "Session not found". The session handle is simply abandoned and the
// server-side session remains alive for the resume call below.
CopilotSession resumedSession = client.resumeSession(sessionId,
new ResumeSessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setDefaultAgent(new DefaultAgentConfig().setExcludedTools(List.of("view"))))
.get();
assertNotNull(resumedSession.getSessionId());
AssistantMessageEvent response = resumedSession.sendAndWait(new MessageOptions().setPrompt("What is 3+3?"))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
resumedSession.close();
}
}
}