forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
476 lines (414 loc) · 15.4 KB
/
Copy pathmcp_server.py
File metadata and controls
476 lines (414 loc) · 15.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
468
469
470
471
472
473
474
475
476
#!/usr/bin/env python3
"""MisakaNet MCP Server — thin adapter for Claude Code, Cursor, Continue, etc.
Exposes 3 tools:
misakanet.search(query, domain?, top?)
misakanet.get_lesson(path_or_id)
misakanet.submit_usage(lesson_id, tool, outcome)
Usage:
# As MCP server (stdio transport)
python3 scripts/mcp_server.py
# In Claude Code settings.json:
{
"mcpServers": {
"misakanet": {
"command": "python3",
"args": ["C:/Users/hp/MisakaNet/scripts/mcp_server.py"]
}
}
}
"""
from __future__ import annotations
import json
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT))
# Import SAG-Lite search
try:
from scripts.build_sag_index import search as sag_search
SAG_DB = REPO_ROOT / "data" / "sag.db"
HAS_SAG = SAG_DB.exists()
except ImportError:
HAS_SAG = False
# Import BM25 search fallback
try:
from misakanet.search.engine import MisakaNetSearchEngine
HAS_BM25 = True
except ImportError:
HAS_BM25 = False
def handle_search(args: dict) -> dict:
"""Search MisakaNet lessons."""
query = args.get("query", "")
domain = args.get("domain")
top = args.get("top", 5)
if not query:
return {"error": "query is required"}
if HAS_SAG:
results = sag_search(SAG_DB, query, domain=domain, top=top)
return {"results": results, "source": "sag-lite"}
elif HAS_BM25:
engine = MisakaNetSearchEngine()
results = engine.search(query, top=top)
return {"results": results, "source": "bm25"}
else:
return {"error": "No search engine available. Run: python3 scripts/build_sag_index.py"}
def handle_get_lesson(args: dict) -> dict:
"""Get a lesson by path or ID."""
path_or_id = args.get("path", args.get("id", ""))
if not path_or_id:
return {"error": "path or id is required"}
# Try direct path
lesson_path = REPO_ROOT / path_or_id
if not lesson_path.exists():
# Try searching by ID in lessons/
for subdir in ["core", "contrib"]:
candidate = REPO_ROOT / "lessons" / subdir / f"{path_or_id}.md"
if candidate.exists():
lesson_path = candidate
break
if not lesson_path.exists():
return {"error": f"Lesson not found: {path_or_id}"}
content = lesson_path.read_text(encoding="utf-8", errors="replace")
return {
"path": str(lesson_path.relative_to(REPO_ROOT)),
"content": content[:5000], # Truncate for MCP context window
}
def handle_submit_usage(args: dict) -> dict:
"""Submit a usage report (placeholder — creates GitHub Issue via API)."""
lesson_id = args.get("lesson_id", "")
tool = args.get("tool", "unknown")
outcome = args.get("outcome", "unknown")
if not lesson_id:
return {"error": "lesson_id is required"}
# For now, just log locally
report = {
"lesson_id": lesson_id,
"tool": tool,
"outcome": outcome,
"status": "logged",
}
# TODO: POST to /api/usage or create GitHub Issue
return report
# ── MCP Resources ──
RESOURCES = [
{
"uri": "misaka://lessons/index",
"name": "Lessons Index",
"description": "Browse all published lessons (core + contrib) with metadata",
"mimeType": "application/json",
},
{
"uri": "misaka://protocol/overview",
"name": "Protocol Overview",
"description": "Swarm Knowledge Protocol configuration (trust tiers, rings, scoring)",
"mimeType": "application/json",
},
{
"uri": "misaka://docs/readme",
"name": "README",
"description": "Project overview, quickstart, and integration guide",
"mimeType": "text/markdown",
},
{
"uri": "misaka://docs/faq",
"name": "Troubleshooting FAQ",
"description": "Common issues and solutions for MisakaNet users",
"mimeType": "text/markdown",
},
{
"uri": "misaka://docs/changelog",
"name": "Changelog",
"description": "Latest release notes and version history",
"mimeType": "text/markdown",
},
]
def handle_resources_list() -> list:
"""Return available resources."""
return RESOURCES
def handle_resources_read(uri: str) -> dict:
"""Read a resource by URI."""
if uri == "misaka://lessons/index":
lessons = []
for subdir in ["core", "contrib"]:
d = REPO_ROOT / "lessons" / subdir
if d.exists():
for f in sorted(d.glob("*.md")):
lessons.append({
"id": f.stem,
"path": str(f.relative_to(REPO_ROOT)),
"category": subdir,
})
return {"lessons": lessons, "count": len(lessons)}
elif uri == "misaka://protocol/overview":
p = REPO_ROOT / "misaka-protocol.json"
if p.exists():
return json.loads(p.read_text(encoding="utf-8"))
return {"error": "misaka-protocol.json not found"}
elif uri == "misaka://docs/readme":
p = REPO_ROOT / "README.md"
if p.exists():
return {"content": p.read_text(encoding="utf-8", errors="replace")[:8000]}
return {"error": "README.md not found"}
elif uri == "misaka://docs/faq":
p = REPO_ROOT / "docs" / "troubleshooting.md"
if p.exists():
return {"content": p.read_text(encoding="utf-8", errors="replace")[:8000]}
return {"error": "troubleshooting.md not found"}
elif uri == "misaka://docs/changelog":
p = REPO_ROOT / "STATUS.md"
if p.exists():
return {"content": p.read_text(encoding="utf-8", errors="replace")[:4000]}
return {"error": "STATUS.md not found"}
return {"error": f"Unknown resource: {uri}"}
# ── MCP Prompts ──
PROMPTS = [
{
"name": "search_lesson",
"title": "Search Lessons",
"description": "Search MisakaNet for lessons matching an error or topic",
"arguments": [
{"name": "query", "description": "Error message or topic to search for", "required": True},
{"name": "domain", "description": "Optional domain filter (devops, python, rag, etc.)", "required": False},
],
},
{
"name": "triage_failure",
"title": "Triage Failure",
"description": "Structured failure triage — find root cause and matching rescue cards",
"arguments": [
{"name": "error", "description": "The error message or stack trace", "required": True},
{"name": "context", "description": "What were you doing when the error occurred", "required": False},
],
},
{
"name": "release_audit",
"title": "Release Audit",
"description": "Check release readiness against MisakaNet quality gates",
"arguments": [
{"name": "version", "description": "Version to audit (e.g., v2.12.0)", "required": True},
],
},
]
def handle_prompts_get(name: str, arguments: dict) -> dict:
"""Return a prompt with arguments filled in."""
if name == "search_lesson":
query = arguments.get("query", "")
domain = arguments.get("domain", "")
domain_hint = f" in the '{domain}' domain" if domain else ""
return {
"description": f"Search for lessons about: {query}",
"messages": [
{
"role": "user",
"content": {
"type": "text",
"text": (
f"Search MisakaNet lessons for solutions to: \"{query}\"{domain_hint}.\n\n"
f"Use the misakanet_search tool with query=\"{query}\""
+ (f" and domain=\"{domain}\"" if domain else "")
+ ".\n\nReport the top 3 matches with their relevance score and actionable summary."
),
},
}
],
}
elif name == "triage_failure":
error = arguments.get("error", "")
context = arguments.get("context", "unknown context")
return {
"description": f"Triage failure: {error[:80]}",
"messages": [
{
"role": "user",
"content": {
"type": "text",
"text": (
f"I encountered this error while {context}:\n\n"
f"```\n{error}\n```\n\n"
"Please:\n"
"1. Search MisakaNet for matching lessons using misakanet_search\n"
"2. If a rescue card exists, apply its fix\n"
"3. If no match, suggest the root cause and next diagnostic steps"
),
},
}
],
}
elif name == "release_audit":
version = arguments.get("version", "latest")
return {
"description": f"Audit release {version}",
"messages": [
{
"role": "user",
"content": {
"type": "text",
"text": (
f"Audit MisakaNet release {version} for readiness.\n\n"
"Check:\n"
"1. Read misaka://docs/changelog for this version's changes\n"
"2. Verify all lessons in misaka://lessons/index have valid frontmatter\n"
"3. Check protocol version matches in misaka://protocol/overview\n"
"4. Report any gaps or blockers for release"
),
},
}
],
}
return {"error": f"Unknown prompt: {name}"}
# ── MCP Tools ──
TOOLS = [
{
"name": "misakanet_search",
"description": "Search MisakaNet lessons for solutions to errors, bugs, and technical problems.",
"inputSchema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query (error message, keyword, or topic)"},
"domain": {"type": "string", "description": "Filter by domain (devops, python, network, feishu, rag, etc.)"},
"top": {"type": "integer", "description": "Number of results (default 5)"},
},
"required": ["query"],
},
},
{
"name": "misakanet_get_lesson",
"description": "Get the full content of a specific lesson by path or ID.",
"inputSchema": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Lesson path (e.g., lessons/core/auto-merge-ci-pipeline.md)"},
"id": {"type": "string", "description": "Lesson ID (filename without .md)"},
},
},
},
{
"name": "misakanet_submit_usage",
"description": "[Experimental] Report that a lesson was used to solve a problem. Currently logs locally only.",
"inputSchema": {
"type": "object",
"properties": {
"lesson_id": {"type": "string", "description": "ID of the lesson that helped"},
"tool": {"type": "string", "description": "Your tool name (e.g., claude-code, cursor, aider)"},
"outcome": {"type": "string", "description": "Outcome: solved, partial, not-helpful"},
},
"required": ["lesson_id"],
},
},
]
def handle_request(request: dict) -> dict:
"""Handle a JSON-RPC request."""
method = request.get("method", "")
params = request.get("params", {})
req_id = request.get("id")
if method == "initialize":
return {
"jsonrpc": "2.0",
"id": req_id,
"result": {
"protocolVersion": "2025-06-18",
"capabilities": {
"tools": {},
"resources": {},
"prompts": {},
},
"serverInfo": {
"name": "misakanet",
"version": "2.12.0",
},
},
}
elif method == "tools/list":
return {
"jsonrpc": "2.0",
"id": req_id,
"result": {"tools": TOOLS},
}
elif method == "tools/call":
tool_name = params.get("name", "")
tool_args = params.get("arguments", {})
handlers = {
"misakanet_search": handle_search,
"misakanet_get_lesson": handle_get_lesson,
"misakanet_submit_usage": handle_submit_usage,
}
handler = handlers.get(tool_name)
if not handler:
return {
"jsonrpc": "2.0",
"id": req_id,
"error": {"code": -32601, "message": f"Unknown tool: {tool_name}"},
}
result = handler(tool_args)
return {
"jsonrpc": "2.0",
"id": req_id,
"result": {
"content": [{"type": "text", "text": json.dumps(result, ensure_ascii=False)}],
},
}
elif method == "notifications/initialized":
return None
# ── Resources ──
elif method == "resources/list":
return {
"jsonrpc": "2.0",
"id": req_id,
"result": {"resources": handle_resources_list()},
}
elif method == "resources/read":
uri = params.get("uri", "")
content = handle_resources_read(uri)
return {
"jsonrpc": "2.0",
"id": req_id,
"result": {
"contents": [
{"uri": uri, "mimeType": "application/json", "text": json.dumps(content, ensure_ascii=False)}
]
},
}
# ── Prompts ──
elif method == "prompts/list":
return {
"jsonrpc": "2.0",
"id": req_id,
"result": {"prompts": PROMPTS},
}
elif method == "prompts/get":
name = params.get("name", "")
arguments = params.get("arguments", {})
result = handle_prompts_get(name, arguments)
if "error" in result:
return {
"jsonrpc": "2.0",
"id": req_id,
"error": {"code": -32602, "message": result["error"]},
}
return {"jsonrpc": "2.0", "id": req_id, "result": result}
else:
return {
"jsonrpc": "2.0",
"id": req_id,
"error": {"code": -32601, "message": f"Unknown method: {method}"},
}
def main():
"""MCP stdio server loop."""
# Write to stderr for debug (stdout is for MCP protocol)
sys.stderr.write("MisakaNet MCP Server started\n")
sys.stderr.write(f"SAG-Lite: {'available' if HAS_SAG else 'not available (run build_sag_index.py)'}\n")
sys.stderr.write(f"BM25: {'available' if HAS_BM25 else 'not available'}\n")
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
request = json.loads(line)
except json.JSONDecodeError:
continue
response = handle_request(request)
if response:
sys.stdout.write(json.dumps(response) + "\n")
sys.stdout.flush()
if __name__ == "__main__":
main()