[docs]classHttpStatefulClient(StatefulClientBase):"""The stateful sse/streamable HTTP MCP client implementation in AgentScope. .. tip:: The stateful client is recommended for MCP servers that need to maintain session states, e.g. web browsers or other interactive MCP servers. .. note:: The stateful client will maintain one session across multiple tool calls, until the client is closed by explicitly calling the `close()` method. """
[docs]def__init__(self,name:str,transport:Literal["streamable_http","sse"],url:str,headers:dict[str,str]|None=None,timeout:float=30,sse_read_timeout:float=60*5,**client_kwargs:Any,)->None:"""Initialize the streamable HTTP MCP client. Args: name (`str`): The name to identify the MCP server, which should be unique across the MCP servers. transport (`Literal["streamable_http", "sse"]`): The transport type of MCP server. Generally, the URL of sse transport should end with `/sse`, while the streamable HTTP URL ends with `/mcp`. url (`str`): The URL to the MCP server. headers (`dict[str, str] | None`, optional): Additional headers to include in the HTTP request. timeout (`float`, optional): The timeout for the HTTP request in seconds. Defaults to 30. sse_read_timeout (`float`, optional): The timeout for reading Server-Sent Events (SSE) in seconds. Defaults to 300 (5 minutes). **client_kwargs (`Any`): The additional keyword arguments to pass to the streamable HTTP client. """super().__init__(name=name)asserttransportin["streamable_http","sse"]self.transport=transportifself.transport=="streamable_http":self.client=streamablehttp_client(url=url,headers=headers,timeout=timeout,sse_read_timeout=sse_read_timeout,**client_kwargs,)else:self.client=sse_client(url=url,headers=headers,timeout=timeout,sse_read_timeout=sse_read_timeout,**client_kwargs,)