Skip to content

Commit 176d53b

Browse files
ZiTao-LiDavdGao
andauthored
feat: Add support for the mcp.types.EmbeddedResource with text resource (#839)
--------- Co-authored-by: DavdGao <[email protected]>
1 parent 7a61962 commit 176d53b

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/agentscope/mcp/_client_base.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ def _convert_mcp_content_to_as_blocks(
6767
),
6868
),
6969
)
70+
elif isinstance(content, mcp.types.EmbeddedResource):
71+
if isinstance(
72+
content.resource,
73+
mcp.types.TextResourceContents,
74+
):
75+
as_content.append(
76+
TextBlock(
77+
type="text",
78+
text=content.resource.model_dump_json(indent=2),
79+
),
80+
)
81+
else:
82+
# TODO: support the BlobResourceContents in the future,
83+
# which is a base64-encoded string representing the
84+
# binary data
85+
logger.error(
86+
"Unsupported EmbeddedResource content type: %s. "
87+
"Skipping this content.",
88+
type(content.resource),
89+
)
7090
else:
7191
logger.warning(
7292
"Unsupported content type: %s. Skipping this content.",

tests/mcp_streamable_http_client_test.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import mcp.types
88
from mcp.server import FastMCP
9+
from mcp.types import EmbeddedResource, TextResourceContents
910

1011
from agentscope.mcp import HttpStatelessClient, HttpStatefulClient
1112
from agentscope.message import TextBlock
@@ -24,10 +25,29 @@ async def tool_1(arg1: str, arg2: list[int]) -> str:
2425
return f"arg1: {arg1}, arg2: {arg2}"
2526

2627

28+
async def tool_2() -> list:
29+
"""
30+
A test tool function return the EmbeddedResource type
31+
"""
32+
return [
33+
EmbeddedResource(
34+
type="resource",
35+
resource=TextResourceContents(
36+
uri="file://tmp.txt",
37+
mimeType="text/plain",
38+
text="test content",
39+
),
40+
),
41+
]
42+
43+
2744
def setup_server() -> None:
2845
"""Set up the streamable HTTP MCP server."""
2946
sse_server = FastMCP("StreamableHTTP", port=8002)
3047
sse_server.tool(description="A test tool function.")(tool_1)
48+
sse_server.tool(
49+
description="A test tool function with embedded resource.",
50+
)(tool_2)
3151
sse_server.run(transport="streamable-http")
3252

3353

@@ -132,3 +152,34 @@ async def test_streamable_http_stateless_client(self) -> None:
132152

133153
await client.close()
134154
self.assertFalse(client.is_connected)
155+
156+
async def test_embedded_content(self) -> None:
157+
"""Test the EmbeddedContent functionality."""
158+
client = HttpStatelessClient(
159+
name="test_embedded_content",
160+
transport="streamable_http",
161+
url=f"http://127.0.0.1:{self.port}/mcp",
162+
)
163+
164+
func_3 = await client.get_callable_function(
165+
"tool_2",
166+
wrap_tool_result=True,
167+
)
168+
res: ToolResponse = await func_3()
169+
self.assertEqual(
170+
res,
171+
ToolResponse(
172+
id=res.id,
173+
content=[
174+
TextBlock(
175+
type="text",
176+
text="""{
177+
"uri": "file://tmp.txt/",
178+
"mimeType": "text/plain",
179+
"meta": null,
180+
"text": "test content"
181+
}""",
182+
),
183+
],
184+
),
185+
)

0 commit comments

Comments
 (0)