Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions python/packages/autogen-ext/src/autogen_ext/tools/mcp/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from autogen_core.tools import BaseTool
from autogen_core.utils import schema_to_pydantic_model
from mcp import ClientSession, Tool
from mcp.types import AnyUrl, EmbeddedResource, ImageContent, TextContent
from pydantic import BaseModel
import json

from ._config import McpServerParams
from ._session import create_mcp_server_session
Expand Down Expand Up @@ -117,27 +115,6 @@ async def from_server_params(cls, server_params: TServerParams, tool_name: str)

return cls(server_params=server_params, tool=matching_tool)

def return_value_as_string(self, value: list) -> str:
"""Return a string representation of the result."""

def serialize_item(item):
if isinstance(item, (TextContent, ImageContent)):
return item.model_dump()
elif isinstance(item, EmbeddedResource):
type = item.type
resource = {}
for key, val in item.resource.model_dump().items():
if isinstance(val, AnyUrl):
resource[key] = str(val)
else:
resource[key] = val
annotations = item.annotations.model_dump() if item.annotations else None
return {"type": type, "resource": resource, "annotations": annotations}
else:
return str(item)

return json.dumps([serialize_item(item) for item in value])

def _format_errors(self, error: Exception) -> str:
"""Recursively format errors into a string."""

Expand Down
58 changes: 1 addition & 57 deletions python/packages/autogen-ext/tests/tools/test_mcp_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@
mcp_server_tools,
)
from mcp import ClientSession, Tool
from mcp.types import (
AnyUrl,
Annotations,
EmbeddedResource,
ImageContent,
TextContent,
TextResourceContents,
)


@pytest.fixture
Expand Down Expand Up @@ -190,53 +182,6 @@ async def test_adapter_from_server_params(
)


@pytest.mark.asyncio
async def test_adapter_from_server_params_with_return_value_as_string(
sample_tool: Tool,
sample_server_params: StdioServerParams,
mock_session: AsyncMock,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test that adapter can be created from server parameters."""
mock_context = AsyncMock()
mock_context.__aenter__.return_value = mock_session
monkeypatch.setattr(
"autogen_ext.tools.mcp._base.create_mcp_server_session",
lambda *args, **kwargs: mock_context, # type: ignore
)

mock_session.list_tools.return_value.tools = [sample_tool]

adapter = await StdioMcpToolAdapter.from_server_params(sample_server_params, "test_tool")

assert (
adapter.return_value_as_string(
[
TextContent(
text="this is a sample text",
type="text",
annotations=Annotations(audience=["user", "assistant"], priority=0.7),
),
ImageContent(
data="this is a sample base64 encoded image",
mimeType="image/png",
type="image",
annotations=None,
),
EmbeddedResource(
type="resource",
resource=TextResourceContents(
text="this is a sample text",
uri=AnyUrl(url="http://example.com/test"),
),
annotations=Annotations(audience=["user"], priority=0.3),
),
]
)
== '[{"type": "text", "text": "this is a sample text", "annotations": {"audience": ["user", "assistant"], "priority": 0.7}}, {"type": "image", "data": "this is a sample base64 encoded image", "mimeType": "image/png", "annotations": null}, {"type": "resource", "resource": {"uri": "http://example.com/test", "mimeType": null, "text": "this is a sample text"}, "annotations": {"audience": ["user"], "priority": 0.3}}]'
)


@pytest.mark.asyncio
async def test_adapter_from_factory(
sample_tool: Tool,
Expand Down Expand Up @@ -476,8 +421,7 @@ async def test_mcp_server_github() -> None:
assert len(tools) == 1
tool = tools[0]
result = await tool.run_json(
{"owner": "microsoft", "repo": "autogen", "path": "python", "branch": "main"},
CancellationToken(),
{"owner": "microsoft", "repo": "autogen", "path": "python", "branch": "main"}, CancellationToken()
)
assert result is not None

Expand Down