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
559 lines (493 loc) · 20.2 KB
/
Copy pathmcp_server.py
File metadata and controls
559 lines (493 loc) · 20.2 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/usr/bin/env python3
"""MisakaNet MCP Server — thin adapter for Claude Code, Cursor, Continue, etc.
Exposes 4 tools:
misakanet.search(query, domain?, top?)
misakanet.get_lesson(path_or_id)
misakanet.submit_usage(lesson_id, tool, outcome)
misakanet.usage_status(user?)
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 re
import sys
from importlib import metadata
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT))
def get_server_version() -> str:
"""Return the installed or checkout package version for MCP metadata."""
try:
return metadata.version("misakanet")
except metadata.PackageNotFoundError:
pyproject = REPO_ROOT / "pyproject.toml"
if pyproject.exists():
match = re.search(
r'^version\s*=\s*["\']([^"\']+)["\']',
pyproject.read_text(encoding="utf-8", errors="replace"),
re.MULTILINE,
)
if match:
return match.group(1)
return "0.0.0"
# 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
def handle_usage_status(args: dict) -> dict:
"""Show current usage status and remaining quota."""
try:
from scripts.usage_meter import get_status, hash_ip
user = args.get("user", "anon:mcp-default")
status = get_status(user)
return {
"user": status["user"],
"free_reads_used": status["free_reads_used"],
"free_reads_limit": status["free_reads_limit"],
"free_reads_remaining": status["free_reads_remaining"],
"credits": status["credits"],
"is_registered": status["is_registered"],
"next": "Use misakanet_submit_intake or misakanet_contribute_lesson to request more credits."
}
except Exception as e:
return {"error": str(e), "user": "unknown", "free_reads_remaining": -1}
# ── 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's public failure-lesson index by error text, keyword, or topic. "
"Use when you need to discover relevant lessons and do not already know a lesson ID. "
"Input semantics: query is required; domain optionally filters by lesson domain; top limits "
"ranked results and defaults to 5. Output schema: JSON with results[] and source; each "
"result is a ranked lesson summary that may include path, title, domain/status, score/rank, "
"and match details depending on the active index. Error cases: missing query, unavailable "
"search index, or no matches (empty results). Side effects: none. Auth: none. Rate limits: "
"local stdio process only; callers should keep result counts small. Do not use for private "
"log collection; search only with redacted snippets. Use misakanet_get_lesson for full content."
),
"inputSchema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Required redacted error message, keyword, or topic (for example: 'pip install timeout' or 'DCO sign-off failed')."},
"domain": {"type": "string", "description": "Optional domain filter such as devops, python, network, feishu, rag, fanuc, or mcp."},
"top": {"type": "integer", "description": "Maximum ranked results to return. Defaults to 5; keep small for MCP context and latency."},
},
"required": ["query"],
},
},
{
"name": "misakanet_get_lesson",
"description": (
"Fetch one public MisakaNet lesson by repository path or lesson ID. Use after "
"misakanet_search returns a promising result, or when a lesson is explicitly referenced; "
"do not use it for broad discovery. Input semantics: provide either path or id. Output "
"schema: JSON with path and markdown content, truncated to 5000 characters for MCP context. "
"Error cases: missing path/id or lesson not found. Side effects: none. Auth: none. Rate "
"limits: local stdio process only; fetch one lesson per call when possible. Do not send "
"private logs or prompts to this tool; it only reads repository lessons."
),
"inputSchema": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Lesson path relative to the repository, for example lessons/core/auto-merge-ci-pipeline.md."},
"id": {"type": "string", "description": "Lesson ID, usually the filename without .md, for example auto-merge-ci-pipeline."},
},
},
},
{
"name": "misakanet_submit_usage",
"description": (
"[Experimental] Record that a public lesson helped with a problem. Use only after the "
"user or calling agent explicitly chooses to submit usage feedback for a specific lesson. "
"Input semantics: lesson_id is required; tool names the calling client; outcome should be "
"solved, partial, not-helpful, or another short status. Output schema: JSON with lesson_id, "
"tool, outcome, and status. Error cases: missing lesson_id. Side effects: currently returns "
"a local placeholder report only; it does not send data externally, open GitHub issues, or "
"publish lessons. Auth: none. Rate limits: local stdio process only; submit at most once per "
"resolved incident. Do not include raw logs, prompts, file contents, or secrets."
),
"inputSchema": {
"type": "object",
"properties": {
"lesson_id": {"type": "string", "description": "Required ID of the lesson that helped, for example auto-merge-ci-pipeline."},
"tool": {"type": "string", "description": "Calling tool or client name, for example claude-code, cursor, codex, or aider."},
"outcome": {"type": "string", "description": "Short result label such as solved, partial, or not-helpful."},
},
"required": ["lesson_id"],
},
},
{
"name": "misakanet_usage_status",
"description": (
"Check current usage status and remaining quota. Use to see how many free lesson reads "
"remain and how many credits are available. Input semantics: user is optional (defaults to "
"anonymous). Output schema: JSON with user, free_reads_used, free_reads_limit, "
"free_reads_remaining, credits, is_registered, and next steps. Error cases: none. Side "
"effects: none. Auth: none. Rate limits: none."
),
"inputSchema": {
"type": "object",
"properties": {
"user": {"type": "string", "description": "Optional user identifier (e.g. 'anon:iphash' or 'token:xxx'). Defaults to 'anon:mcp-default'."},
},
},
},
]
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": get_server_version(),
},
},
}
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,
"misakanet_usage_status": handle_usage_status,
}
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()