README - replace code snippets with examples (#1136)

This commit is contained in:
Inna Harper 2025-07-14 13:57:47 +01:00 • committed by GitHub
commit 9bf5160eea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,8 +21,10 @@ from examples.snippets.servers import (
basic_tool,
completion,
elicitation,
fastmcp_quickstart,
notifications,
sampling,
structured_output,
tool_progress,
)
from mcp.client.session import ClientSession
@ -100,6 +102,10 @@ def run_server_with_transport(module_name: str, port: int, transport: str) -> No
mcp = completion.mcp
elif module_name == "notifications":
mcp = notifications.mcp
elif module_name == "fastmcp_quickstart":
mcp = fastmcp_quickstart.mcp
elif module_name == "structured_output":
mcp = structured_output.mcp
else:
raise ImportError(f"Unknown module: {module_name}")
@ -590,3 +596,77 @@ async def test_completion(server_transport: str, server_url: str) -> None:
assert completion_result.completion is not None
assert "python" in completion_result.completion.values
assert all(lang.startswith("py") for lang in completion_result.completion.values)
# Test FastMCP quickstart example
@pytest.mark.anyio
@pytest.mark.parametrize(
"server_transport",
[
("fastmcp_quickstart", "sse"),
("fastmcp_quickstart", "streamable-http"),
],
indirect=True,
)
async def test_fastmcp_quickstart(server_transport: str, server_url: str) -> None:
"""Test FastMCP quickstart example."""
transport = server_transport
client_cm = create_client_for_transport(transport, server_url)
async with client_cm as client_streams:
read_stream, write_stream = unpack_streams(client_streams)
async with ClientSession(read_stream, write_stream) as session:
# Test initialization
result = await session.initialize()
assert isinstance(result, InitializeResult)
assert result.serverInfo.name == "Demo"
# Test add tool
tool_result = await session.call_tool("add", {"a": 10, "b": 20})
assert len(tool_result.content) == 1
assert isinstance(tool_result.content[0], TextContent)
assert tool_result.content[0].text == "30"
# Test greeting resource directly
from pydantic import AnyUrl
resource_result = await session.read_resource(AnyUrl("greeting://Alice"))
assert len(resource_result.contents) == 1
assert isinstance(resource_result.contents[0], TextResourceContents)
assert resource_result.contents[0].text == "Hello, Alice!"
# Test structured output example
@pytest.mark.anyio
@pytest.mark.parametrize(
"server_transport",
[
("structured_output", "sse"),
("structured_output", "streamable-http"),
],
indirect=True,
)
async def test_structured_output(server_transport: str, server_url: str) -> None:
"""Test structured output functionality."""
transport = server_transport
client_cm = create_client_for_transport(transport, server_url)
async with client_cm as client_streams:
read_stream, write_stream = unpack_streams(client_streams)
async with ClientSession(read_stream, write_stream) as session:
# Test initialization
result = await session.initialize()
assert isinstance(result, InitializeResult)
assert result.serverInfo.name == "Structured Output Example"
# Test get_weather tool
weather_result = await session.call_tool("get_weather", {"city": "New York"})
assert len(weather_result.content) == 1
assert isinstance(weather_result.content[0], TextContent)
# Check that the result contains expected weather data
result_text = weather_result.content[0].text
assert "72.5" in result_text # temperature
assert "sunny" in result_text # condition
assert "45" in result_text # humidity
assert "5.2" in result_text # wind_speed