-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMcpAndAgentsTest.java
More file actions
276 lines (208 loc) · 11.5 KB
/
McpAndAgentsTest.java
File metadata and controls
276 lines (208 loc) · 11.5 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
/*---------------------------------------------------------------------------------------------
* 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) {
Map<String, Object> server = new HashMap<>();
server.put("type", "local");
server.put("command", command);
server.put("args", args);
server.put("tools", List.of("*"));
return server;
}
// ============ MCP Server Tests ============
@Test
void testAcceptMcpServerConfigurationOnSessionCreate() throws Exception {
ctx.configureForTest("mcp_and_agents", "should_accept_mcp_server_configuration_on_session_create");
Map<String, Object> mcpServers = new HashMap<>();
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().getContent().contains("4"),
"Response should contain 4: " + response.getData().getContent());
session.close();
}
}
@Test
void testAcceptMcpServerConfigurationOnSessionResume() 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
Map<String, Object> mcpServers = new HashMap<>();
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().getContent().contains("6"),
"Response should contain 6: " + response.getData().getContent());
session2.close();
}
}
@Test
void testHandleMultipleMcpServers() 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");
Map<String, Object> mcpServers = new HashMap<>();
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 ============
@Test
void testAcceptCustomAgentConfigurationOnSessionCreate() 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().getContent().contains("10"),
"Response should contain 10: " + response.getData().getContent());
session.close();
}
}
@Test
void testAcceptCustomAgentConfigurationOnSessionResume() 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().getContent().contains("12"),
"Response should contain 12: " + response.getData().getContent());
session2.close();
}
}
@Test
void testCustomAgentWithToolsConfiguration() 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();
}
}
@Test
void testCustomAgentWithMcpServers() 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");
Map<String, Object> agentMcpServers = new HashMap<>();
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();
}
}
@Test
void testMultipleCustomAgents() 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 ============
@Test
void testAcceptBothMcpServersAndCustomAgents() throws Exception {
ctx.configureForTest("mcp_and_agents", "should_accept_both_mcp_servers_and_custom_agents");
Map<String, Object> mcpServers = new HashMap<>();
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().getContent().contains("14"),
"Response should contain 14: " + response.getData().getContent());
session.close();
}
}
}