Spaces:
Running
Running
Create mcp_server.py
Browse files- mcp_server.py +46 -0
mcp_server.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# mcp_server.py (最小骨架)
|
2 |
+
import json, os
|
3 |
+
from modelcontextprotocol import Server
|
4 |
+
import rag_utils
|
5 |
+
|
6 |
+
srv = Server(name="soulcompass-mcp")
|
7 |
+
|
8 |
+
@srv.tool("tarot.search")
|
9 |
+
def tarot_search(query: str, k: int = 3):
|
10 |
+
hits = rag_utils.search_tarot(query, k)
|
11 |
+
return {"hits": hits}
|
12 |
+
|
13 |
+
@srv.tool("numerology.search")
|
14 |
+
def num_search(query: str, k: int = 3):
|
15 |
+
hits = rag_utils.search_numerology(query, k)
|
16 |
+
return {"hits": hits}
|
17 |
+
|
18 |
+
@srv.tool("corpus.search_union")
|
19 |
+
def union_search(query: str, k_each: int = 2):
|
20 |
+
hits = rag_utils.search_union(query, k_each)
|
21 |
+
return {"hits": hits}
|
22 |
+
|
23 |
+
# 以 UID 區分使用者資料(沿用你 app.py 的結構)
|
24 |
+
DATA_ROOT = "data/users"
|
25 |
+
|
26 |
+
@srv.tool("journal.append")
|
27 |
+
def journal_append(uid: str, entry: dict):
|
28 |
+
user_dir = os.path.join(DATA_ROOT, uid); os.makedirs(user_dir, exist_ok=True)
|
29 |
+
path = os.path.join(user_dir, "journal.jsonl")
|
30 |
+
with open(path, "a", encoding="utf-8") as f:
|
31 |
+
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
32 |
+
return {"ok": True}
|
33 |
+
|
34 |
+
@srv.tool("journal.query")
|
35 |
+
def journal_query(uid: str, limit: int = 50):
|
36 |
+
path = os.path.join(DATA_ROOT, uid, "journal.jsonl")
|
37 |
+
out = []
|
38 |
+
if os.path.exists(path):
|
39 |
+
for line in open(path, "r", encoding="utf-8").read().splitlines()[-limit:]:
|
40 |
+
try: out.append(json.loads(line))
|
41 |
+
except: pass
|
42 |
+
return {"entries": out}
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
# 以 stdio 方式啟動(Space 內最簡單)
|
46 |
+
srv.run_stdio()
|