-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmcp_test.py
More file actions
232 lines (180 loc) Β· 6.25 KB
/
mcp_test.py
File metadata and controls
232 lines (180 loc) Β· 6.25 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
#!/usr/bin/env python3
"""
MCP Test Runner
Runs tests against MCP servers and tools.
Usage:
python mcp_test.py # Run all tests
python mcp_test.py --smoke # Run smoke tests only
python mcp_test.py --tool read_file # Test specific tool
"""
import sys
from pathlib import Path
# Ensure codomyrmex is in path
try:
import codomyrmex
except ImportError:
project_root = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(project_root / "src"))
import argparse
import asyncio
from codomyrmex.model_context_protocol.testing import (
MockMCPClient,
ServerTester,
)
from codomyrmex.model_context_protocol import MCPServer
from codomyrmex.model_context_protocol.validators import (
MessageValidator,
)
def create_test_server():
"""Create a test server with sample tools."""
server = MCPServer()
@server.tool(name="echo", description="Echo back the input")
def echo(message: str) -> str:
return message
@server.tool(name="add", description="Add two numbers")
def add(a: int, b: int) -> int:
return a + b
@server.tool(name="greet", description="Generate a greeting")
def greet(name: str, formal: bool = False) -> str:
if formal:
return f"Good day, {name}."
return f"Hello, {name}!"
return server
async def run_smoke_tests(server) -> None:
"""Run smoke tests."""
print("π§ͺ Running Smoke Tests\n")
tester = ServerTester(server)
suite = await tester.run_smoke_tests()
for result in suite.results:
status = "β" if result.passed else "β"
print(f" {status} {result.name} ({result.duration_ms:.1f}ms)")
if not result.passed and result.error:
print(f" Error: {result.error}")
print(f"\n{suite.summary()}")
async def run_tool_tests(server, tool_name: str) -> None:
"""Run tests for a specific tool."""
print(f"π§ Testing Tool: {tool_name}\n")
client = MockMCPClient(server)
# Initialize first
await client.initialize()
# Get tool list
tools_response = await client.list_tools()
if "result" not in tools_response:
print("β Failed to get tools list")
return
tools = tools_response["result"].get("tools", [])
tool_names = [t.get("name") for t in tools]
if tool_name not in tool_names:
print(f"β Tool '{tool_name}' not found")
print(f" Available: {', '.join(tool_names)}")
return
# Test the tool with sample arguments
print(f" Calling {tool_name}...")
# Build simple test arguments based on tool name
test_args = {}
if tool_name == "echo":
test_args = {"message": "Hello, MCP!"}
elif tool_name == "add":
test_args = {"a": 5, "b": 3}
elif tool_name == "greet":
test_args = {"name": "World"}
response = await client.call_tool(tool_name, test_args)
if "result" in response:
print(" β Call succeeded")
content = response["result"].get("content", [])
if content:
print(f" Output: {content[0].get('text', '')[:100]}")
else:
print(" β Call failed")
if "error" in response:
print(f" Error: {response['error'].get('message')}")
async def run_validation_tests() -> None:
"""Run message validation tests."""
print("π Running Validation Tests\n")
validator = MessageValidator()
test_cases = [
{
"name": "Valid request",
"message": {"jsonrpc": "2.0", "id": 1, "method": "tools/list"},
"type": "request",
"should_pass": True,
},
{
"name": "Missing method",
"message": {"jsonrpc": "2.0", "id": 1},
"type": "request",
"should_pass": False,
},
{
"name": "Valid response",
"message": {"jsonrpc": "2.0", "id": 1, "result": {"tools": []}},
"type": "response",
"should_pass": True,
},
{
"name": "Response with both result and error",
"message": {"jsonrpc": "2.0", "id": 1, "result": {}, "error": {}},
"type": "response",
"should_pass": False,
},
]
passed = 0
for case in test_cases:
if case["type"] == "request":
result = validator.validate_request(case["message"])
else:
result = validator.validate_response(case["message"])
actual_pass = result.valid
expected_pass = case["should_pass"]
if actual_pass == expected_pass:
print(f" β {case['name']}")
passed += 1
else:
print(
f" β {case['name']} (expected {'pass' if expected_pass else 'fail'})"
)
if result.errors:
print(f" Errors: {result.errors}")
print(f"\n{passed}/{len(test_cases)} validation tests passed")
async def run_all_tests() -> None:
"""Run all test suites."""
server = create_test_server()
await run_smoke_tests(server)
print()
await run_validation_tests()
def main():
# Auto-injected: Load configuration
from pathlib import Path
import yaml
config_path = (
Path(__file__).resolve().parent.parent.parent
/ "config"
/ "model_context_protocol"
/ "config.yaml"
)
if config_path.exists():
with open(config_path) as f:
yaml.safe_load(f) or {}
print("Loaded config from config/model_context_protocol/config.yaml")
parser = argparse.ArgumentParser(
description="MCP Test Runner",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("--smoke", action="store_true", help="Run smoke tests only")
parser.add_argument("--tool", "-t", help="Test specific tool")
parser.add_argument(
"--validation", action="store_true", help="Run validation tests"
)
args = parser.parse_args()
server = create_test_server()
if args.smoke:
asyncio.run(run_smoke_tests(server))
elif args.tool:
asyncio.run(run_tool_tests(server, args.tool))
elif args.validation:
asyncio.run(run_validation_tests())
else:
asyncio.run(run_all_tests())
return 0
if __name__ == "__main__":
sys.exit(main())