Skip to content

Commit 6af5810

Browse files
committed
Added integration tests
1 parent cad3ff2 commit 6af5810

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

tests_integ/mcp/test_mcp_client.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,129 @@ def transport_callback() -> MCPTransport:
267267
assert "Hello, Charlie!" in prompt_text
268268

269269

270+
def test_mcp_client_embedded_resources():
271+
"""Test that MCP client properly handles EmbeddedResource content types."""
272+
embedded_resource_mcp_client = MCPClient(
273+
lambda: stdio_client(StdioServerParameters(command="python", args=["tests_integ/embedded_resource_server.py"]))
274+
)
275+
276+
with embedded_resource_mcp_client:
277+
# Test text embedded resource
278+
text_result = embedded_resource_mcp_client.call_tool_sync(
279+
tool_use_id="test-embedded-text",
280+
name="get_text_file_content",
281+
arguments={},
282+
)
283+
assert text_result["status"] == "success"
284+
assert len(text_result["content"]) == 1
285+
assert text_result["content"][0]["text"] == "Hello, this is embedded text content!"
286+
287+
# Test JSON embedded resource (blob with textual MIME type)
288+
json_result = embedded_resource_mcp_client.call_tool_sync(
289+
tool_use_id="test-embedded-json",
290+
name="get_json_file_content",
291+
arguments={},
292+
)
293+
assert json_result["status"] == "success"
294+
assert len(json_result["content"]) == 1
295+
json_content = json_result["content"][0]["text"]
296+
assert "Hello from embedded JSON!" in json_content
297+
assert "test" in json_content
298+
299+
# Test YAML embedded resource (blob with textual MIME type)
300+
yaml_result = embedded_resource_mcp_client.call_tool_sync(
301+
tool_use_id="test-embedded-yaml",
302+
name="get_yaml_file_content",
303+
arguments={},
304+
)
305+
assert yaml_result["status"] == "success"
306+
assert len(yaml_result["content"]) == 1
307+
yaml_content = yaml_result["content"][0]["text"]
308+
assert "Hello from embedded YAML!" in yaml_content
309+
assert "item1" in yaml_content
310+
311+
# Test image embedded resource
312+
image_result = embedded_resource_mcp_client.call_tool_sync(
313+
tool_use_id="test-embedded-image",
314+
name="get_image_content",
315+
arguments={},
316+
)
317+
assert image_result["status"] == "success"
318+
assert len(image_result["content"]) == 1
319+
assert "image" in image_result["content"][0]
320+
assert image_result["content"][0]["image"]["format"] == "png"
321+
assert "bytes" in image_result["content"][0]["image"]["source"]
322+
323+
# Test binary embedded resource (should be dropped due to non-textual MIME type)
324+
binary_result = embedded_resource_mcp_client.call_tool_sync(
325+
tool_use_id="test-embedded-binary",
326+
name="get_binary_content",
327+
arguments={},
328+
)
329+
assert binary_result["status"] == "success"
330+
# Binary content should be dropped, so no content returned
331+
assert len(binary_result["content"]) == 0
332+
333+
334+
@pytest.mark.asyncio
335+
async def test_mcp_client_embedded_resources_async():
336+
"""Test that async MCP client properly handles EmbeddedResource content types."""
337+
embedded_resource_mcp_client = MCPClient(
338+
lambda: stdio_client(StdioServerParameters(command="python", args=["tests_integ/embedded_resource_server.py"]))
339+
)
340+
341+
with embedded_resource_mcp_client:
342+
# Test text embedded resource async
343+
text_result = await embedded_resource_mcp_client.call_tool_async(
344+
tool_use_id="test-embedded-text-async",
345+
name="get_text_file_content",
346+
arguments={},
347+
)
348+
assert text_result["status"] == "success"
349+
assert len(text_result["content"]) == 1
350+
assert text_result["content"][0]["text"] == "Hello, this is embedded text content!"
351+
352+
# Test JSON embedded resource async
353+
json_result = await embedded_resource_mcp_client.call_tool_async(
354+
tool_use_id="test-embedded-json-async",
355+
name="get_json_file_content",
356+
arguments={},
357+
)
358+
assert json_result["status"] == "success"
359+
assert len(json_result["content"]) == 1
360+
json_content = json_result["content"][0]["text"]
361+
assert "Hello from embedded JSON!" in json_content
362+
363+
364+
def test_mcp_client_embedded_resources_with_agent():
365+
"""Test that embedded resources work correctly when used with Agent."""
366+
embedded_resource_mcp_client = MCPClient(
367+
lambda: stdio_client(StdioServerParameters(command="python", args=["tests_integ/embedded_resource_server.py"]))
368+
)
369+
370+
with embedded_resource_mcp_client:
371+
tools = embedded_resource_mcp_client.list_tools_sync()
372+
agent = Agent(tools=tools)
373+
374+
# Test that agent can successfully use tools that return embedded resources
375+
result = agent("Get the text file content and tell me what it says")
376+
377+
# Check that the agent successfully processed the embedded resource
378+
assert result.message is not None
379+
response_text = " ".join([
380+
block["text"]
381+
for block in result.message["content"]
382+
if "text" in block
383+
]).lower()
384+
385+
# The agent should have received and processed the embedded text content
386+
assert any([
387+
"hello" in response_text,
388+
"embedded text content" in response_text,
389+
"text content" in response_text
390+
])
391+
392+
270393
def _messages_to_content_blocks(messages: List[Message]) -> List[ToolUse]:
271394
return [block["toolUse"] for message in messages for block in message["content"] if "toolUse" in block]
272395

0 commit comments

Comments
 (0)