File size: 12,707 Bytes
4b03eed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- 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 pydantic import BaseModel

from agentscope.mcp import MCPClient, HttpMCPConfig
from agentscope.message import ToolCallBlock
from agentscope.tool import ToolResponse, ToolChunk, Toolkit
from agentscope.state 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")


# ---------------------------------------------------------------------------
# Server / tool definitions for $defs preservation test
# ---------------------------------------------------------------------------


class _ItemConfig(BaseModel):
    """Config sub-model to generate $defs in the MCP inputSchema."""

    key: str
    count: int


async def tool_with_model(name: str, config: _ItemConfig) -> str:
    """A tool whose parameter uses a Pydantic sub-model.



    Args:

        name: Item name.

        config: Item configuration.

    """
    return f"name={name}, key={config.key}, count={config.count}"


def setup_defs_server() -> None:
    """Set up an SSE MCP server that exposes a tool with Pydantic

    sub-models."""
    server = FastMCP("DefsSSE", port=8005)
    server.tool()(tool_with_model)
    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": "mcp__test_sse_client__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."""
        # Create stateless client (is_stateful=False)
        stateless_client = MCPClient(
            name="test_sse_client",
            is_stateful=False,
            mcp_config=HttpMCPConfig(
                type="http_mcp",
                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 = await toolkit_with_mcp.get_tool_schemas()

        self.assertListEqual(
            schemas,
            self.schemas,
        )

        state = AgentState()
        res_gen = toolkit_with_mcp.call_tool(
            ToolCallBlock(
                id="xx",
                type="tool_call",
                name="mcp__test_sse_client__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.assertListEqual(self.toolkit.tool_groups, [])

        # Try to add the mcp client
        self.toolkit = Toolkit(mcps=[stateless_client])
        self.assertListEqual(
            await self.toolkit.get_tool_schemas(),
            self.schemas,
        )

        self.toolkit.clear()

    async def test_stateful_client(self) -> None:
        """Test the stateful sse MCP client."""

        # Test stateful client (is_stateful=True)
        stateful_client = MCPClient(
            name="test_sse_client",
            is_stateful=True,
            mcp_config=HttpMCPConfig(
                type="http_mcp",
                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(
            await toolkit_with_mcp.get_tool_schemas(),
            self.schemas,
        )

        state = AgentState()
        res_gen = toolkit_with_mcp.call_tool(
            ToolCallBlock(
                id="xx",
                type="tool_call",
                name="mcp__test_sse_client__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.assertListEqual(self.toolkit.tool_groups, [])

        self.toolkit = Toolkit(mcps=[stateful_client])
        self.assertListEqual(
            await self.toolkit.get_tool_schemas(),
            self.schemas,
        )

        await stateful_client.close()
        self.assertFalse(stateful_client.is_connected)


class SseSchemaDefsPreservationTest(IsolatedAsyncioTestCase):
    """End-to-end tests for $defs preservation in MCP tool schemas.



    These tests start a real FastMCP server that exposes a tool whose

    parameter is a Pydantic sub-model.  FastMCP generates an inputSchema with

    ``$defs`` for the sub-model.  We verify that the schema returned by

    ``await toolkit.get_tool_schemas()`` preserves those ``$defs`` and that

    Pydantic-generated ``title`` fields inside ``$defs`` are stripped.

    """

    async def asyncSetUp(self) -> None:
        """Start the $defs test server."""
        self.port = 8005
        self.process = Process(target=setup_defs_server)
        self.process.start()
        await asyncio.sleep(10)

        self.schemas = [
            {
                "type": "function",
                "function": {
                    "name": "mcp__test_defs_client__tool_with_model",
                    "description": "A tool whose parameter uses a "
                    "Pydantic sub-model.\n\n    Args:\n        "
                    "name: Item name.\n        "
                    "config: Item configuration.\n    ",
                    "parameters": {
                        "$defs": {
                            "_ItemConfig": {
                                "description": "Config sub-model to "
                                "generate $defs in the "
                                "MCP inputSchema.",
                                "properties": {
                                    "key": {"type": "string"},
                                    "count": {"type": "integer"},
                                },
                                "required": ["key", "count"],
                                "type": "object",
                            },
                        },
                        "properties": {
                            "name": {"type": "string"},
                            "config": {"$ref": "#/$defs/_ItemConfig"},
                        },
                        "required": ["name", "config"],
                        "type": "object",
                    },
                },
            },
        ]

    async def asyncTearDown(self) -> None:
        """Stop the $defs test server."""
        while self.process.is_alive():
            self.process.terminate()
            await asyncio.sleep(5)

    async def test_defs_preserved_and_titles_stripped(self) -> None:
        """$defs from Pydantic sub-model parameters must survive the full

        pipeline.



        Failure scenario (before fix):

            MCPTool.__init__ only copied ``properties`` and ``required``,

            so ``$defs._ItemConfig`` was silently dropped.  The LLM would

            receive a schema where ``config`` had an unresolvable

            ``$ref: "#/$defs/_ItemConfig"``.



        Expected behaviour (after fix):

            - ``MCPTool.input_schema`` contains ``$defs._ItemConfig``

            - ``await toolkit.get_tool_schemas()`` output contains ``$defs``

              with the ref resolved and Pydantic titles stripped.

        """
        client = MCPClient(
            name="test_defs_client",
            is_stateful=False,
            mcp_config=HttpMCPConfig(
                type="http_mcp",
                url=f"http://127.0.0.1:{self.port}/sse",
            ),
        )

        mcp_tool = await client.get_tool("tool_with_model")

        # 1. input_schema must preserve $defs
        self.assertIn(
            "$defs",
            mcp_tool.input_schema,
            "MCPTool.input_schema must preserve $defs from inputSchema",
        )

        # 2. get_tool_schemas() must preserve $defs and strip titles
        toolkit = Toolkit(tools=[mcp_tool])
        schemas = await toolkit.get_tool_schemas()
        self.assertListEqual(schemas, self.schemas)