Repository navigation
Expand file tree
/
Copy pathtest_tools.py
More file actions
467 lines (372 loc) · 17.4 KB
/
Copy pathtest_tools.py
File metadata and controls
467 lines (372 loc) · 17.4 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"""Tool interactions against MCPServer, driven through the public Client API."""
import logging
from typing import Annotated, Literal
import pytest
from inline_snapshot import snapshot
from mcp_types import (
INVALID_PARAMS,
URL_ELICITATION_REQUIRED,
CallToolResult,
ElicitRequestURLParams,
ErrorData,
LoggingMessageNotification,
LoggingMessageNotificationParams,
TextContent,
)
from pydantic import BaseModel, Field
from mcp import MCPError
from mcp.client import IncomingMessage
from mcp.server.mcpserver import Context, MCPServer
from mcp.server.mcpserver.exceptions import ToolError
from mcp.shared.exceptions import UrlElicitationRequiredError
from tests._stamp import Unstamp
from tests.interaction._connect import Connect
from tests.interaction._requirements import requirement
pytestmark = pytest.mark.anyio
@requirement("tools:call:content:text")
async def test_call_tool_returns_text_content(connect: Connect, unstamped: Unstamp) -> None:
"""Arguments reach the tool function; its return value comes back as text content.
MCPServer also derives an output schema from the return annotation and attaches the
matching structuredContent to the result.
"""
mcp = MCPServer("adder")
@mcp.tool()
def add(a: int, b: int) -> str:
return str(a + b)
async with connect(mcp) as client:
result = await client.call_tool("add", {"a": 2, "b": 3})
assert unstamped(result) == snapshot(
CallToolResult(content=[TextContent(text="5")], structured_content={"result": "5"})
)
@requirement("mcpserver:tool:schema-variants")
async def test_complex_parameter_types_are_validated_and_coerced_before_the_tool_runs(
connect: Connect, unstamped: Unstamp
) -> None:
"""Literal, nested-model, and constrained parameters are validated and coerced from the wire arguments.
The string "3" is coerced to `int` and the `point` dict to a `Point` instance before the function
body sees them, proving the generated input schema and validation pipeline cover non-trivial types.
"""
mcp = MCPServer("typed")
class Point(BaseModel):
x: int
y: int
@mcp.tool()
def place(mode: Literal["fast", "slow"], point: Point, count: Annotated[int, Field(ge=1, le=10)]) -> str:
assert isinstance(point, Point)
return f"{mode} at ({point.x}, {point.y}) x{count}"
async with connect(mcp) as client:
result = await client.call_tool("place", {"mode": "fast", "point": {"x": "3", "y": 4}, "count": 5})
assert unstamped(result) == snapshot(
CallToolResult(
content=[TextContent(text="fast at (3, 4) x5")], structured_content={"result": "fast at (3, 4) x5"}
)
)
@requirement("mcpserver:tool:handler-throws")
@requirement("mcpserver:output-schema:skip-on-error")
async def test_call_tool_function_exception_becomes_error_result(connect: Connect, unstamped: Unstamp) -> None:
"""An exception raised by a tool function is returned as an is_error result, not a JSON-RPC error.
The exception's own text ("boom") is withheld: an unexpected exception is a crash, and only
ToolError carries a message to the client. The function's `-> str` annotation gives the tool
a derived output schema, but the error result is built before any schema validation runs.
"""
mcp = MCPServer("errors")
@mcp.tool()
def explode() -> str:
raise ValueError("boom")
async with connect(mcp) as client:
result = await client.call_tool("explode", {})
assert unstamped(result) == snapshot(
CallToolResult(content=[TextContent(text="Error executing tool explode")], is_error=True)
)
@requirement("mcpserver:tool:handler-throws")
async def test_call_tool_tool_error_becomes_error_result(connect: Connect, unstamped: Unstamp) -> None:
"""A ToolError raised by a tool function is returned as an is_error result, not a JSON-RPC error."""
mcp = MCPServer("errors")
@mcp.tool()
def flux() -> str:
raise ToolError("flux capacitor offline")
async with connect(mcp) as client:
result = await client.call_tool("flux", {})
assert unstamped(result) == snapshot(
CallToolResult(content=[TextContent(text="Error executing tool flux: flux capacitor offline")], is_error=True)
)
@requirement("mcpserver:tool:unknown-name")
async def test_call_tool_unknown_name_raises_invalid_params(connect: Connect) -> None:
"""The spec classifies an unknown tool name as a JSON-RPC invalid params error."""
mcp = MCPServer("errors")
@mcp.tool()
def add() -> None:
"""A registered tool; the test calls a different name."""
raise NotImplementedError
async with connect(mcp) as client:
with pytest.raises(MCPError) as exc_info:
await client.call_tool("nope", {})
assert exc_info.value.code == INVALID_PARAMS
assert exc_info.value.error == snapshot(ErrorData(code=-32602, message="Unknown tool: nope"))
@requirement("mcpserver:tool:output-schema:model")
@requirement("tools:call:structured-content:text-mirror")
async def test_call_tool_model_return_becomes_structured_content(connect: Connect, unstamped: Unstamp) -> None:
"""A tool returning a pydantic model advertises the model's schema as the tool's output schema
and returns the model's fields as structured content alongside a serialised text block.
"""
mcp = MCPServer("weather")
class Weather(BaseModel):
temperature: float
conditions: str
@mcp.tool()
def get_weather() -> Weather:
return Weather(temperature=22.5, conditions="sunny")
async with connect(mcp) as client:
listed = await client.list_tools()
result = await client.call_tool("get_weather", {})
assert listed.tools[0].output_schema == snapshot(
{
"properties": {
"temperature": {"title": "Temperature", "type": "number"},
"conditions": {"title": "Conditions", "type": "string"},
},
"required": ["temperature", "conditions"],
"title": "Weather",
"type": "object",
}
)
assert unstamped(result) == snapshot(
CallToolResult(
content=[
TextContent(
text="""\
{
"temperature": 22.5,
"conditions": "sunny"
}\
"""
)
],
structured_content={"temperature": 22.5, "conditions": "sunny"},
)
)
@requirement("mcpserver:tool:output-schema:wrapped")
async def test_call_tool_list_return_is_wrapped_in_result_key(connect: Connect, unstamped: Unstamp) -> None:
"""A tool returning a list wraps the value under a "result" key in both the generated output
schema and the structured content.
"""
mcp = MCPServer("primes")
@mcp.tool()
def primes() -> list[int]:
return [2, 3, 5]
async with connect(mcp) as client:
listed = await client.list_tools()
result = await client.call_tool("primes", {})
assert listed.tools[0].output_schema == snapshot(
{
"properties": {"result": {"items": {"type": "integer"}, "title": "Result", "type": "array"}},
"required": ["result"],
"title": "primesOutput",
"type": "object",
}
)
assert unstamped(result) == snapshot(
CallToolResult(
content=[TextContent(text="2"), TextContent(text="3"), TextContent(text="5")],
structured_content={"result": [2, 3, 5]},
)
)
@requirement("mcpserver:tool:input-validation")
async def test_call_tool_invalid_arguments_become_error_result(connect: Connect) -> None:
"""Arguments that fail validation against the tool's signature are reported as an is_error
result describing the failure, not as a protocol error.
"""
mcp = MCPServer("adder")
@mcp.tool()
def add(a: int, b: int) -> str:
"""Validation rejects the arguments before the function is ever called."""
raise NotImplementedError
async with connect(mcp) as client:
result = await client.call_tool("add", {"b": 3})
# The description is raw pydantic output -- it embeds a pydantic-version-specific
# errors.pydantic.dev URL and the internal `addArguments` model name -- so only the stable
# prefix is asserted; a full snapshot would break on every pydantic upgrade.
assert result.is_error is True
assert isinstance(result.content[0], TextContent)
assert result.content[0].text.startswith("Error executing tool add: 1 validation error")
@requirement("mcpserver:output-schema:server-validate")
@requirement("mcpserver:output-schema:missing-structured")
async def test_tool_with_output_schema_returning_mismatched_structured_content_is_an_error_result(
connect: Connect, unstamped: Unstamp
) -> None:
"""Structured content that fails the tool's own output schema is rejected on the server side.
A tool annotated `Annotated[CallToolResult, Model]` returns a hand-built CallToolResult while
declaring `Model` as its output schema; MCPServer validates the supplied structured_content
against that schema before returning. The two cases -- a content shape that does not match,
and no structured content at all -- both fail that validation and are reported as is_error
results carrying the (raw pydantic) validation error wrapped in the SDK's stable prefix.
"""
mcp = MCPServer("forecaster")
class Weather(BaseModel):
temperature: float
conditions: str
@mcp.tool()
def mismatched() -> Annotated[CallToolResult, Weather]:
return CallToolResult(content=[TextContent(text="oops")], structured_content={"nope": True})
@mcp.tool()
def missing() -> Annotated[CallToolResult, Weather]:
return CallToolResult(content=[TextContent(text="oops")])
async with connect(mcp) as client:
mismatched_result = await client.call_tool("mismatched", {})
missing_result = await client.call_tool("missing", {})
# A return value that fails its own output schema is the tool's bug, so the pydantic detail
# goes to the server log and the client gets only the generic crash text.
assert unstamped(mismatched_result) == snapshot(
CallToolResult(content=[TextContent(text="Error executing tool mismatched")], is_error=True)
)
assert unstamped(missing_result) == snapshot(
CallToolResult(content=[TextContent(text="Error executing tool missing")], is_error=True)
)
@requirement("mcpserver:output-schema:skip-on-error")
async def test_tool_with_output_schema_returning_is_error_result_skips_output_validation(
connect: Connect, unstamped: Unstamp
) -> None:
"""A deliberate is_error result from a tool with an output schema reaches the client as written.
The tool declares `Weather` as its output schema but returns a hand-built
`CallToolResult(is_error=True)` with no structured content. An error result has nothing to
validate, so the author's message is delivered instead of being replaced by a validation failure.
"""
mcp = MCPServer("forecaster")
class Weather(BaseModel):
temperature: float
conditions: str
@mcp.tool()
def forecast() -> Annotated[CallToolResult, Weather]:
return CallToolResult(content=[TextContent(text="Upstream is down, try again later.")], is_error=True)
async with connect(mcp) as client:
result = await client.call_tool("forecast", {})
assert unstamped(result) == snapshot(
CallToolResult(content=[TextContent(text="Upstream is down, try again later.")], is_error=True)
)
@requirement("mcpserver:tool:duplicate-name")
async def test_registering_a_duplicate_tool_name_warns_and_keeps_the_first(
connect: Connect, unstamped: Unstamp
) -> None:
"""Registering a second tool with an already-used name keeps the first registration.
The intended behaviour is rejection at registration time; MCPServer instead logs a warning
and discards the second registration (see the divergence note on the requirement). The
second function is registered via add_tool with an explicit name so the test does not
redefine the same function name in this scope.
"""
mcp = MCPServer("duplicates")
@mcp.tool()
def echo() -> str:
return "first"
def echo_second() -> str:
"""Passed to add_tool with a duplicate name; the registration is discarded so this never runs."""
raise NotImplementedError
mcp.add_tool(echo_second, name="echo")
async with connect(mcp) as client:
listed = await client.list_tools()
result = await client.call_tool("echo", {})
assert [tool.name for tool in listed.tools] == ["echo"]
assert unstamped(result) == snapshot(
CallToolResult(content=[TextContent(text="first")], structured_content={"result": "first"})
)
@requirement("mcpserver:tool:naming-validation")
async def test_registering_a_tool_with_a_spec_invalid_name_warns_but_does_not_reject(
connect: Connect, caplog: pytest.LogCaptureFixture, unstamped: Unstamp
) -> None:
"""A tool name that violates the SEP-986 rules logs a warning at registration but is still registered.
The intended behaviour is rejection at registration time; MCPServer instead logs the
naming-rule violation and proceeds (see the divergence note on the requirement). The warning
spans several SDK-authored log records, so only the stable prefix and inclusion of the
offending name are asserted.
"""
mcp = MCPServer("naming")
with caplog.at_level(logging.WARNING, logger="mcp.shared.tool_name_validation"):
@mcp.tool(name="bad name!")
def bad() -> str:
return "ok"
assert any(
rec.levelno == logging.WARNING
and rec.message.startswith("Tool name validation warning")
and "bad name!" in rec.message
for rec in caplog.records
)
async with connect(mcp) as client:
listed = await client.list_tools()
result = await client.call_tool("bad name!", {})
assert [tool.name for tool in listed.tools] == ["bad name!"]
assert unstamped(result) == snapshot(
CallToolResult(content=[TextContent(text="ok")], structured_content={"result": "ok"})
)
@requirement("mcpserver:tool:url-elicitation-error")
async def test_decorated_tool_raising_url_elicitation_required_surfaces_as_error_32042(connect: Connect) -> None:
"""A decorated tool raising the URL-elicitation-required error reaches the client as error -32042.
MCPServer wraps every other tool exception as an is_error result; this error is special-cased
so it propagates as the JSON-RPC error the client needs in order to present the listed URL
interactions and retry the call.
"""
mcp = MCPServer("authorizer")
@mcp.tool()
def read_files() -> str:
raise UrlElicitationRequiredError(
[
ElicitRequestURLParams(
message="Authorization required for your files.",
url="https://example.com/oauth/authorize",
elicitation_id="auth-001",
)
]
)
async with connect(mcp) as client:
with pytest.raises(MCPError) as exc_info:
await client.call_tool("read_files", {})
assert exc_info.value.error.code == URL_ELICITATION_REQUIRED
assert exc_info.value.error == snapshot(
ErrorData(
code=-32042,
message="URL elicitation required",
data={
"elicitations": [
{
"mode": "url",
"message": "Authorization required for your files.",
"url": "https://example.com/oauth/authorize",
"elicitationId": "auth-001",
}
]
},
)
)
@requirement("mcpserver:register:post-connect")
async def test_adding_and_removing_tools_does_not_notify_connected_clients(connect: Connect) -> None:
"""Mutating the tool set on a running server changes tools/list but sends no notification.
add_tool and remove_tool only update the registry: a connected client that listed the tools
before the mutation has no way to learn it should list them again. The spec provides
notifications/tools/list_changed for exactly this; MCPServer never sends it. The tool emits
one log message as a sentinel so the test proves notifications do reach the collector -- the
log message arrives, a list_changed does not.
"""
received: list[IncomingMessage] = []
mcp = MCPServer("mutable")
def extra() -> str:
"""A tool registered at runtime; never called."""
raise NotImplementedError
@mcp.tool()
def doomed() -> str:
"""A tool removed at runtime; never called."""
raise NotImplementedError
@mcp.tool()
async def grow(ctx: Context) -> str:
mcp.add_tool(extra, name="extra")
mcp.remove_tool("doomed")
await ctx.info("tool set changed") # pyright: ignore[reportDeprecated]
return "mutated"
async def collect(message: IncomingMessage) -> None:
received.append(message)
async with connect(mcp, message_handler=collect, log_level="debug") as client:
before = await client.list_tools()
await client.call_tool("grow", {})
after = await client.list_tools()
assert [tool.name for tool in before.tools] == ["doomed", "grow"]
assert [tool.name for tool in after.tools] == ["grow", "extra"]
assert received == snapshot(
[LoggingMessageNotification(params=LoggingMessageNotificationParams(level="info", data="tool set changed"))]
)