forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mcp_server.py
More file actions
212 lines (175 loc) 路 6.88 KB
/
Copy pathtest_mcp_server.py
File metadata and controls
212 lines (175 loc) 路 6.88 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
#!/usr/bin/env python3
"""Smoke test for MisakaNet MCP Server.
Tests the JSON-RPC handlers directly (no subprocess needed).
Usage:
python3 tests/test_mcp_server.py
"""
import json
import re
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT))
from scripts.mcp_server import TOOLS, handle_request
PASS = 0
FAIL = 0
def rpc(method: str, params: dict = None, req_id: int = 1) -> dict:
"""Send a JSON-RPC request to the handler."""
return handle_request({
"jsonrpc": "2.0",
"id": req_id,
"method": method,
"params": params or {},
})
def check(name: str, condition: bool, detail: str = ""):
global PASS, FAIL
if condition:
PASS += 1
print(f" PASS {name}")
else:
FAIL += 1
print(f" FAIL {name}{': ' + detail if detail else ''}")
def test_initialize():
print("\n-- initialize --")
resp = rpc("initialize")
result = resp.get("result", {})
check("has protocolVersion", "protocolVersion" in result)
check("has serverInfo.name", result.get("serverInfo", {}).get("name") == "misakanet")
expected_version = re.search(
r'^version\s*=\s*["\']([^"\']+)["\']',
(REPO_ROOT / "pyproject.toml").read_text(encoding="utf-8", errors="replace"),
re.MULTILINE,
).group(1)
check(
"has current serverInfo.version",
result.get("serverInfo", {}).get("version") == expected_version,
)
check("has capabilities.tools", "tools" in result.get("capabilities", {}))
def test_tools_list():
print("\n-- tools/list --")
resp = rpc("tools/list")
tools = resp.get("result", {}).get("tools", [])
tool_names = {t["name"] for t in tools}
check("has misakanet_search", "misakanet_search" in tool_names)
check("has misakanet_get_lesson", "misakanet_get_lesson" in tool_names)
check("has misakanet_submit_usage", "misakanet_submit_usage" in tool_names)
check("search requires query", "query" in tools[0]["inputSchema"]["required"])
def test_tool_descriptions_are_agent_friendly():
print("\n-- tool descriptions --")
required_terms = [
"Input semantics",
"Output schema",
"Error cases",
"Side effects",
"Auth",
"Rate limits",
]
for tool in TOOLS:
desc = tool.get("description", "")
missing = [term for term in required_terms if term not in desc]
check(f"{tool['name']} describes operating contract", not missing, f"missing: {missing}")
def test_search():
print("\n-- tools/call: misakanet_search --")
resp = rpc("tools/call", {
"name": "misakanet_search",
"arguments": {"query": "CI pipeline", "top": 3},
})
result_text = resp.get("result", {}).get("content", [{}])[0].get("text", "{}")
result = json.loads(result_text)
if "error" in result:
print(f" WARN Search unavailable: {result['error']}")
print(" (Run: python3 scripts/build_sag_index.py)")
return
results = result.get("results", [])
check("returns results list", isinstance(results, list))
if results:
first = results[0]
check("result has path", "path" in first, f"keys: {list(first.keys())}")
check("result has status", "status" in first)
check("result has score/rank", "score" in first or "rank" in first)
check("no draft results", first.get("status") != "draft")
else:
print(" WARN No results returned (index may be empty)")
def test_get_lesson():
print("\n-- tools/call: misakanet_get_lesson --")
# Find a real lesson file
lessons_dir = REPO_ROOT / "lessons"
sample = None
for subdir in ["core", "contrib"]:
candidates = list((lessons_dir / subdir).glob("*.md")) if (lessons_dir / subdir).exists() else []
if candidates:
sample = candidates[0]
break
if not sample:
print(" WARN No lesson files found, skipping")
return
lesson_id = sample.stem
resp = rpc("tools/call", {
"name": "misakanet_get_lesson",
"arguments": {"id": lesson_id},
})
result_text = resp.get("result", {}).get("content", [{}])[0].get("text", "{}")
result = json.loads(result_text)
check("returns path", "path" in result, f"keys: {list(result.keys())}")
check("returns content", "content" in result)
check("content is non-empty", len(result.get("content", "")) > 10)
def test_submit_usage():
print("\n-- tools/call: misakanet_submit_usage --")
resp = rpc("tools/call", {
"name": "misakanet_submit_usage",
"arguments": {"lesson_id": "test-lesson", "tool": "smoke-test", "outcome": "solved"},
})
result_text = resp.get("result", {}).get("content", [{}])[0].get("text", "{}")
result = json.loads(result_text)
check("returns status", "status" in result)
check("status is logged", result.get("status") == "logged")
def test_unknown_tool():
print("\n-- error handling --")
resp = rpc("tools/call", {
"name": "nonexistent_tool",
"arguments": {},
})
check("returns error for unknown tool", "error" in resp)
check("error code is -32601", resp.get("error", {}).get("code") == -32601)
def test_no_drafts_in_search():
print("\n-- search scope: no drafts --")
resp = rpc("tools/call", {
"name": "misakanet_search",
"arguments": {"query": "draft test lesson", "top": 10},
})
result_text = resp.get("result", {}).get("content", [{}])[0].get("text", "{}")
result = json.loads(result_text)
if "error" in result:
print(" WARN Search unavailable, skipping draft check")
return
results = result.get("results", [])
draft_count = sum(1 for r in results if r.get("status") == "draft")
check("no drafts in results", draft_count == 0, f"found {draft_count} drafts")
def test_usage_status():
print("\n-- tools/call: misakanet_usage_status --")
resp = rpc("tools/call", {
"name": "misakanet_usage_status",
"arguments": {"user": "anon:test-mcp"},
})
result_text = resp.get("result", {}).get("content", [{}])[0].get("text", "{}")
result = json.loads(result_text)
check("returns user", "user" in result)
check("returns free_reads_used", "free_reads_used" in result)
check("returns free_reads_limit", "free_reads_limit" in result)
check("returns free_reads_remaining", "free_reads_remaining" in result)
check("returns credits", "credits" in result)
check("returns is_registered", "is_registered" in result)
if __name__ == "__main__":
print("MisakaNet MCP Server smoke test")
test_initialize()
test_tools_list()
test_tool_descriptions_are_agent_friendly()
test_search()
test_get_lesson()
test_submit_usage()
test_unknown_tool()
test_no_drafts_in_search()
test_usage_status()
print(f"\n{'=' * 40}")
print(f"Results: {PASS} passed, {FAIL} failed")
sys.exit(1 if FAIL else 0)