forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
181 lines (158 loc) · 5.01 KB
/
Copy pathprotocol.py
File metadata and controls
181 lines (158 loc) · 5.01 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
"""MCP JSON-RPC protocol handler for MisakaNet server."""
from __future__ import annotations
import json
import sys
from ._config import get_server_version
from .handlers import (
handle_get_lesson,
handle_memory_context,
handle_preflight,
handle_register,
handle_search,
handle_submit_intake,
handle_submit_usage,
handle_usage_status,
handle_write_lesson,
)
from .prompts import PROMPTS, handle_prompts_get
from .resources import handle_resources_list, handle_resources_read
from .tools import _filtered_tools
# Handler dispatch table
_HANDLERS = {
"misakanet_search": handle_search,
"misakanet_get_lesson": handle_get_lesson,
"misakanet_submit_usage": handle_submit_usage,
"misakanet_submit_intake": handle_submit_intake,
"misakanet_write_lesson": handle_write_lesson,
"misakanet_preflight": handle_preflight,
"misakanet_usage_status": handle_usage_status,
"misakanet_register": handle_register,
"misakanet_memory_context": handle_memory_context,
}
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": _filtered_tools()},
}
elif method == "tools/call":
tool_name = params.get("name", "")
tool_args = params.get("arguments", {})
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."""
from ._config import _init_search
HAS_SAG, _, HAS_BM25, _ = _init_search() # noqa: N806
sys.stderr.write("MisakaNet MCP Server started\n")
sag_status = "available" if HAS_SAG else "not available (run build_sag_index.py)"
bm25_status = "available" if HAS_BM25 else "not available"
sys.stderr.write(f"SAG-Lite: {sag_status}\n")
sys.stderr.write(f"BM25: {bm25_status}\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()