Files
agent-craft/m10_mcp_basics/simple_client.py
T

19 lines
867 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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