Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,24 @@
|
|
1 |
# app.py
|
2 |
-
|
3 |
-
from
|
4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
# ========== 基本設定 ==========
|
|
|
1 |
# app.py
|
2 |
+
from modelcontextprotocol import Client
|
3 |
+
from modelcontextprotocol.transports.stdio import StdioTransport
|
4 |
+
import subprocess, sys, uuid
|
5 |
+
|
6 |
+
# 啟動 MCP server 子程序(stdin/stdout)
|
7 |
+
def start_mcp():
|
8 |
+
# 用當前 Python 啟動 mcp_server.py
|
9 |
+
proc = subprocess.Popen([sys.executable, "mcp_server.py"],
|
10 |
+
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
11 |
+
transport = StdioTransport(proc.stdin, proc.stdout)
|
12 |
+
client = Client(transport=transport)
|
13 |
+
client.start()
|
14 |
+
return client, proc
|
15 |
+
|
16 |
+
mcp_client, mcp_proc = start_mcp()
|
17 |
+
|
18 |
+
# 呼叫工具(例:Tarot)
|
19 |
+
def mcp_tarot_search(q, k=3):
|
20 |
+
res = mcp_client.call_tool("tarot.search", {"query": q, "k": k})
|
21 |
+
return res.get("hits", [])
|
22 |
|
23 |
|
24 |
# ========== 基本設定 ==========
|