Skip to content

how to list the tools of streamable mcpserver? #940

Open
@zhuzm

Description

@zhuzm

Question

when I use “ await client.connect_to_streamable_server(server_url=server_url) ” to connect the streamable_server, it report error as below, does anyone can help? or any example to achieve this ? thanks!

DEBUG:main:正在创建会话...
DEBUG:main:正在初始化会话...
DEBUG:mcp.client.streamable_http:Sending client message: root=JSONRPCRequest(method='initialize', params={'protocolVersion': '2024-11-05', 'capabilities': {'sampling': {}, 'roots': {'listChanged': True}}, 'clientInfo': {'name': 'mcp', 'version': '0.1.0'}}, jsonrpc='2.0', id=0)
ERROR:main:streamable-http连接失败: 'function' object has no attribute 'total_seconds'
错误: 'function' object has no attribute 'total_seconds'

============================
class MCPClient:
def init(self):
self.session = None
self._session_context = None
self._streams_context = None
self.anthropic = Anthropic()

    async def connect_to_streamable_server(self, server_url: str):
            """Connect to an MCP server running with streamable-http transport"""
            try:
                logger.debug(f"正在创建streamable-http客户端连接: {server_url}")
                self._streams_context = streamablehttp_client(
                    url=server_url
                )
                streams = await self._streams_context.__aenter__()
    
                logger.debug("正在创建会话...")
                self._session_context = ClientSession(*streams)
                self.session = await self._session_context.__aenter__()
    
                logger.debug("正在初始化会话...")
                await self.session.initialize()
                logger.info("streamable-http连接成功建立")
            except Exception as e:
                logger.error(f"streamable-http连接失败: {str(e)}")
                raise

      async def get_tools(self) -> List[dict]:
    """Fetch tools and return their details"""
    try:
        logger.debug("正在获取工具列表...")
        response = await self.session.list_tools()
        tools = response.tools
        logger.info(f"成功获取到 {len(tools)} 个工具")
        return [
            {
                "name": tool.name,
                "description": tool.description,
                "input_schema": tool.inputSchema
            }
            for tool in tools
        ]
    except Exception as e:
        logger.error(f"获取工具列表失败: {str(e)}")
        raise

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions