-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathtest_types.py
More file actions
93 lines (81 loc) · 3.21 KB
/
test_types.py
File metadata and controls
93 lines (81 loc) · 3.21 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
import pytest
from mcp.types import (
LATEST_PROTOCOL_VERSION,
ClientCapabilities,
ClientRequest,
Implementation,
InitializeRequest,
InitializeRequestParams,
JSONRPCMessage,
JSONRPCRequest,
Tool,
)
@pytest.mark.anyio
async def test_jsonrpc_request():
json_data = {
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": LATEST_PROTOCOL_VERSION,
"capabilities": {"batch": None, "sampling": None},
"clientInfo": {"name": "mcp", "version": "0.1.0"},
},
}
request = JSONRPCMessage.model_validate(json_data)
assert isinstance(request.root, JSONRPCRequest)
ClientRequest.model_validate(request.model_dump(by_alias=True, exclude_none=True))
assert request.root.jsonrpc == "2.0"
assert request.root.id == 1
assert request.root.method == "initialize"
assert request.root.params is not None
assert request.root.params["protocolVersion"] == LATEST_PROTOCOL_VERSION
@pytest.mark.anyio
async def test_method_initialization():
"""
Test that the method is automatically set on object creation.
Testing just for InitializeRequest to keep the test simple, but should be set for other types as well.
"""
initialize_request = InitializeRequest(
params=InitializeRequestParams(
protocolVersion=LATEST_PROTOCOL_VERSION,
capabilities=ClientCapabilities(),
clientInfo=Implementation(
name="mcp",
version="0.1.0",
),
)
)
assert initialize_request.method == "initialize", "method should be set to 'initialize'"
assert initialize_request.params is not None
assert initialize_request.params.protocolVersion == LATEST_PROTOCOL_VERSION
@pytest.mark.parametrize(
"name",
[
"getUser",
"DATA_EXPORT_v2",
"admin.tools.list",
"a",
"Z9_.-",
"x" * 128, # max length
],
)
def test_tool_allows_valid_names(name: str) -> None:
Tool(name=name, inputSchema={"type": "object"})
@pytest.mark.parametrize(
("name", "expected"),
[
("", "Invalid tool name length: 0. Tool name must be between 1 and 128 characters."),
("x" * 129, "Invalid tool name length: 129. Tool name must be between 1 and 128 characters."),
("has space", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
("comma,name", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
("not/allowed", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
("name@", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
("name#", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
("name$", "Invalid tool name characters. Allowed: A-Z, a-z, 0-9, underscore (_), dash (-), dot (.)."),
],
)
def test_tool_rejects_invalid_names(name: str, expected: str) -> None:
with pytest.raises(ValueError) as exc_info:
Tool(name=name, inputSchema={"type": "object"})
assert expected in str(exc_info.value)