test_mcp_tools.py 12.6 KB
Newer Older
1
2
3
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

4

5
6
7
import pytest
import pytest_asyncio
from openai import OpenAI
8
9
from openai_harmony import ToolDescription, ToolNamespaceConfig

10
from vllm.entrypoints.mcp.tool_server import MCPToolServer
11

12
from ....utils import RemoteOpenAIServer
13
14
15
16

MODEL_NAME = "openai/gpt-oss-20b"


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
def test_get_tool_description():
    """Test MCPToolServer.get_tool_description filtering logic.

    Note: The wildcard "*" is normalized to None by
    _extract_allowed_tools_from_mcp_requests before reaching this layer,
    so we only test None and specific tool filtering here.
    See test_serving_responses.py for "*" normalization tests.
    """
    pytest.importorskip("mcp")

    server = MCPToolServer()
    tool1 = ToolDescription.new(
        name="tool1", description="First", parameters={"type": "object"}
    )
    tool2 = ToolDescription.new(
        name="tool2", description="Second", parameters={"type": "object"}
    )
    tool3 = ToolDescription.new(
        name="tool3", description="Third", parameters={"type": "object"}
    )

    server.harmony_tool_descriptions = {
        "test_server": ToolNamespaceConfig(
            name="test_server", description="test", tools=[tool1, tool2, tool3]
        )
    }

    # Nonexistent server
    assert server.get_tool_description("nonexistent") is None

    # None (no filter) - returns all tools
    result = server.get_tool_description("test_server", allowed_tools=None)
    assert len(result.tools) == 3

    # Filter to specific tools
    result = server.get_tool_description(
        "test_server", allowed_tools=["tool1", "tool3"]
    )
    assert len(result.tools) == 2
    assert result.tools[0].name == "tool1"
    assert result.tools[1].name == "tool3"

    # Single tool
60
61
62
63
    result = server.get_tool_description(
        "test_server",
        allowed_tools=["tool2"],
    )
64
65
66
67
68
69
70
71
72
    assert len(result.tools) == 1
    assert result.tools[0].name == "tool2"

    # No matching tools - returns None
    result = server.get_tool_description("test_server", allowed_tools=["nonexistent"])
    assert result is None

    # Empty list - returns None
    assert server.get_tool_description("test_server", allowed_tools=[]) is None
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


class TestMCPEnabled:
    """Tests that require MCP tools to be enabled via environment variable."""

    @pytest.fixture(scope="class")
    def monkeypatch_class(self):
        from _pytest.monkeypatch import MonkeyPatch

        mpatch = MonkeyPatch()
        yield mpatch
        mpatch.undo()

    @pytest.fixture(scope="class")
    def mcp_enabled_server(self, monkeypatch_class: pytest.MonkeyPatch):
        args = ["--enforce-eager", "--tool-server", "demo"]

        with monkeypatch_class.context() as m:
            m.setenv("VLLM_ENABLE_RESPONSES_API_STORE", "1")
            m.setenv("PYTHON_EXECUTION_BACKEND", "dangerously_use_uv")
            m.setenv(
                "VLLM_GPT_OSS_SYSTEM_TOOL_MCP_LABELS", "code_interpreter,container"
            )
            # Helps the model follow instructions better
            m.setenv("VLLM_GPT_OSS_HARMONY_SYSTEM_INSTRUCTIONS", "1")
            with RemoteOpenAIServer(MODEL_NAME, args) as remote_server:
                yield remote_server

    @pytest_asyncio.fixture
    async def mcp_enabled_client(self, mcp_enabled_server):
        async with mcp_enabled_server.get_async_client() as async_client:
            yield async_client

    @pytest.mark.asyncio
    @pytest.mark.parametrize("model_name", [MODEL_NAME])
    async def test_mcp_tool_env_flag_enabled(
        self, mcp_enabled_client: OpenAI, model_name: str
    ):
        response = await mcp_enabled_client.responses.create(
            model=model_name,
            input=(
                "Execute the following code: "
                "import random; print(random.randint(1, 1000000))"
            ),
            instructions=(
                "You must use the Python tool to execute code. "
                "Never simulate execution."
            ),
            tools=[
                {
                    "type": "mcp",
                    "server_label": "code_interpreter",
                    # URL unused for DemoToolServer
                    "server_url": "http://localhost:8888",
                }
            ],
            extra_body={"enable_response_messages": True},
        )
        assert response is not None
        assert response.status == "completed"
        # Verify output messages: Tool calls and responses on analysis channel
        tool_call_found = False
        tool_response_found = False
        for message in response.output_messages:
            recipient = message.get("recipient")
            if recipient and recipient.startswith("python"):
                tool_call_found = True
                assert message.get("channel") == "analysis", (
                    "Tool call should be on analysis channel"
                )
            author = message.get("author", {})
            if (
                author.get("role") == "tool"
                and author.get("name")
                and author.get("name").startswith("python")
            ):
                tool_response_found = True
                assert message.get("channel") == "analysis", (
                    "Tool response should be on analysis channel"
                )

        assert tool_call_found, "Should have found at least one Python tool call"
        assert tool_response_found, (
            "Should have found at least one Python tool response"
        )
        for message in response.input_messages:
            assert message.get("author").get("role") != "developer", (
                "No developer messages should be present with valid mcp tool"
            )

    @pytest.mark.asyncio
    @pytest.mark.parametrize("model_name", [MODEL_NAME])
    async def test_mcp_tool_with_allowed_tools_star(
        self, mcp_enabled_client: OpenAI, model_name: str
    ):
        """Test MCP tool with allowed_tools=['*'] to select all available
        tools.

        This E2E test verifies that the "*" wildcard works end-to-end.
        See test_serving_responses.py for detailed unit tests of "*"
        normalization.
        """
        response = await mcp_enabled_client.responses.create(
            model=model_name,
            input=(
                "Execute the following code: "
                "import random; print(random.randint(1, 1000000))"
            ),
            instructions=(
                "You must use the Python tool to execute code. "
                "Never simulate execution."
            ),
            tools=[
                {
                    "type": "mcp",
                    "server_label": "code_interpreter",
                    "server_url": "http://localhost:8888",
                    # Using "*" to allow all tools from this MCP server
                    "allowed_tools": ["*"],
                }
            ],
            extra_body={"enable_response_messages": True},
        )
        assert response is not None
        assert response.status == "completed"
        # Verify tool calls work with allowed_tools=["*"]
        tool_call_found = False
        for message in response.output_messages:
            recipient = message.get("recipient")
            if recipient and recipient.startswith("python"):
                tool_call_found = True
                break
        assert tool_call_found, (
            "Should have found at least one Python tool call with '*'"
        )

    @pytest.mark.flaky(reruns=3)
    @pytest.mark.asyncio
    @pytest.mark.parametrize("model_name", [MODEL_NAME])
    async def test_mcp_tool_calling_streaming_types(
213
214
215
216
        self,
        pairs_of_event_types: dict[str, str],
        mcp_enabled_client: OpenAI,
        model_name: str,
217
218
219
220
221
222
223
    ):
        tools = [
            {
                "type": "mcp",
                "server_label": "code_interpreter",
            }
        ]
