Skip to content

How to connect a client to multiple servers #330

@qqqtwh

Description

@qqqtwh

original code

async def connect_to_server(self,server_script_path:str):

    is_python = server_script_path.endswith('.py')

    if not is_python:
        raise ValueError('must python')
    command = "python"
    
    server_paramss = StdioServerParameters(
        command=command,
        args=[server_script_path],
        env=None
    )

    stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_paramss))
    self.stdio, self.write = stdio_transport

    self.session = await self.exit_stack.enter_async_context(ClientSession(self.stdio,self.write))

    await self.session.initialize()

    response = await self.session.list_tools()
    tools = response.tools

    print("\n server tools :", [tool.name for tool in tools])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions