-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_mcp_and_agents_e2e.py
More file actions
333 lines (271 loc) · 11.3 KB
/
test_mcp_and_agents_e2e.py
File metadata and controls
333 lines (271 loc) · 11.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
331
332
333
"""
Tests for MCP servers and custom agents functionality
"""
import asyncio
import time
from pathlib import Path
import pytest
from copilot.generated.rpc import McpServerStatus
from copilot.session import CustomAgentConfig, MCPServerConfig, PermissionHandler
from .testharness import E2ETestContext
TEST_MCP_SERVER = str(
(Path(__file__).parents[2] / "test" / "harness" / "test-mcp-server.mjs").resolve()
)
TEST_HARNESS_DIR = str((Path(__file__).parents[2] / "test" / "harness").resolve())
pytestmark = pytest.mark.asyncio(loop_scope="module")
def _test_mcp_servers(*server_names: str) -> dict[str, MCPServerConfig]:
return {
server_name: {
"command": "node",
"args": [TEST_MCP_SERVER],
"tools": ["*"],
"working_directory": TEST_HARNESS_DIR,
}
for server_name in server_names
}
async def _wait_for_mcp_server_status(
session, server_name: str, expected_status: McpServerStatus = McpServerStatus.CONNECTED
) -> None:
deadline = time.monotonic() + 60
last_status = "<not listed>"
while time.monotonic() < deadline:
result = await session.rpc.mcp.list()
server = next((s for s in result.servers if s.name == server_name), None)
if server is not None and server.status == expected_status:
return
last_status = server.status if server is not None else "<not listed>"
await asyncio.sleep(0.2)
raise AssertionError(
f"{server_name} did not reach {expected_status.value}; last status was {last_status}"
)
class TestMCPServers:
async def test_should_accept_mcp_server_configuration_on_session_create(
self, ctx: E2ETestContext
):
"""Test that MCP server configuration is accepted on session create"""
mcp_servers = _test_mcp_servers("test-server")
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all, mcp_servers=mcp_servers
)
assert session.session_id is not None
await _wait_for_mcp_server_status(session, "test-server")
# Simple interaction to verify session works
message = await session.send_and_wait("What is 2+2?")
assert message is not None
assert "4" in message.data.content
await session.disconnect()
async def test_should_accept_mcp_server_configuration_without_args(self, ctx: E2ETestContext):
"""Test that MCP server configuration works without args field"""
mcp_servers: dict[str, MCPServerConfig] = {
"test-server": {
"command": "git",
"tools": ["*"],
}
}
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all, mcp_servers=mcp_servers
)
assert session.session_id is not None
await session.disconnect()
async def test_should_accept_mcp_server_configuration_on_session_resume(
self, ctx: E2ETestContext
):
"""Test that MCP server configuration is accepted on session resume"""
# Create a session first
session1 = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all
)
session_id = session1.session_id
await session1.send_and_wait("What is 1+1?")
# Resume with MCP servers
mcp_servers = _test_mcp_servers("test-server")
session2 = await ctx.client.resume_session(
session_id,
on_permission_request=PermissionHandler.approve_all,
mcp_servers=mcp_servers,
)
assert session2.session_id == session_id
await _wait_for_mcp_server_status(session2, "test-server")
await session2.disconnect()
async def test_should_pass_literal_env_values_to_mcp_server_subprocess(
self, ctx: E2ETestContext
):
"""Test that env values are passed as literals to MCP server subprocess"""
mcp_servers: dict[str, MCPServerConfig] = {
"env-echo": {
"command": "node",
"args": [TEST_MCP_SERVER],
"tools": ["*"],
"env": {"TEST_SECRET": "hunter2"},
"working_directory": TEST_HARNESS_DIR,
}
}
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all, mcp_servers=mcp_servers
)
assert session.session_id is not None
await _wait_for_mcp_server_status(session, "env-echo")
message = await session.send_and_wait(
"Use the env-echo/get_env tool to read the TEST_SECRET "
"environment variable. Reply with just the value, nothing else."
)
assert message is not None
assert "hunter2" in message.data.content
await session.disconnect()
class TestCustomAgents:
async def test_should_accept_custom_agent_configuration_on_session_create(
self, ctx: E2ETestContext
):
"""Test that custom agent configuration is accepted on session create"""
custom_agents: list[CustomAgentConfig] = [
{
"name": "test-agent",
"display_name": "Test Agent",
"description": "A test agent for SDK testing",
"prompt": "You are a helpful test agent.",
"infer": True,
}
]
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all, custom_agents=custom_agents
)
assert session.session_id is not None
# Simple interaction to verify session works
message = await session.send_and_wait("What is 5+5?")
assert message is not None
assert "10" in message.data.content
await session.disconnect()
async def test_should_accept_custom_agent_configuration_on_session_resume(
self, ctx: E2ETestContext
):
"""Test that custom agent configuration is accepted on session resume"""
# Create a session first
session1 = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all
)
session_id = session1.session_id
await session1.send_and_wait("What is 1+1?")
# Resume with custom agents
custom_agents: list[CustomAgentConfig] = [
{
"name": "resume-agent",
"display_name": "Resume Agent",
"description": "An agent added on resume",
"prompt": "You are a resume test agent.",
}
]
session2 = await ctx.client.resume_session(
session_id,
on_permission_request=PermissionHandler.approve_all,
custom_agents=custom_agents,
)
assert session2.session_id == session_id
message = await session2.send_and_wait("What is 6+6?")
assert message is not None
assert "12" in message.data.content
await session2.disconnect()
async def test_should_handle_multiple_mcp_servers(self, ctx: E2ETestContext):
"""Multiple MCP servers can be configured at once."""
mcp_servers = _test_mcp_servers("server1", "server2")
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
mcp_servers=mcp_servers,
)
try:
assert session.session_id is not None
await _wait_for_mcp_server_status(session, "server1")
await _wait_for_mcp_server_status(session, "server2")
import re
assert re.match(r"^[a-f0-9-]+$", session.session_id)
finally:
await session.disconnect()
class TestCombinedConfiguration:
async def test_should_accept_both_mcp_servers_and_custom_agents(self, ctx: E2ETestContext):
"""Test that both MCP servers and custom agents can be configured together"""
mcp_servers = _test_mcp_servers("shared-server")
custom_agents: list[CustomAgentConfig] = [
{
"name": "combined-agent",
"display_name": "Combined Agent",
"description": "An agent using shared MCP servers",
"prompt": "You are a combined test agent.",
}
]
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
mcp_servers=mcp_servers,
custom_agents=custom_agents,
)
assert session.session_id is not None
await _wait_for_mcp_server_status(session, "shared-server")
await session.disconnect()
async def test_should_handle_custom_agent_with_tools_configuration(self, ctx: E2ETestContext):
"""A custom agent can advertise specific tools."""
custom_agents: list[CustomAgentConfig] = [
{
"name": "tool-agent",
"display_name": "Tool Agent",
"description": "An agent with specific tools",
"prompt": "You are an agent with specific tools.",
"tools": ["bash", "edit"],
"infer": True,
}
]
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
custom_agents=custom_agents,
)
try:
import re
assert session.session_id is not None
assert re.match(r"^[a-f0-9-]+$", session.session_id)
finally:
await session.disconnect()
async def test_should_handle_custom_agent_with_mcp_servers(self, ctx: E2ETestContext):
"""A custom agent can declare its own MCP servers."""
custom_agents: list[CustomAgentConfig] = [
{
"name": "mcp-agent",
"display_name": "MCP Agent",
"description": "An agent with its own MCP servers",
"prompt": "You are an agent with MCP servers.",
"mcp_servers": _test_mcp_servers("agent-server"),
}
]
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
custom_agents=custom_agents,
)
try:
import re
assert session.session_id is not None
assert re.match(r"^[a-f0-9-]+$", session.session_id)
finally:
await session.disconnect()
async def test_should_handle_multiple_custom_agents(self, ctx: E2ETestContext):
"""Multiple custom agents can be configured at once."""
custom_agents: list[CustomAgentConfig] = [
{
"name": "agent1",
"display_name": "Agent One",
"description": "First agent",
"prompt": "You are agent one.",
},
{
"name": "agent2",
"display_name": "Agent Two",
"description": "Second agent",
"prompt": "You are agent two.",
"infer": False,
},
]
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
custom_agents=custom_agents,
)
try:
import re
assert session.session_id is not None
assert re.match(r"^[a-f0-9-]+$", session.session_id)
finally:
await session.disconnect()