224
        input_text = "What is 123 * 456? Use python to calculate the result."
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

        stream_response = await mcp_enabled_client.responses.create(
            model=model_name,
            input=input_text,
            tools=tools,
            stream=True,
            instructions=(
                "You must use the Python tool to execute code. "
                "Never simulate execution."
            ),
        )

        stack_of_event_types = []
        saw_mcp_type = False
        async for event in stream_response:
            if event.type == "response.created":
                stack_of_event_types.append(event.type)
            elif event.type == "response.completed":
                assert stack_of_event_types[-1] == pairs_of_event_types[event.type]
                stack_of_event_types.pop()
            elif (
                event.type.endswith("added")
                or event.type == "response.mcp_call.in_progress"
            ):
                stack_of_event_types.append(event.type)
            elif event.type.endswith("delta"):
                if stack_of_event_types[-1] == event.type:
                    continue
                stack_of_event_types.append(event.type)
            elif (
                event.type.endswith("done")
                or event.type == "response.mcp_call.completed"
            ):
                assert stack_of_event_types[-1] == pairs_of_event_types[event.type]
                if "mcp_call" in event.type:
                    saw_mcp_type = True
                stack_of_event_types.pop()

        assert len(stack_of_event_types) == 0
        assert saw_mcp_type, "Should have seen at least one mcp call"


class TestMCPDisabled:
    """Tests that verify behavior when MCP tools are disabled."""

    @pytest.fixture(scope="class")
    def monkeypatch_class(self):
        from _pytest.monkeypatch import MonkeyPatch

        mpatch = MonkeyPatch()
        yield mpatch
        mpatch.undo()

    @pytest.fixture(scope="class")
    def mcp_disabled_server(self, monkeypatch_class: pytest.MonkeyPatch):
        args = ["--enforce-eager", "--tool-server", "demo"]

        with monkeypatch_class.context() as m:
            m.setenv("VLLM_ENABLE_RESPONSES_API_STORE", "1")
            m.setenv("PYTHON_EXECUTION_BACKEND", "dangerously_use_uv")
            # Helps the model follow instructions better
            m.setenv("VLLM_GPT_OSS_HARMONY_SYSTEM_INSTRUCTIONS", "1")
            with RemoteOpenAIServer(MODEL_NAME, args) as remote_server:
                yield remote_server

    @pytest_asyncio.fixture
    async def mcp_disabled_client(self, mcp_disabled_server):
        async with mcp_disabled_server.get_async_client() as async_client:
            yield async_client

    @pytest.mark.asyncio
    @pytest.mark.parametrize("model_name", [MODEL_NAME])
    async def test_mcp_tool_env_flag_disabled(
        self, mcp_disabled_client: OpenAI, model_name: str
    ):
        response = await mcp_disabled_client.responses.create(
            model=model_name,
            input=(
                "Execute the following code if the tool is present: "
                "import random; print(random.randint(1, 1000000))"
            ),
            tools=[
                {
                    "type": "mcp",
                    "server_label": "code_interpreter",
                    # URL unused for DemoToolServer
                    "server_url": "http://localhost:8888",
                }
            ],
            extra_body={"enable_response_messages": True},
        )
        assert response is not None
        assert response.status == "completed"
        # Verify output messages: No tool calls and responses
        tool_call_found = False
        tool_response_found = False
        for message in response.output_messages:
            recipient = message.get("recipient")
            if recipient and recipient.startswith("python"):
                tool_call_found = True
                assert message.get("channel") == "analysis", (
                    "Tool call should be on analysis channel"
                )
            author = message.get("author", {})
            if (
                author.get("role") == "tool"
                and author.get("name")
                and author.get("name").startswith("python")
            ):
                tool_response_found = True
                assert message.get("channel") == "analysis", (
                    "Tool response should be on analysis channel"
                )

        assert not tool_call_found, "Should not have a python call"
        assert not tool_response_found, "Should not have a tool response"
        for message in response.input_messages:
            assert message.get("author").get("role") != "developer", (
                "No developer messages should be present without a valid tool"
            )