|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from unittest.mock import AsyncMock |
| 16 | + |
| 17 | +import pytest |
| 18 | +from autogen_ext.tools.mcp import SseMcpToolAdapter, SseServerParams |
| 19 | +from mcp import ClientSession, Tool |
| 20 | +from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics |
| 21 | + |
| 22 | +from newrelic.api.background_task import background_task |
| 23 | + |
| 24 | +# Test setup derived from: https://github.com/microsoft/autogen/blob/main/python/packages/autogen-ext/tests/tools/test_mcp_tools.py |
| 25 | +# autogen MIT license: https://github.com/microsoft/autogen/blob/main/LICENSE and |
| 26 | +# https://github.com/microsoft/autogen/blob/main/LICENSE-CODE |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def mock_sse_session(): |
| 31 | + session = AsyncMock(spec=ClientSession) |
| 32 | + session.initialize = AsyncMock() |
| 33 | + session.call_tool = AsyncMock() |
| 34 | + session.list_tools = AsyncMock() |
| 35 | + return session |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def add_exclamation(): |
| 40 | + return Tool( |
| 41 | + name="add_exclamation", |
| 42 | + description="A test SSE tool that adds an exclamation mark to a string", |
| 43 | + inputSchema={"type": "object", "properties": {"input": {"type": "string"}}, "required": ["input"]}, |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +@validate_transaction_metrics( |
| 48 | + "test_mcp_tool_adapter:test_from_server_params_tracing", |
| 49 | + scoped_metrics=[("Llm/autogen_ext.tools.mcp._sse:SseMcpToolAdapter.from_server_params/add_exclamation", 1)], |
| 50 | + rollup_metrics=[("Llm/autogen_ext.tools.mcp._sse:SseMcpToolAdapter.from_server_params/add_exclamation", 1)], |
| 51 | + background_task=True, |
| 52 | +) |
| 53 | +@background_task() |
| 54 | +def test_from_server_params_tracing(loop, mock_sse_session, monkeypatch, add_exclamation): |
| 55 | + async def _test(): |
| 56 | + params = SseServerParams(url="http://test-url") |
| 57 | + mock_context = AsyncMock() |
| 58 | + mock_context.__aenter__.return_value = mock_sse_session |
| 59 | + monkeypatch.setattr( |
| 60 | + "autogen_ext.tools.mcp._base.create_mcp_server_session", lambda *args, **kwargs: mock_context |
| 61 | + ) |
| 62 | + |
| 63 | + mock_sse_session.list_tools.return_value.tools = [add_exclamation] |
| 64 | + |
| 65 | + adapter = await SseMcpToolAdapter.from_server_params(params, "add_exclamation") |
| 66 | + |
| 67 | + loop.run_until_complete(_test()) |
0 commit comments