feat: add module 10 code and related content

This commit is contained in:
Annyfee
2025-12-03 13:04:27 +08:00
parent 8931420c5d
commit ed9f77d0b8
8 changed files with 535 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
from mcp import ClientSession,StdioServerParameters
from mcp.client.stdio import stdio_client
class SimpleClient:
def __init__(self,command:str,args:list[str],env:dict=None):
# 指定要启动的工具和参数
self.params = StdioServerParameters(command=command,args=args,env=env)
async def run_once(self,tool_name:str,tool_args:dict):
# 语法糖: async with 自动帮我们 打开连接 -> 运行 -> 关闭连接
async with stdio_client(self.params) as (read,write):
# 建立父子进程管道(stdin/stdout
async with ClientSession(read,write) as session:
# 用JSON-RPC与工具对话
await session.initialize()
# 直接调用工具
result = await session.call_tool(tool_name,tool_args)
return result.content[0].text