Spaces:
Running
Running
File size: 1,472 Bytes
9f77a82 |
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 |
# mcp_server.py (最小骨架)
import json, os
from modelcontextprotocol import Server
import rag_utils
srv = Server(name="soulcompass-mcp")
@srv.tool("tarot.search")
def tarot_search(query: str, k: int = 3):
hits = rag_utils.search_tarot(query, k)
return {"hits": hits}
@srv.tool("numerology.search")
def num_search(query: str, k: int = 3):
hits = rag_utils.search_numerology(query, k)
return {"hits": hits}
@srv.tool("corpus.search_union")
def union_search(query: str, k_each: int = 2):
hits = rag_utils.search_union(query, k_each)
return {"hits": hits}
# 以 UID 區分使用者資料(沿用你 app.py 的結構)
DATA_ROOT = "data/users"
@srv.tool("journal.append")
def journal_append(uid: str, entry: dict):
user_dir = os.path.join(DATA_ROOT, uid); os.makedirs(user_dir, exist_ok=True)
path = os.path.join(user_dir, "journal.jsonl")
with open(path, "a", encoding="utf-8") as f:
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
return {"ok": True}
@srv.tool("journal.query")
def journal_query(uid: str, limit: int = 50):
path = os.path.join(DATA_ROOT, uid, "journal.jsonl")
out = []
if os.path.exists(path):
for line in open(path, "r", encoding="utf-8").read().splitlines()[-limit:]:
try: out.append(json.loads(line))
except: pass
return {"entries": out}
if __name__ == "__main__":
# 以 stdio 方式啟動(Space 內最簡單)
srv.run_stdio()
|