|
| 1 | +"""Tests for the MCP Apps extension (`io.modelcontextprotocol/ui`, SEP-2133). |
| 2 | +
|
| 3 | +The headline property is SEP-2133 graceful degradation: a UI-bound tool returns |
| 4 | +rich output to a client that negotiated Apps and text-only output to one that did |
| 5 | +not. The remaining tests pin SDK-defined wiring (the `_meta.ui.resourceUri` stamp, |
| 6 | +the `ui://` resource MIME type, capability advertisement, and `ui://`-scheme |
| 7 | +validation). |
| 8 | +""" |
| 9 | + |
| 10 | +import mcp_types as types |
| 11 | +import pytest |
| 12 | +from inline_snapshot import snapshot |
| 13 | +from mcp_types import CallToolResult, ReadResourceResult, TextContent, TextResourceContents |
| 14 | + |
| 15 | +from mcp.client.client import Client |
| 16 | +from mcp.server import Server, ServerRequestContext |
| 17 | +from mcp.server.apps import APP_MIME_TYPE, EXTENSION_ID, Apps, client_supports_apps |
| 18 | +from mcp.server.mcpserver import MCPServer |
| 19 | +from mcp.server.mcpserver.context import Context |
| 20 | + |
| 21 | +pytestmark = pytest.mark.anyio |
| 22 | + |
| 23 | + |
| 24 | +def _clock_server() -> MCPServer: |
| 25 | + apps = Apps() |
| 26 | + |
| 27 | + @apps.tool(resource_uri="ui://clock/app.html", title="Get Time", description="Return the current time.") |
| 28 | + def get_time(ctx: Context) -> str: |
| 29 | + if not client_supports_apps(ctx): |
| 30 | + return "The time is 2026-06-26T00:00:00Z." |
| 31 | + return "2026-06-26T00:00:00Z" |
| 32 | + |
| 33 | + apps.add_html_resource("ui://clock/app.html", "<title>Clock</title>", title="Clock") |
| 34 | + return MCPServer("clock", extensions=[apps]) |
| 35 | + |
| 36 | + |
| 37 | +async def test_apps_tool_stamps_ui_resource_uri_on_tool_meta() -> None: |
| 38 | + """SDK-defined: `@apps.tool(resource_uri=...)` stamps `_meta.ui.resourceUri` on the |
| 39 | + advertised tool, observed end-to-end through `list_tools`.""" |
| 40 | + async with Client(_clock_server()) as client: |
| 41 | + result = await client.list_tools() |
| 42 | + assert [(t.name, t.meta) for t in result.tools] == snapshot( |
| 43 | + [("get_time", {"ui": {"resourceUri": "ui://clock/app.html"}})] |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +async def test_add_html_resource_serves_ui_resource_at_app_mime_type() -> None: |
| 48 | + """SDK-defined: `add_html_resource` registers the `ui://` resource served as |
| 49 | + `text/html;profile=mcp-app`, observed through `read_resource`.""" |
| 50 | + async with Client(_clock_server()) as client: |
| 51 | + result = await client.read_resource("ui://clock/app.html") |
| 52 | + assert result == snapshot( |
| 53 | + ReadResourceResult( |
| 54 | + contents=[ |
| 55 | + TextResourceContents( |
| 56 | + uri="ui://clock/app.html", |
| 57 | + mime_type="text/html;profile=mcp-app", |
| 58 | + text="<title>Clock</title>", |
| 59 | + ) |
| 60 | + ] |
| 61 | + ) |
| 62 | + ) |
| 63 | + assert isinstance(result.contents[0], TextResourceContents) |
| 64 | + assert result.contents[0].mime_type == APP_MIME_TYPE |
| 65 | + |
| 66 | + |
| 67 | +async def test_auto_mode_carries_apps_extension_under_server_capabilities() -> None: |
| 68 | + """SDK-defined: the Apps extension rides `server/discover`, so a `mode='auto'` client |
| 69 | + sees `EXTENSION_ID` under `server_capabilities.extensions`.""" |
| 70 | + async with Client(_clock_server(), mode="auto") as client: |
| 71 | + assert client.server_capabilities.extensions == snapshot({"io.modelcontextprotocol/ui": {}}) |
| 72 | + |
| 73 | + |
| 74 | +async def test_legacy_handshake_drops_apps_extension_from_capabilities() -> None: |
| 75 | + """Pinned gap: the 2025 `ServerCapabilities` wire schema has no `extensions` field, |
| 76 | + so a `mode='legacy'` handshake cannot carry the Apps capability -- only `mode='auto'` |
| 77 | + (server/discover) does. This pins the divergence rather than fixing it.""" |
| 78 | + async with Client(_clock_server(), mode="legacy") as client: |
| 79 | + assert client.server_capabilities.extensions is None |
| 80 | + |
| 81 | + |
| 82 | +async def test_apps_tool_returns_rich_output_when_client_negotiated_apps() -> None: |
| 83 | + """SEP-2133 graceful degradation: a client that advertised `EXTENSION_ID` gets the |
| 84 | + rich (UI) path, while one that did not gets the text-only fallback. The same tool, |
| 85 | + branching on `client_supports_apps(ctx)`, drives both halves.""" |
| 86 | + server = _clock_server() |
| 87 | + |
| 88 | + async with Client(server, extensions={EXTENSION_ID: {"mimeTypes": [APP_MIME_TYPE]}}) as supports: |
| 89 | + rich = await supports.call_tool("get_time", {}) |
| 90 | + async with Client(server) as plain: |
| 91 | + fallback = await plain.call_tool("get_time", {}) |
| 92 | + |
| 93 | + assert rich.content == snapshot([TextContent(text="2026-06-26T00:00:00Z")]) |
| 94 | + assert fallback.content == snapshot([TextContent(text="The time is 2026-06-26T00:00:00Z.")]) |
| 95 | + |
| 96 | + |
| 97 | +async def test_client_supports_apps_reads_lowlevel_request_context() -> None: |
| 98 | + """SDK-defined: `client_supports_apps` accepts a lowlevel `ServerRequestContext` too, |
| 99 | + reading the client's advertised extensions off `session.client_params`.""" |
| 100 | + observed: list[bool] = [] |
| 101 | + |
| 102 | + async def list_tools( |
| 103 | + ctx: ServerRequestContext, params: types.PaginatedRequestParams | None |
| 104 | + ) -> types.ListToolsResult: |
| 105 | + return types.ListToolsResult(tools=[types.Tool(name="probe", input_schema={"type": "object"})]) |
| 106 | + |
| 107 | + async def call_tool(ctx: ServerRequestContext, params: types.CallToolRequestParams) -> CallToolResult: |
| 108 | + assert params.name == "probe" |
| 109 | + observed.append(client_supports_apps(ctx)) |
| 110 | + return CallToolResult(content=[TextContent(text="ok")]) |
| 111 | + |
| 112 | + server = Server("probe", on_list_tools=list_tools, on_call_tool=call_tool) |
| 113 | + |
| 114 | + async with Client(server, extensions={EXTENSION_ID: {"mimeTypes": [APP_MIME_TYPE]}}) as supports: |
| 115 | + await supports.call_tool("probe", {}) |
| 116 | + async with Client(server) as plain: |
| 117 | + await plain.call_tool("probe", {}) |
| 118 | + |
| 119 | + assert observed == [True, False] |
| 120 | + |
| 121 | + |
| 122 | +def test_apps_tool_rejects_non_ui_resource_uri() -> None: |
| 123 | + """SDK-defined: `@apps.tool` accepts only `ui://` URIs; any other scheme is a |
| 124 | + programmer error raised at decoration time.""" |
| 125 | + apps = Apps() |
| 126 | + with pytest.raises(ValueError): |
| 127 | + apps.tool(resource_uri="https://example.com/app.html") |
| 128 | + |
| 129 | + |
| 130 | +def test_add_html_resource_rejects_non_ui_resource_uri() -> None: |
| 131 | + """SDK-defined: `add_html_resource` accepts only `ui://` URIs; any other scheme is |
| 132 | + a programmer error raised at registration time.""" |
| 133 | + apps = Apps() |
| 134 | + with pytest.raises(ValueError): |
| 135 | + apps.add_html_resource("https://example.com/app.html", "<title>x</title>") |
0 commit comments