-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathtest_real_host.py
More file actions
55 lines (45 loc) · 2.44 KB
/
Copy pathtest_real_host.py
File metadata and controls
55 lines (45 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""`docs/get-started/real-host.md`: the one server every host section on the page launches, driven in memory."""
import pytest
from inline_snapshot import snapshot
from mcp_types import TextContent, TextResourceContents
from docs_src.real_host import tutorial001
from mcp import Client
# See test_index.py for why this is a per-module mark and not a conftest hook.
pytestmark = [pytest.mark.anyio, pytest.mark.filterwarnings("error::mcp.MCPDeprecationWarning")]
async def test_the_host_sees_exactly_what_the_decorators_registered() -> None:
"""tutorial001: `tools/list` is what a host hands its model. Name, description, and schema come from the code."""
async with Client(tutorial001.mcp) as client:
search, get = (await client.list_tools()).tools
assert search.name == "search_books"
assert search.description == "Search the catalog by title or author."
assert search.input_schema == snapshot(
{
"type": "object",
"additionalProperties": False,
"properties": {"query": {"title": "Query", "type": "string"}},
"required": ["query"],
"title": "search_booksArguments",
}
)
assert get.name == "get_author"
async def test_a_tool_call_round_trips_the_way_a_host_drives_it() -> None:
"""tutorial001: `tools/call` sends arguments in; the function's return value comes back as the result."""
async with Client(tutorial001.mcp) as client:
result = await client.call_tool("search_books", {"query": "gibson"})
assert not result.is_error
assert result.structured_content == {"result": ["Neuromancer"]}
author = await client.call_tool("get_author", {"title": "Dune"})
assert author.content == [TextContent(type="text", text="Frank Herbert")]
async def test_the_resource_a_host_can_attach_to_context() -> None:
"""tutorial001: `catalog://titles` has no parameter, so it is a concrete, listable, readable resource."""
async with Client(tutorial001.mcp) as client:
(resource,) = (await client.list_resources()).resources
assert str(resource.uri) == "catalog://titles"
result = await client.read_resource("catalog://titles")
assert result.contents == [
TextResourceContents(
uri="catalog://titles",
mime_type="text/plain",
text="Dune\nNeuromancer\nThe Left Hand of Darkness",
)
]