-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_client.py
More file actions
58 lines (51 loc) · 2.05 KB
/
mcp_client.py
File metadata and controls
58 lines (51 loc) · 2.05 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
from mcp.client.stdio import stdio_client
from mcp.client.stdio import StdioServerParameters
from mcp.client.session import ClientSession
import asyncio
import logging
# 设置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
async def main():
# 创建服务器参数
server_params = StdioServerParameters(
command="python",
args=["mcp_server.py"],
env={"PYTHONUNBUFFERED": "1"} # 禁用Python输出缓冲
)
# 连接到服务器
logging.info("正在连接到服务器...")
async with stdio_client(server_params) as (read_stream, write_stream):
session = ClientSession(read_stream, write_stream)
logging.info("已连接到服务器")
# 获取可用工具列表
logging.info("正在获取工具列表...")
tools = await session.list_tools()
logging.info("\n可用工具:")
for tool in tools:
logging.info(f"- {tool['name']}: {tool['description']}")
# 测试查询
test_queries = [
"查询北京2024年3月的案件明细",
"查询上海2023年10月至12月的案件数量",
"查询广州2025年1月的案件类型分布"
]
for query in test_queries:
logging.info(f"\n执行查询: {query}")
try:
result = await session.call_tool(
"convert_to_sql",
{
"query": query,
"top_k": 1
}
)
logging.info("查询结果:")
logging.debug(f"原始响应: {result}")
for item in result["results"]:
logging.info(f"- 描述: {item['description']}")
logging.info(f"- SQL: {item['sql']}")
logging.info(f"- 相似度: {item['score']:.4f}")
except Exception as e:
logging.error(f"查询出错: {e}")
if __name__ == "__main__":
asyncio.run(main())