fastmcp/tests/test_examples.py
Jeremiah Lowin 0600834da4 Fix single-element list unwrapping in tool content
Single-element lists like [1] were being incorrectly unwrapped to "1"
in unstructured content while multi-element lists remained as lists.
This created inconsistent behavior where the structure was lost for
single items.

This fix ensures lists always preserve their structure in unstructured
content regardless of length, making behavior consistent and predictable.

Also removes pretty-printing from JSON serialization for more compact
output across tools, prompts, and resources.

Fixes #1064

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-07 12:07:16 -04:00

69 lines
2.4 KiB
Python

"""Tests for example servers"""
from pydantic import AnyUrl
from fastmcp import Client
async def test_simple_echo():
"""Test the simple echo server"""
from examples.simple_echo import mcp
async with Client(mcp) as client:
result = await client.call_tool_mcp("echo", {"text": "hello"})
assert len(result.content) == 1
assert result.content[0].text == "hello" # type: ignore[attr-defined]
async def test_complex_inputs():
"""Test the complex inputs server"""
from examples.complex_inputs import mcp
async with Client(mcp) as client:
tank = {"shrimp": [{"name": "bob"}, {"name": "alice"}]}
result = await client.call_tool_mcp(
"name_shrimp", {"tank": tank, "extra_names": ["charlie"]}
)
assert len(result.content) == 1
assert result.content[0].text == '["bob","alice","charlie"]' # type: ignore[attr-defined]
async def test_desktop(monkeypatch):
"""Test the desktop server"""
from examples.desktop import mcp
async with Client(mcp) as client:
# Test the add function
result = await client.call_tool_mcp("add", {"a": 1, "b": 2})
assert len(result.content) == 1
assert result.content[0].text == "3" # type: ignore[attr-defined]
async with Client(mcp) as client:
result = await client.read_resource(AnyUrl("greeting://rooter12"))
assert len(result) == 1
assert result[0].text == "Hello, rooter12!" # type: ignore[attr-defined]
async def test_echo():
"""Test the echo server"""
from examples.echo import mcp
async with Client(mcp) as client:
result = await client.call_tool_mcp("echo_tool", {"text": "hello"})
assert len(result.content) == 1
assert result.content[0].text == "hello" # type: ignore[attr-defined]
async with Client(mcp) as client:
result = await client.read_resource(AnyUrl("echo://static"))
assert len(result) == 1
assert result[0].text == "Echo!" # type: ignore[attr-defined]
async with Client(mcp) as client:
result = await client.read_resource(AnyUrl("echo://server42"))
assert len(result) == 1
assert result[0].text == "Echo: server42" # type: ignore[attr-defined]
async with Client(mcp) as client:
result = await client.get_prompt("echo", {"text": "hello"})
assert len(result.messages) == 1
assert result.messages[0].content.text == "hello" # type: ignore[attr-defined]