-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMcpAndAgentsTest.java
More file actions
330 lines (262 loc) · 13.3 KB
/
McpAndAgentsTest.java
File metadata and controls
330 lines (262 loc) · 13.3 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
/*---------------------------------------------------------------------------------------------
* 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.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.github.copilot.sdk.events.AssistantMessageEvent;
import com.github.copilot.sdk.json.CustomAgentConfig;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.ResumeSessionConfig;
import com.github.copilot.sdk.json.SessionConfig;
/**
* 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 local server configuration
private Map<String, Object> createLocalMcpServer(String command, List<String> args) {
var server = new HashMap<String, Object>();
server.put("type", "local");
server.put("command", command);
server.put("args", args);
server.put("tools", List.of("*"));
return server;
}
// ============ 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, Object>();
mcpServers.put("test-server", createLocalMcpServer("echo", List.of("hello")));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(new SessionConfig().setMcpServers(mcpServers)).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().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, Object>();
mcpServers.put("test-server", createLocalMcpServer("echo", List.of("hello")));
CopilotSession session2 = client
.resumeSession(sessionId, new ResumeSessionConfig().setMcpServers(mcpServers)).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, Object>();
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)).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)).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().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)).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)).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, Object>();
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)).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)).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, Object>();
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)).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();
}
}
}