pytest-copilot 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytest_copilot/__init__.py +133 -0
- pytest_copilot/copilot/__init__.py +47 -0
- pytest_copilot/copilot/_command_job.py +109 -0
- pytest_copilot/copilot/api.py +12 -0
- pytest_copilot/copilot/client.py +58 -0
- pytest_copilot/copilot/commands.py +265 -0
- pytest_copilot/copilot/config.py +133 -0
- pytest_copilot/copilot/contracts.py +222 -0
- pytest_copilot/copilot/controls.py +230 -0
- pytest_copilot/copilot/events.py +795 -0
- pytest_copilot/copilot/fixtures.py +289 -0
- pytest_copilot/copilot/personas.py +607 -0
- pytest_copilot/copilot/requests.py +350 -0
- pytest_copilot/copilot/result.py +309 -0
- pytest_copilot/copilot/runner.py +266 -0
- pytest_copilot/copilot/skills.py +104 -0
- pytest_copilot/copilot/task.py +546 -0
- pytest_copilot/core/__init__.py +77 -0
- pytest_copilot/core/errors.py +44 -0
- pytest_copilot/core/evals.py +521 -0
- pytest_copilot/core/plugin.py +403 -0
- pytest_copilot/core/prompt.py +167 -0
- pytest_copilot/core/result.py +543 -0
- pytest_copilot/core/serialization.py +128 -0
- pytest_copilot/core/skill.py +406 -0
- pytest_copilot/core/skill_evals.py +148 -0
- pytest_copilot/core/skill_grading.py +78 -0
- pytest_copilot/core/validation.py +83 -0
- pytest_copilot/execution/__init__.py +15 -0
- pytest_copilot/execution/cost.py +159 -0
- pytest_copilot/execution/rate_limiter.py +177 -0
- pytest_copilot/execution/servers.py +442 -0
- pytest_copilot/execution/skill_tools.py +228 -0
- pytest_copilot/fixtures/__init__.py +18 -0
- pytest_copilot/fixtures/factories.py +44 -0
- pytest_copilot/fixtures/iteration.py +16 -0
- pytest_copilot/onboarding.py +413 -0
- pytest_copilot/plugin.py +562 -0
- pytest_copilot/plugin_options.py +32 -0
- pytest_copilot/plugin_report.py +18 -0
- pytest_copilot/reporting/__init__.py +16 -0
- pytest_copilot/reporting/collector.py +203 -0
- pytest_copilot/reporting/generator.py +55 -0
- pytest_copilot/reporting/schema.py +5 -0
- pytest_copilot/testing/__init__.py +17 -0
- pytest_copilot/testing/banking.py +479 -0
- pytest_copilot/testing/banking_mcp.py +167 -0
- pytest_copilot/testing/todo.py +380 -0
- pytest_copilot/testing/todo_mcp.py +156 -0
- pytest_copilot/testing/types.py +31 -0
- pytest_copilot-2.0.0.dist-info/METADATA +177 -0
- pytest_copilot-2.0.0.dist-info/RECORD +55 -0
- pytest_copilot-2.0.0.dist-info/WHEEL +4 -0
- pytest_copilot-2.0.0.dist-info/entry_points.txt +5 -0
- pytest_copilot-2.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
"""Server management for MCP servers.
|
|
2
|
+
|
|
3
|
+
Provides a process class for connecting to MCP servers,
|
|
4
|
+
plus configuration dataclasses (``MCPServer``, ``Wait``).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
import contextlib
|
|
11
|
+
import logging
|
|
12
|
+
import os
|
|
13
|
+
import time
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
from enum import Enum
|
|
16
|
+
from typing import TYPE_CHECKING, Any, Literal
|
|
17
|
+
|
|
18
|
+
from pytest_copilot.core.errors import ServerStartError
|
|
19
|
+
|
|
20
|
+
_logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from mcp import ClientSession
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class WaitStrategy(str, Enum):
|
|
27
|
+
"""Strategy for waiting until an MCP server is ready."""
|
|
28
|
+
|
|
29
|
+
TOOLS = "tools"
|
|
30
|
+
"""Wait until the server exposes the expected tools."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(slots=True, frozen=True)
|
|
34
|
+
class Wait:
|
|
35
|
+
"""Server startup wait configuration.
|
|
36
|
+
|
|
37
|
+
Example::
|
|
38
|
+
|
|
39
|
+
# Wait until specific tools are available
|
|
40
|
+
Wait.for_tools(["read_file", "write_file"])
|
|
41
|
+
|
|
42
|
+
# No wait — connect immediately
|
|
43
|
+
Wait(strategy=WaitStrategy.TOOLS, tools=[])
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
strategy: WaitStrategy = WaitStrategy.TOOLS
|
|
47
|
+
tools: list[str] = field(default_factory=list)
|
|
48
|
+
timeout_ms: int = 30_000
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def for_tools(cls, tools: list[str], timeout_ms: int = 30_000) -> Wait:
|
|
52
|
+
"""Wait until the server exposes the given tools."""
|
|
53
|
+
return cls(strategy=WaitStrategy.TOOLS, tools=tools, timeout_ms=timeout_ms)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(slots=True)
|
|
57
|
+
class MCPServer:
|
|
58
|
+
"""Configuration for an MCP server started by MCPServerProcess.
|
|
59
|
+
|
|
60
|
+
Supports stdio (subprocess), SSE, and Streamable HTTP transports.
|
|
61
|
+
|
|
62
|
+
Example::
|
|
63
|
+
|
|
64
|
+
# Stdio — launch a subprocess
|
|
65
|
+
MCPServer(command=["python", "-m", "my_server"])
|
|
66
|
+
|
|
67
|
+
# Stdio with wait — start server and check tools are ready
|
|
68
|
+
MCPServer(
|
|
69
|
+
command=["python", "-m", "my_server"],
|
|
70
|
+
wait=Wait.for_tools(["read_file", "write_file"]),
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# SSE — connect to a remote endpoint
|
|
74
|
+
MCPServer(
|
|
75
|
+
transport="sse",
|
|
76
|
+
url="http://localhost:8080/sse",
|
|
77
|
+
headers={"Authorization": "Bearer ${MY_TOKEN}"},
|
|
78
|
+
)
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
command: list[str] = field(default_factory=list)
|
|
82
|
+
"""Command to launch the server (required for stdio transport)."""
|
|
83
|
+
transport: Literal["stdio", "sse", "streamable-http"] = "stdio"
|
|
84
|
+
args: list[str] = field(default_factory=list)
|
|
85
|
+
env: dict[str, str] = field(default_factory=dict)
|
|
86
|
+
cwd: str | None = None
|
|
87
|
+
url: str | None = None
|
|
88
|
+
"""URL for SSE or Streamable HTTP transports."""
|
|
89
|
+
headers: dict[str, str] = field(default_factory=dict)
|
|
90
|
+
wait: Wait = field(default_factory=Wait)
|
|
91
|
+
|
|
92
|
+
def __post_init__(self) -> None:
|
|
93
|
+
if self.transport in ("sse", "streamable-http") and not self.url:
|
|
94
|
+
msg = f"transport={self.transport!r} requires a url"
|
|
95
|
+
raise ValueError(msg)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _expand_env(value: str) -> str:
|
|
99
|
+
"""Expand environment variables in a string."""
|
|
100
|
+
return os.path.expandvars(value)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class MCPServerProcess:
|
|
104
|
+
"""Manages an MCP server connection using the MCP SDK.
|
|
105
|
+
|
|
106
|
+
Supports all three transports via the SDK's ``ClientSession``:
|
|
107
|
+
|
|
108
|
+
- **stdio** — Launches a subprocess and communicates via stdin/stdout
|
|
109
|
+
- **sse** — Connects to a remote SSE endpoint
|
|
110
|
+
- **streamable-http** — Connects to a remote Streamable HTTP endpoint
|
|
111
|
+
|
|
112
|
+
Example:
|
|
113
|
+
server = MCPServerProcess(mcp_config)
|
|
114
|
+
await server.start()
|
|
115
|
+
tools = server.get_tools()
|
|
116
|
+
result = await server.call_tool("read_file", {"path": "foo.txt"})
|
|
117
|
+
await server.stop()
|
|
118
|
+
"""
|
|
119
|
+
|
|
120
|
+
def __init__(self, config: MCPServer) -> None:
|
|
121
|
+
self.config = config
|
|
122
|
+
self._session: ClientSession | None = None
|
|
123
|
+
self._tools: dict[str, dict[str, Any]] = {}
|
|
124
|
+
self._prompts: dict[str, Any] = {}
|
|
125
|
+
self._exit_stack: contextlib.AsyncExitStack | None = None
|
|
126
|
+
|
|
127
|
+
async def start(self) -> None:
|
|
128
|
+
"""Start or connect to the MCP server and discover tools and prompts."""
|
|
129
|
+
from mcp import ClientSession as _ClientSession
|
|
130
|
+
|
|
131
|
+
self._exit_stack = contextlib.AsyncExitStack()
|
|
132
|
+
label = self._transport_label()
|
|
133
|
+
|
|
134
|
+
try:
|
|
135
|
+
read_stream, write_stream = await self._open_transport()
|
|
136
|
+
|
|
137
|
+
self._session = await self._exit_stack.enter_async_context(
|
|
138
|
+
_ClientSession(read_stream, write_stream)
|
|
139
|
+
)
|
|
140
|
+
await self._session.initialize()
|
|
141
|
+
|
|
142
|
+
if self.config.wait.strategy == WaitStrategy.TOOLS and self.config.wait.tools:
|
|
143
|
+
await self._wait_for_required_tools(label)
|
|
144
|
+
else:
|
|
145
|
+
await self._refresh_capabilities()
|
|
146
|
+
|
|
147
|
+
except ServerStartError:
|
|
148
|
+
await self._cleanup_failed_start("MCP startup")
|
|
149
|
+
self._session = None
|
|
150
|
+
raise
|
|
151
|
+
except Exception as e:
|
|
152
|
+
await self._cleanup_failed_start("MCP startup")
|
|
153
|
+
self._session = None
|
|
154
|
+
raise ServerStartError("MCP", label, str(e)) from e
|
|
155
|
+
|
|
156
|
+
async def _cleanup_failed_start(self, context: str) -> None:
|
|
157
|
+
"""Best-effort cleanup for startup failures."""
|
|
158
|
+
if self._exit_stack is None:
|
|
159
|
+
return
|
|
160
|
+
exit_stack = self._exit_stack
|
|
161
|
+
self._exit_stack = None
|
|
162
|
+
try:
|
|
163
|
+
await exit_stack.aclose()
|
|
164
|
+
except Exception:
|
|
165
|
+
_logger.warning("%s cleanup failed", context, exc_info=True)
|
|
166
|
+
|
|
167
|
+
async def _refresh_capabilities(self) -> None:
|
|
168
|
+
"""Refresh tool and prompt metadata from the connected server."""
|
|
169
|
+
if self._session is None:
|
|
170
|
+
raise RuntimeError("Server not started")
|
|
171
|
+
|
|
172
|
+
tools_result = await self._session.list_tools()
|
|
173
|
+
self._tools = {
|
|
174
|
+
tool.name: {
|
|
175
|
+
"name": tool.name,
|
|
176
|
+
"description": tool.description or "",
|
|
177
|
+
"inputSchema": tool.input_schema,
|
|
178
|
+
}
|
|
179
|
+
for tool in tools_result.tools
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
self._prompts = {}
|
|
183
|
+
try:
|
|
184
|
+
prompts_result = await self._session.list_prompts()
|
|
185
|
+
from pytest_copilot.core.result import MCPPrompt, MCPPromptArgument
|
|
186
|
+
|
|
187
|
+
for prompt in prompts_result.prompts:
|
|
188
|
+
args = [
|
|
189
|
+
MCPPromptArgument(
|
|
190
|
+
name=a.name,
|
|
191
|
+
description=a.description or "",
|
|
192
|
+
required=bool(a.required),
|
|
193
|
+
)
|
|
194
|
+
for a in (prompt.arguments or [])
|
|
195
|
+
]
|
|
196
|
+
self._prompts[prompt.name] = MCPPrompt(
|
|
197
|
+
name=prompt.name,
|
|
198
|
+
description=prompt.description or "",
|
|
199
|
+
arguments=args,
|
|
200
|
+
)
|
|
201
|
+
except Exception:
|
|
202
|
+
_logger.debug("Server does not support prompts capability", exc_info=True)
|
|
203
|
+
|
|
204
|
+
async def _wait_for_required_tools(self, label: list[str]) -> None:
|
|
205
|
+
"""Poll until all required tools are exposed or the timeout elapses."""
|
|
206
|
+
required_tools = tuple(self.config.wait.tools)
|
|
207
|
+
deadline = time.monotonic() + (self.config.wait.timeout_ms / 1000)
|
|
208
|
+
delay_s = 0.05
|
|
209
|
+
last_error: Exception | None = None
|
|
210
|
+
|
|
211
|
+
while True:
|
|
212
|
+
try:
|
|
213
|
+
await self._refresh_capabilities()
|
|
214
|
+
last_error = None
|
|
215
|
+
except Exception as exc:
|
|
216
|
+
last_error = exc
|
|
217
|
+
|
|
218
|
+
available_tools = set(self._tools)
|
|
219
|
+
missing = sorted(set(required_tools) - available_tools)
|
|
220
|
+
if not missing:
|
|
221
|
+
return
|
|
222
|
+
|
|
223
|
+
remaining = deadline - time.monotonic()
|
|
224
|
+
if remaining <= 0:
|
|
225
|
+
details = [
|
|
226
|
+
"Required tools not available.",
|
|
227
|
+
"Timed out after "
|
|
228
|
+
f"{self.config.wait.timeout_ms}ms waiting for tools {list(required_tools)}.",
|
|
229
|
+
f"Last visible tools: {sorted(available_tools)}.",
|
|
230
|
+
]
|
|
231
|
+
if last_error is not None:
|
|
232
|
+
details.append(f"Last discovery error: {last_error}")
|
|
233
|
+
raise ServerStartError("MCP", label, " ".join(details))
|
|
234
|
+
|
|
235
|
+
await asyncio.sleep(min(delay_s, remaining))
|
|
236
|
+
delay_s = min(delay_s * 2, 0.5)
|
|
237
|
+
|
|
238
|
+
async def _open_transport(self) -> tuple[Any, Any]:
|
|
239
|
+
"""Open the appropriate transport and return (read_stream, write_stream)."""
|
|
240
|
+
assert self._exit_stack is not None # noqa: S101
|
|
241
|
+
|
|
242
|
+
match self.config.transport:
|
|
243
|
+
case "stdio":
|
|
244
|
+
from mcp.client.stdio import StdioServerParameters, stdio_client
|
|
245
|
+
|
|
246
|
+
cmd = self.config.command
|
|
247
|
+
if not cmd or not isinstance(cmd[0], str) or not cmd[0].strip():
|
|
248
|
+
raise ServerStartError(
|
|
249
|
+
"MCP",
|
|
250
|
+
self._transport_label(),
|
|
251
|
+
"stdio transport requires a non-empty command",
|
|
252
|
+
)
|
|
253
|
+
params = StdioServerParameters(
|
|
254
|
+
command=cmd[0],
|
|
255
|
+
args=[*cmd[1:], *self.config.args],
|
|
256
|
+
env={**os.environ, **self.config.env} if self.config.env else None,
|
|
257
|
+
cwd=self.config.cwd,
|
|
258
|
+
)
|
|
259
|
+
streams = await self._exit_stack.enter_async_context(stdio_client(params))
|
|
260
|
+
return streams[0], streams[1]
|
|
261
|
+
|
|
262
|
+
case "sse":
|
|
263
|
+
from mcp.client.sse import sse_client
|
|
264
|
+
|
|
265
|
+
url = self.config.url
|
|
266
|
+
assert url is not None # noqa: S101 — validated in MCPServer.__post_init__
|
|
267
|
+
headers = {k: _expand_env(v) for k, v in self.config.headers.items()}
|
|
268
|
+
streams = await self._exit_stack.enter_async_context(
|
|
269
|
+
sse_client(url, headers=headers or None)
|
|
270
|
+
)
|
|
271
|
+
return streams[0], streams[1]
|
|
272
|
+
|
|
273
|
+
case "streamable-http":
|
|
274
|
+
import httpx2
|
|
275
|
+
from mcp.client.streamable_http import streamable_http_client
|
|
276
|
+
|
|
277
|
+
url = self.config.url
|
|
278
|
+
assert url is not None # noqa: S101 — validated in MCPServer.__post_init__
|
|
279
|
+
|
|
280
|
+
http_client: httpx2.AsyncClient | None = None
|
|
281
|
+
if self.config.headers:
|
|
282
|
+
headers = {k: _expand_env(v) for k, v in self.config.headers.items()}
|
|
283
|
+
http_client = await self._exit_stack.enter_async_context(
|
|
284
|
+
httpx2.AsyncClient(headers=headers)
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
streams = await self._exit_stack.enter_async_context(
|
|
288
|
+
streamable_http_client(url, http_client=http_client)
|
|
289
|
+
)
|
|
290
|
+
return streams[0], streams[1]
|
|
291
|
+
|
|
292
|
+
case _: # pragma: no cover
|
|
293
|
+
msg = f"Unknown transport: {self.config.transport}"
|
|
294
|
+
raise ValueError(msg)
|
|
295
|
+
|
|
296
|
+
raise AssertionError("unreachable") # pragma: no cover
|
|
297
|
+
|
|
298
|
+
async def stop(self) -> None:
|
|
299
|
+
"""Stop the MCP server / disconnect from remote."""
|
|
300
|
+
if self._exit_stack:
|
|
301
|
+
try:
|
|
302
|
+
await self._exit_stack.aclose()
|
|
303
|
+
except Exception:
|
|
304
|
+
_logger.debug(
|
|
305
|
+
"Server cleanup encountered async teardown error",
|
|
306
|
+
exc_info=True,
|
|
307
|
+
)
|
|
308
|
+
finally:
|
|
309
|
+
self._exit_stack = None
|
|
310
|
+
self._session = None
|
|
311
|
+
|
|
312
|
+
def get_tools(self) -> dict[str, dict[str, Any]]:
|
|
313
|
+
"""Get available tools."""
|
|
314
|
+
return self._tools
|
|
315
|
+
|
|
316
|
+
def get_prompts(self) -> dict[str, Any]:
|
|
317
|
+
"""Get available prompt templates discovered from the server.
|
|
318
|
+
|
|
319
|
+
Returns a dict mapping prompt name to :class:`MCPPrompt`.
|
|
320
|
+
Empty if the server does not support the prompts capability.
|
|
321
|
+
"""
|
|
322
|
+
return self._prompts
|
|
323
|
+
|
|
324
|
+
async def list_prompts(self) -> list[Any]:
|
|
325
|
+
"""List prompt templates exposed by the MCP server.
|
|
326
|
+
|
|
327
|
+
Returns a list of :class:`MCPPrompt` objects. Empty list if the server
|
|
328
|
+
does not support the prompts capability.
|
|
329
|
+
|
|
330
|
+
Example::
|
|
331
|
+
|
|
332
|
+
server = MCPServerProcess(mcp_config)
|
|
333
|
+
await server.start()
|
|
334
|
+
prompts = await server.list_prompts()
|
|
335
|
+
for p in prompts:
|
|
336
|
+
print(p.name, p.description)
|
|
337
|
+
"""
|
|
338
|
+
if not self._session:
|
|
339
|
+
raise RuntimeError("Server not started")
|
|
340
|
+
|
|
341
|
+
from pytest_copilot.core.result import MCPPrompt, MCPPromptArgument
|
|
342
|
+
|
|
343
|
+
try:
|
|
344
|
+
result = await self._session.list_prompts()
|
|
345
|
+
except Exception:
|
|
346
|
+
return []
|
|
347
|
+
|
|
348
|
+
prompts = []
|
|
349
|
+
for prompt in result.prompts:
|
|
350
|
+
args = [
|
|
351
|
+
MCPPromptArgument(
|
|
352
|
+
name=a.name,
|
|
353
|
+
description=a.description or "",
|
|
354
|
+
required=bool(a.required),
|
|
355
|
+
)
|
|
356
|
+
for a in (prompt.arguments or [])
|
|
357
|
+
]
|
|
358
|
+
prompts.append(
|
|
359
|
+
MCPPrompt(
|
|
360
|
+
name=prompt.name,
|
|
361
|
+
description=prompt.description or "",
|
|
362
|
+
arguments=args,
|
|
363
|
+
)
|
|
364
|
+
)
|
|
365
|
+
return prompts
|
|
366
|
+
|
|
367
|
+
async def get_prompt(
|
|
368
|
+
self,
|
|
369
|
+
name: str,
|
|
370
|
+
arguments: dict[str, str] | None = None,
|
|
371
|
+
) -> list[dict[str, Any]]:
|
|
372
|
+
"""Render a prompt template from the MCP server.
|
|
373
|
+
|
|
374
|
+
Calls ``prompts/get`` with the given arguments and returns the assembled
|
|
375
|
+
messages as a list of dicts with ``role`` and ``content`` keys — ready to
|
|
376
|
+
use as test input or assertions.
|
|
377
|
+
|
|
378
|
+
Args:
|
|
379
|
+
name: Prompt name as returned by :meth:`list_prompts`.
|
|
380
|
+
arguments: Argument values to fill in the template.
|
|
381
|
+
|
|
382
|
+
Returns:
|
|
383
|
+
List of ``{"role": str, "content": str}`` dicts.
|
|
384
|
+
|
|
385
|
+
Raises:
|
|
386
|
+
RuntimeError: If the server is not started.
|
|
387
|
+
ValueError: If the prompt name is not found.
|
|
388
|
+
|
|
389
|
+
Example::
|
|
390
|
+
|
|
391
|
+
messages = await server.get_prompt("code_review", {"code": "def hello(): ..."})
|
|
392
|
+
# [{"role": "user", "content": "Please review this code: def hello(): ..."}]
|
|
393
|
+
result = await copilot_run(agent, messages[0]["content"])
|
|
394
|
+
"""
|
|
395
|
+
if not self._session:
|
|
396
|
+
raise RuntimeError("Server not started")
|
|
397
|
+
|
|
398
|
+
from mcp import types
|
|
399
|
+
|
|
400
|
+
result = await self._session.get_prompt(name, arguments)
|
|
401
|
+
|
|
402
|
+
messages = []
|
|
403
|
+
for msg in result.messages:
|
|
404
|
+
content = ""
|
|
405
|
+
if isinstance(msg.content, types.TextContent):
|
|
406
|
+
content = msg.content.text
|
|
407
|
+
elif isinstance(msg.content, types.EmbeddedResource):
|
|
408
|
+
# Resource content — extract text if available
|
|
409
|
+
resource = msg.content.resource
|
|
410
|
+
if hasattr(resource, "text"):
|
|
411
|
+
content = resource.text # type: ignore[union-attr]
|
|
412
|
+
else:
|
|
413
|
+
content = str(resource)
|
|
414
|
+
else:
|
|
415
|
+
content = str(msg.content)
|
|
416
|
+
role = msg.role.value if hasattr(msg.role, "value") else str(msg.role) # type: ignore[union-attr]
|
|
417
|
+
messages.append({"role": role, "content": content})
|
|
418
|
+
|
|
419
|
+
return messages
|
|
420
|
+
|
|
421
|
+
async def call_tool(self, name: str, arguments: dict[str, Any]) -> str:
|
|
422
|
+
"""Call a tool and return result as text."""
|
|
423
|
+
if not self._session:
|
|
424
|
+
raise RuntimeError("Server not started")
|
|
425
|
+
|
|
426
|
+
from mcp import types
|
|
427
|
+
|
|
428
|
+
result = await self._session.call_tool(name, arguments)
|
|
429
|
+
|
|
430
|
+
# Extract text from content blocks
|
|
431
|
+
if result.content:
|
|
432
|
+
for block in result.content:
|
|
433
|
+
if isinstance(block, types.TextContent):
|
|
434
|
+
return block.text
|
|
435
|
+
return str(result.content[0])
|
|
436
|
+
return ""
|
|
437
|
+
|
|
438
|
+
def _transport_label(self) -> list[str]:
|
|
439
|
+
"""Human-readable label for error messages."""
|
|
440
|
+
if self.config.transport == "stdio":
|
|
441
|
+
return self.config.command + self.config.args
|
|
442
|
+
return [self.config.transport, self.config.url or ""]
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"""Virtual tools for skill references, scripts, and assets.
|
|
2
|
+
|
|
3
|
+
When a skill has a references/, scripts/, or assets/ directory, these tools
|
|
4
|
+
are auto-injected to allow the agent to list and read content on demand.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import TYPE_CHECKING, Any
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from pytest_copilot.core.skill import Skill
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_skill_tools_schema(skill: Skill) -> list[dict[str, Any]]:
|
|
16
|
+
"""Generate OpenAI-compatible tool schemas for skill virtual tools.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
skill: Skill with references/scripts/assets to expose
|
|
20
|
+
|
|
21
|
+
Returns:
|
|
22
|
+
List of tool definitions in OpenAI function format
|
|
23
|
+
"""
|
|
24
|
+
tools: list[dict[str, Any]] = []
|
|
25
|
+
|
|
26
|
+
if skill.has_references:
|
|
27
|
+
tools.extend(
|
|
28
|
+
[
|
|
29
|
+
{
|
|
30
|
+
"type": "function",
|
|
31
|
+
"function": {
|
|
32
|
+
"name": "list_skill_references",
|
|
33
|
+
"description": (
|
|
34
|
+
f"List available reference documents for the '{skill.name}' skill. "
|
|
35
|
+
"Returns filenames that can be read with read_skill_reference."
|
|
36
|
+
),
|
|
37
|
+
"parameters": {
|
|
38
|
+
"type": "object",
|
|
39
|
+
"properties": {},
|
|
40
|
+
"required": [],
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"type": "function",
|
|
46
|
+
"function": {
|
|
47
|
+
"name": "read_skill_reference",
|
|
48
|
+
"description": (
|
|
49
|
+
f"Read a reference document from the '{skill.name}' skill. "
|
|
50
|
+
"Use list_skill_references first to see available files."
|
|
51
|
+
),
|
|
52
|
+
"parameters": {
|
|
53
|
+
"type": "object",
|
|
54
|
+
"properties": {
|
|
55
|
+
"filename": {
|
|
56
|
+
"type": "string",
|
|
57
|
+
"description": "Name of the reference file to read",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
"required": ["filename"],
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
]
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
if skill.has_scripts:
|
|
68
|
+
tools.extend(
|
|
69
|
+
[
|
|
70
|
+
{
|
|
71
|
+
"type": "function",
|
|
72
|
+
"function": {
|
|
73
|
+
"name": "list_skill_scripts",
|
|
74
|
+
"description": (
|
|
75
|
+
f"List available scripts for the '{skill.name}' skill. "
|
|
76
|
+
"Returns filenames that can be read with read_skill_script."
|
|
77
|
+
),
|
|
78
|
+
"parameters": {
|
|
79
|
+
"type": "object",
|
|
80
|
+
"properties": {},
|
|
81
|
+
"required": [],
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"type": "function",
|
|
87
|
+
"function": {
|
|
88
|
+
"name": "read_skill_script",
|
|
89
|
+
"description": (
|
|
90
|
+
f"Read a script from the '{skill.name}' skill. "
|
|
91
|
+
"Use list_skill_scripts first to see available files."
|
|
92
|
+
),
|
|
93
|
+
"parameters": {
|
|
94
|
+
"type": "object",
|
|
95
|
+
"properties": {
|
|
96
|
+
"filename": {
|
|
97
|
+
"type": "string",
|
|
98
|
+
"description": "Name of the script file to read",
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
"required": ["filename"],
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
]
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
if skill.has_assets:
|
|
109
|
+
tools.append(
|
|
110
|
+
{
|
|
111
|
+
"type": "function",
|
|
112
|
+
"function": {
|
|
113
|
+
"name": "list_skill_assets",
|
|
114
|
+
"description": (
|
|
115
|
+
f"List available asset files for the '{skill.name}' skill. "
|
|
116
|
+
"Returns filenames of supporting assets."
|
|
117
|
+
),
|
|
118
|
+
"parameters": {
|
|
119
|
+
"type": "object",
|
|
120
|
+
"properties": {},
|
|
121
|
+
"required": [],
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
return tools
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def execute_skill_tool(
|
|
131
|
+
skill: Skill,
|
|
132
|
+
tool_name: str,
|
|
133
|
+
arguments: dict[str, Any],
|
|
134
|
+
) -> str:
|
|
135
|
+
"""Execute a skill virtual tool.
|
|
136
|
+
|
|
137
|
+
Args:
|
|
138
|
+
skill: Skill containing the resources
|
|
139
|
+
tool_name: Tool name to execute
|
|
140
|
+
arguments: Tool arguments
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
Tool result as string
|
|
144
|
+
|
|
145
|
+
Raises:
|
|
146
|
+
ValueError: If tool_name is unknown
|
|
147
|
+
KeyError: If requested file not found
|
|
148
|
+
"""
|
|
149
|
+
if tool_name == "list_skill_references":
|
|
150
|
+
return _list_references(skill)
|
|
151
|
+
elif tool_name == "read_skill_reference":
|
|
152
|
+
filename = arguments.get("filename", "")
|
|
153
|
+
return _read_reference(skill, filename)
|
|
154
|
+
elif tool_name == "list_skill_scripts":
|
|
155
|
+
return _list_scripts(skill)
|
|
156
|
+
elif tool_name == "read_skill_script":
|
|
157
|
+
filename = arguments.get("filename", "")
|
|
158
|
+
return _read_script(skill, filename)
|
|
159
|
+
elif tool_name == "list_skill_assets":
|
|
160
|
+
return _list_assets(skill)
|
|
161
|
+
else:
|
|
162
|
+
raise ValueError(f"Unknown skill tool: {tool_name}")
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
_SKILL_TOOL_NAMES = frozenset(
|
|
166
|
+
{
|
|
167
|
+
"list_skill_references",
|
|
168
|
+
"read_skill_reference",
|
|
169
|
+
"list_skill_scripts",
|
|
170
|
+
"read_skill_script",
|
|
171
|
+
"list_skill_assets",
|
|
172
|
+
}
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def is_skill_tool(tool_name: str) -> bool:
|
|
177
|
+
"""Check if a tool name is a skill virtual tool."""
|
|
178
|
+
return tool_name in _SKILL_TOOL_NAMES
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _list_references(skill: Skill) -> str:
|
|
182
|
+
"""List available reference files."""
|
|
183
|
+
if not skill.references:
|
|
184
|
+
return "No reference documents available."
|
|
185
|
+
|
|
186
|
+
files = sorted(skill.references.keys())
|
|
187
|
+
return "Available reference documents:\n" + "\n".join(f"- {f}" for f in files)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _read_reference(skill: Skill, filename: str) -> str:
|
|
191
|
+
"""Read a specific reference file."""
|
|
192
|
+
if not filename:
|
|
193
|
+
return "Error: filename parameter is required"
|
|
194
|
+
|
|
195
|
+
if filename not in skill.references:
|
|
196
|
+
available = ", ".join(sorted(skill.references.keys()))
|
|
197
|
+
return f"Error: Reference '{filename}' not found. Available: {available}"
|
|
198
|
+
|
|
199
|
+
return skill.references[filename]
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _list_scripts(skill: Skill) -> str:
|
|
203
|
+
"""List available script files."""
|
|
204
|
+
if not skill.scripts:
|
|
205
|
+
return "No scripts available."
|
|
206
|
+
|
|
207
|
+
files = sorted(skill.scripts.keys())
|
|
208
|
+
return "Available scripts:\n" + "\n".join(f"- {f}" for f in files)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _read_script(skill: Skill, filename: str) -> str:
|
|
212
|
+
"""Read a specific script file."""
|
|
213
|
+
if not filename:
|
|
214
|
+
return "Error: filename parameter is required"
|
|
215
|
+
|
|
216
|
+
if filename not in skill.scripts:
|
|
217
|
+
available = ", ".join(sorted(skill.scripts.keys()))
|
|
218
|
+
return f"Error: Script '{filename}' not found. Available: {available}"
|
|
219
|
+
|
|
220
|
+
return skill.scripts[filename]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _list_assets(skill: Skill) -> str:
|
|
224
|
+
"""List available asset files."""
|
|
225
|
+
if not skill.assets:
|
|
226
|
+
return "No assets available."
|
|
227
|
+
|
|
228
|
+
return "Available assets:\n" + "\n".join(f"- {f}" for f in skill.assets)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Pytest fixtures for pytest-copilot.
|
|
2
|
+
|
|
3
|
+
This module re-exports all fixtures for pytest plugin registration.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
# Re-export fixtures for pytest plugin discovery
|
|
7
|
+
from pytest_copilot.fixtures.factories import skill_factory
|
|
8
|
+
from pytest_copilot.fixtures.iteration import _aitest_iteration
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"_aitest_iteration",
|
|
12
|
+
"skill_factory",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
# Copilot fixtures (required - no longer optional)
|
|
16
|
+
from pytest_copilot.copilot.fixtures import ab_run, copilot_run # noqa: F401
|
|
17
|
+
|
|
18
|
+
__all__ += ["copilot_run", "ab_run"]
|