Skip to content

Build and run agents you can see, understand and trust.

ci(mcp): refactor the previous MCP unittests for the new version (#1505)

DavdGao401f5a264f0c
diff --git a/src/agentscope/mcp/_http_stateful_client.py b/src/agentscope/mcp/_http_stateful_client.py
index 610471e53..e9b6caec7 100644
--- a/src/agentscope/mcp/_http_stateful_client.py
+++ b/src/agentscope/mcp/_http_stateful_client.py
@@ -2,6 +2,7 @@
"""The MCP stateful HTTP client module in AgentScope."""
from typing import Any, Literal
+import httpx
from mcp.client.sse import sse_client
from mcp.client.streamable_http import streamable_http_client
@@ -34,8 +35,7 @@ class HttpStatefulClient(StatefulClientBase):
transport: Literal["streamable_http", "sse"],
url: str,
headers: dict[str, str] | None = None,
- timeout: float = 30,
- sse_read_timeout: float = 60 * 5,
+ timeout: float | None = None,
**client_kwargs: Any,
) -> None:
"""Initialize the streamable HTTP MCP client.
@@ -53,10 +53,7 @@ class HttpStatefulClient(StatefulClientBase):
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).
+ The timeout for the HTTP request in seconds.
**client_kwargs (`Any`):
The additional keyword arguments to pass to the streamable
HTTP client.
@@ -67,11 +64,16 @@ class HttpStatefulClient(StatefulClientBase):
self.transport = transport
if self.transport == "streamable_http":
+ if headers or timeout:
+ client = httpx.AsyncClient(
+ headers=headers,
+ timeout=timeout,
+ )
+ else:
+ client = None
self.client = streamable_http_client(
url=url,
- headers=headers,
- timeout=timeout,
- sse_read_timeout=sse_read_timeout,
+ http_client=client,
**client_kwargs,
)
else:
@@ -79,6 +81,5 @@ class HttpStatefulClient(StatefulClientBase):
url=url,
headers=headers,
timeout=timeout,
- sse_read_timeout=sse_read_timeout,
**client_kwargs,
)
diff --git a/src/agentscope/mcp/_http_stateless_client.py b/src/agentscope/mcp/_http_stateless_client.py
index 3e2b43ff3..6942f2771 100644
--- a/src/agentscope/mcp/_http_stateless_client.py
+++ b/src/agentscope/mcp/_http_stateless_client.py
@@ -3,6 +3,7 @@
from contextlib import _AsyncGeneratorContextManager
from typing import Any, Literal, List, TYPE_CHECKING
+import httpx
import mcp.types
from mcp import ClientSession
from mcp.client.sse import sse_client
@@ -37,7 +38,6 @@ class HttpStatelessClient(MCPClientBase):
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 server.
@@ -56,9 +56,6 @@ class HttpStatelessClient(MCPClientBase):
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.
@@ -71,9 +68,8 @@ class HttpStatelessClient(MCPClientBase):
self.client_config = {
"url": url,
- "headers": headers or {},
+ "headers": headers or None,
"timeout": timeout,
- "sse_read_timeout": sse_read_timeout,
**client_kwargs,
}
@@ -85,7 +81,21 @@ class HttpStatelessClient(MCPClientBase):
return sse_client(**self.client_config)
if self.transport == "streamable_http":
- return streamable_http_client(**self.client_config)
+ # For the newest version of mcp client, headers is not supported,
+ # we should create an async client here
+ if self.client_config.get("headers") or self.client_config.get(
+ "timeout",
+ ):
+ client = httpx.AsyncClient(
+ headers=self.client_config.get("headers", None),
+ timeout=self.client_config.get("timeout"),
+ )
+ else:
+ client = None
+ return streamable_http_client(
+ url=self.client_config["url"],
+ http_client=client,
+ )
raise ValueError(
f"Unsupported transport type: {self.transport}. "
diff --git a/tests/mcp_sse_client_test.py b/tests/mcp_sse_client_test.py
new file mode 100644
index 000000000..40c8d3c1a
--- /dev/null
+++ b/tests/mcp_sse_client_test.py
@@ -0,0 +1,241 @@
+# -*- coding: utf-8 -*-
+"""The MCP client test module in agentscope."""
+import asyncio
+import json
+from multiprocessing import Process
+from unittest.async_case import IsolatedAsyncioTestCase
+
+from mcp.server import FastMCP
+
+from agentscope.mcp import HttpStatelessClient, HttpStatefulClient
+from agentscope.message import ToolCallBlock
+from agentscope.tool import ToolResponse, ToolChunk, Toolkit
+from agentscope.agent import AgentState
+
+
+async def tool_1(arg1: str, arg2: list[int]) -> str:
+ """A test tool function.
+
+ Args:
+ arg1 (`str`):
+ The first argument named arg1.
+ arg2 (`list[int]`):
+ The second argument named arg2.
+ """
+ return f"arg1: {arg1}, arg2: {arg2}"
+
+
+def setup_server() -> None:
+ """Set up the streamable HTTP MCP server."""
+ sse_server = FastMCP("SSE", port=8003)
+ sse_server.tool(description="A test tool function.")(tool_1)
+ sse_server.run(transport="sse")
+
+
+class SseMCPClientTest(IsolatedAsyncioTestCase):
+ """Test class for MCP server functionality."""
+
+ async def asyncTearDown(self) -> None:
+ """Tear down the test environment."""
+ del self.toolkit
+
+ while self.process.is_alive():
+ self.process.terminate()
+ await asyncio.sleep(5)
+
+ async def asyncSetUp(self) -> None:
+ """Set up the test environment."""
+ self.port = 8003
+ self.process = Process(target=setup_server)
+ self.process.start()
+ await asyncio.sleep(10)
+
+ self.toolkit = Toolkit()
+ self.schemas = [
+ {
+ "type": "function",
+ "function": {
+ "name": "tool_1",
+ "description": "A test tool function.",
+ "parameters": {
+ "type": "object",
+ "properties": {
+ "arg1": {
+ "type": "string",
+ },
+ "arg2": {
+ "items": {
+ "type": "integer",
+ },
+ "type": "array",
+ },
+ },
+ "required": [
+ "arg1",
+ "arg2",
+ ],
+ },
+ },
+ },
+ ]
+
+ async def test_stateless_client(self) -> None:
+ """Test the stateless sse MCP client."""
+ stateless_client = HttpStatelessClient(
+ name="test_sse_client",
+ transport="sse",
+ url=f"http://127.0.0.1:{self.port}/sse",
+ )
+
+ mcp_tool_1 = await stateless_client.get_tool("tool_1")
+ # Repeat to ensure idempotency
+ res_1: ToolChunk = await mcp_tool_1(arg1="123", arg2=[1, 2, 3])
+ res_2: ToolChunk = await mcp_tool_1(arg1="345", arg2=[4, 5, 6])
+ res_3: ToolChunk = await mcp_tool_1(arg1="345", arg2=[4, 5, 6])
+
+ self.assertEqual(
+ res_1.content[0].text,
+ "arg1: 123, arg2: [1, 2, 3]",
+ )
+ self.assertEqual(
+ res_2.content[0].text,
+ "arg1: 345, arg2: [4, 5, 6]",
+ )
+ self.assertEqual(
+ res_3.content[0].text,
+ "arg1: 345, arg2: [4, 5, 6]",
+ )
+
+ # Register MCPTool via Toolkit constructor
+ toolkit_with_mcp = Toolkit(tools=[mcp_tool_1])
+
+ schemas = toolkit_with_mcp.get_function_schemas()
+ self.assertListEqual(
+ schemas,
+ self.schemas,
+ )
+
+ state = AgentState()
+ res_gen = toolkit_with_mcp.call_tool(
+ ToolCallBlock(
+ id="xx",
+ type="tool_call",
+ name="tool_1",
+ input=json.dumps(
+ {
+ "arg1": "789",
+ "arg2": [7, 8, 9],
+ },
+ ),
+ ),
+ state=state,
+ )
+
+ final_response = None
+ async for chunk in res_gen:
+ if isinstance(chunk, ToolResponse):
+ final_response = chunk
+ else:
+ self.assertIsInstance(chunk, ToolChunk)
+
+ self.assertIsNotNone(final_response)
+ self.assertEqual(
+ final_response.content[0].text,
+ "arg1: 789, arg2: [7, 8, 9]",
+ )
+
+ self.toolkit.clear()
+ self.assertDictEqual(self.toolkit.tools, {})
+
+ # Try to add the mcp client
+ await self.toolkit.register_mcp_client(stateless_client)
+ self.assertListEqual(
+ self.toolkit.get_function_schemas(),
+ self.schemas,
+ )
+
+ self.toolkit.clear()
+
+ async def test_stateful_client(self) -> None:
+ """Test the stateful sse MCP client."""
+
+ # Test stateful client
+ stateful_client = HttpStatefulClient(
+ name="test_sse_client_stateful",
+ transport="sse",
+ url=f"http://127.0.0.1:{self.port}/sse",
+ )
+
+ self.assertFalse(stateful_client.is_connected)
+ await stateful_client.connect()
+
+ self.assertTrue(stateful_client.is_connected)
+
+ mcp_tool_1 = await stateful_client.get_tool("tool_1")
+ # Repeat to ensure idempotency
+ res_1: ToolChunk = await mcp_tool_1(arg1="12", arg2=[1, 2])
+ res_2: ToolChunk = await mcp_tool_1(arg1="34", arg2=[4, 5])
+ res_3: ToolChunk = await mcp_tool_1(arg1="34", arg2=[4, 5])
+
+ self.assertEqual(
+ res_1.content[0].text,
+ "arg1: 12, arg2: [1, 2]",
+ )
+ self.assertEqual(
+ res_2.content[0].text,
+ "arg1: 34, arg2: [4, 5]",
+ )
+ self.assertEqual(
+ res_3.content[0].text,
+ "arg1: 34, arg2: [4, 5]",
+ )
+
+ # with toolkit - Register MCPTool via Toolkit constructor
+ toolkit_with_mcp = Toolkit(tools=[mcp_tool_1])
+
+ self.assertListEqual(
+ toolkit_with_mcp.get_function_schemas(),
+ self.schemas,
+ )
+
+ state = AgentState()
+ res_gen = toolkit_with_mcp.call_tool(
+ ToolCallBlock(
+ id="xx",
+ type="tool_call",
+ name="tool_1",
+ input=json.dumps(
+ {
+ "arg1": "56",
+ "arg2": [5, 6],
+ },
+ ),
+ ),
+ state=state,
+ )
+
+ final_response = None
+ async for chunk in res_gen:
+ if isinstance(chunk, ToolResponse):
+ final_response = chunk
+ else:
+ self.assertIsInstance(chunk, ToolChunk)
+
+ self.assertIsNotNone(final_response)
+ self.assertEqual(
+ final_response.content[0].text,
+ "arg1: 56, arg2: [5, 6]",
+ )
+
+ # mcp client level test
+ self.toolkit.clear()
+ self.assertDictEqual(self.toolkit.tools, {})
+
+ await self.toolkit.register_mcp_client(stateful_client)
+ self.assertListEqual(
+ self.toolkit.get_function_schemas(),
+ self.schemas,
+ )
+
+ await stateful_client.close()
+ self.assertFalse(stateful_client.is_connected)
diff --git a/tests/mcp_streamable_http_client_test.py b/tests/mcp_streamable_http_client_test.py
new file mode 100644
index 000000000..34cf06e4b
--- /dev/null
+++ b/tests/mcp_streamable_http_client_test.py
@@ -0,0 +1,136 @@
+# -*- coding: utf-8 -*-
+"""The MCP client test module in agentscope."""
+import asyncio
+from multiprocessing import Process
+from unittest.async_case import IsolatedAsyncioTestCase
+
+from mcp.server import FastMCP
+from mcp.types import EmbeddedResource, TextResourceContents
+
+from agentscope.mcp import HttpStatelessClient, HttpStatefulClient
+from agentscope.tool import ToolChunk
+
+
+async def tool_1(arg1: str, arg2: list[int]) -> str:
+ """A test tool function.
+
+ Args:
+ arg1 (`str`):
+ The first argument named arg1.
+ arg2 (`list[int]`):
+ The second argument named arg2.
+ """
+ return f"arg1: {arg1}, arg2: {arg2}"
+
+
+async def tool_2() -> list:
+ """
+ A test tool function return the EmbeddedResource type
+ """
+ return [
+ EmbeddedResource(
+ type="resource",
+ resource=TextResourceContents(
+ uri="file://tmp.txt",
+ mimeType="text/plain",
+ text="test content",
+ ),
+ ),
+ ]
+
+
+def setup_server() -> None:
+ """Set up the streamable HTTP MCP server."""
+ sse_server = FastMCP("StreamableHTTP", port=8002)
+ sse_server.tool(description="A test tool function.")(tool_1)
+ sse_server.tool(
+ description="A test tool function with embedded resource.",
+ )(tool_2)
+ sse_server.run(transport="streamable-http")
+
+
+class StreamableHttpMCPClientTest(IsolatedAsyncioTestCase):
+ """Test class for streamable HTTP MCP client."""
+
+ async def asyncTearDown(self) -> None:
+ """Tear down the test environment."""
+ while self.process.is_alive():
+ self.process.terminate()
+ await asyncio.sleep(5)
+
+ async def asyncSetUp(self) -> None:
+ """Set up the test environment."""
+ self.port = 8002
+ self.process = Process(target=setup_server)
+ self.process.start()
+ await asyncio.sleep(10)
+
+ async def test_streamable_http_stateless_client(self) -> None:
+ """Test the MCP server connection functionality."""
+
+ client = HttpStatelessClient(
+ name="test_streamable_http_stateless_client",
+ transport="streamable_http",
+ url=f"http://127.0.0.1:{self.port}/mcp",
+ )
+
+ my_tool_1 = await client.get_tool("tool_1")
+ res_1: ToolChunk = await my_tool_1(arg1="123", arg2=[1, 2, 3])
+ self.assertEqual(
+ res_1.content[0].text,
+ "arg1: 123, arg2: [1, 2, 3]",
+ )
+
+ res_2: ToolChunk = await my_tool_1(arg1="345", arg2=[4, 5, 6])
+ self.assertEqual(
+ res_2.content[0].text,
+ "arg1: 345, arg2: [4, 5, 6]",
+ )
+
+ # Test stateful client connection
+ client = HttpStatefulClient(
+ name="test_streamable_http_stateless_client",
+ transport="streamable_http",
+ url=f"http://127.0.0.1:{self.port}/mcp",
+ )
+
+ self.assertFalse(client.is_connected)
+ await client.connect()
+
+ self.assertTrue(client.is_connected)
+
+ my_tool_1 = await client.get_tool("tool_1")
+ res_3: ToolChunk = await my_tool_1(arg1="12", arg2=[1, 2])
+ self.assertEqual(
+ res_3.content[0].text,
+ "arg1: 12, arg2: [1, 2]",
+ )
+
+ res_4: ToolChunk = await my_tool_1(arg1="34", arg2=[4, 5])
+ self.assertEqual(
+ res_4.content[0].text,
+ "arg1: 34, arg2: [4, 5]",
+ )
+
+ await client.close()
+ self.assertFalse(client.is_connected)
+
+ async def test_embedded_content(self) -> None:
+ """Test the EmbeddedContent functionality."""
+ client = HttpStatelessClient(
+ name="test_embedded_content",
+ transport="streamable_http",
+ url=f"http://127.0.0.1:{self.port}/mcp",
+ )
+
+ my_tool_2 = await client.get_tool("tool_2")
+ res: ToolChunk = await my_tool_2()
+ self.assertEqual(
+ res.content[0].text,
+ """{
+ "uri": "file://tmp.txt/",
+ "mimeType": "text/plain",
+ "meta": null,
+ "text": "test content"
+}""",
+ )
diff --git a/tests/toolkit_test.py b/tests/toolkit_test.py
index e91151600..2ea398d1d 100644
--- a/tests/toolkit_test.py
+++ b/tests/toolkit_test.py
@@ -3,6 +3,7 @@
import json
from typing import Any, AsyncGenerator, Generator
from unittest.async_case import IsolatedAsyncioTestCase
+from utils import AnyString
from agentscope.agent import AgentState
from agentscope.message import (
@@ -20,7 +21,6 @@ from agentscope.tool import (
PermissionBehavior,
ToolResponse,
)
-from tests.utils import AnyString
class Tool1(ToolBase):