|
| 1 | +"""Tests for MCP server list_resources, list_resource_templates, and read_resource.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | +from mcp.types import ( |
| 7 | + ListResourcesResult, |
| 8 | + ListResourceTemplatesResult, |
| 9 | + ReadResourceResult, |
| 10 | + Resource, |
| 11 | + ResourceTemplate, |
| 12 | + TextResourceContents, |
| 13 | +) |
| 14 | +from pydantic import AnyUrl |
| 15 | + |
| 16 | +from agents.mcp import MCPServerStreamableHttp |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def server(): |
| 21 | + return MCPServerStreamableHttp(params={"url": "http://localhost:8000/mcp"}) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.asyncio |
| 25 | +async def test_list_resources_raises_when_not_connected(server: MCPServerStreamableHttp): |
| 26 | + """list_resources raises UserError when server has not been connected.""" |
| 27 | + from agents.exceptions import UserError |
| 28 | + |
| 29 | + with pytest.raises(UserError, match="Server not initialized"): |
| 30 | + await server.list_resources() |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.asyncio |
| 34 | +async def test_list_resource_templates_raises_when_not_connected(server: MCPServerStreamableHttp): |
| 35 | + """list_resource_templates raises UserError when server has not been connected.""" |
| 36 | + from agents.exceptions import UserError |
| 37 | + |
| 38 | + with pytest.raises(UserError, match="Server not initialized"): |
| 39 | + await server.list_resource_templates() |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.asyncio |
| 43 | +async def test_read_resource_raises_when_not_connected(server: MCPServerStreamableHttp): |
| 44 | + """read_resource raises UserError when server has not been connected.""" |
| 45 | + from agents.exceptions import UserError |
| 46 | + |
| 47 | + with pytest.raises(UserError, match="Server not initialized"): |
| 48 | + await server.read_resource("file:///etc/hosts") |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.asyncio |
| 52 | +async def test_list_resources_returns_result(server: MCPServerStreamableHttp): |
| 53 | + """list_resources delegates to the underlying MCP session.""" |
| 54 | + mock_session = MagicMock() |
| 55 | + expected = ListResourcesResult( |
| 56 | + resources=[ |
| 57 | + Resource(uri=AnyUrl("file:///readme.md"), name="readme.md", mimeType="text/markdown"), |
| 58 | + ] |
| 59 | + ) |
| 60 | + mock_session.list_resources = AsyncMock(return_value=expected) |
| 61 | + server.session = mock_session |
| 62 | + |
| 63 | + result = await server.list_resources() |
| 64 | + |
| 65 | + assert result is expected |
| 66 | + mock_session.list_resources.assert_awaited_once_with(None) |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.asyncio |
| 70 | +async def test_list_resources_forwards_cursor(server: MCPServerStreamableHttp): |
| 71 | + """list_resources forwards the cursor argument for pagination.""" |
| 72 | + mock_session = MagicMock() |
| 73 | + page2 = ListResourcesResult(resources=[]) |
| 74 | + mock_session.list_resources = AsyncMock(return_value=page2) |
| 75 | + server.session = mock_session |
| 76 | + |
| 77 | + result = await server.list_resources(cursor="tok_abc") |
| 78 | + |
| 79 | + assert result is page2 |
| 80 | + mock_session.list_resources.assert_awaited_once_with("tok_abc") |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.asyncio |
| 84 | +async def test_list_resource_templates_returns_result(server: MCPServerStreamableHttp): |
| 85 | + """list_resource_templates delegates to the underlying MCP session.""" |
| 86 | + mock_session = MagicMock() |
| 87 | + expected = ListResourceTemplatesResult( |
| 88 | + resourceTemplates=[ |
| 89 | + ResourceTemplate(uriTemplate="file:///{path}", name="file"), |
| 90 | + ] |
| 91 | + ) |
| 92 | + mock_session.list_resource_templates = AsyncMock(return_value=expected) |
| 93 | + server.session = mock_session |
| 94 | + |
| 95 | + result = await server.list_resource_templates() |
| 96 | + |
| 97 | + assert result is expected |
| 98 | + mock_session.list_resource_templates.assert_awaited_once_with(None) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.asyncio |
| 102 | +async def test_list_resource_templates_forwards_cursor(server: MCPServerStreamableHttp): |
| 103 | + """list_resource_templates forwards the cursor argument for pagination.""" |
| 104 | + mock_session = MagicMock() |
| 105 | + page2 = ListResourceTemplatesResult(resourceTemplates=[]) |
| 106 | + mock_session.list_resource_templates = AsyncMock(return_value=page2) |
| 107 | + server.session = mock_session |
| 108 | + |
| 109 | + result = await server.list_resource_templates(cursor="tok_xyz") |
| 110 | + |
| 111 | + assert result is page2 |
| 112 | + mock_session.list_resource_templates.assert_awaited_once_with("tok_xyz") |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.asyncio |
| 116 | +async def test_read_resource_returns_result(server: MCPServerStreamableHttp): |
| 117 | + """read_resource delegates to the underlying MCP session with the given URI.""" |
| 118 | + mock_session = MagicMock() |
| 119 | + uri = "file:///readme.md" |
| 120 | + expected = ReadResourceResult( |
| 121 | + contents=[ |
| 122 | + TextResourceContents(uri=AnyUrl(uri), text="# Hello", mimeType="text/markdown"), |
| 123 | + ] |
| 124 | + ) |
| 125 | + mock_session.read_resource = AsyncMock(return_value=expected) |
| 126 | + server.session = mock_session |
| 127 | + |
| 128 | + result = await server.read_resource(uri) |
| 129 | + |
| 130 | + assert result is expected |
| 131 | + mock_session.read_resource.assert_awaited_once_with(AnyUrl(uri)) |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.asyncio |
| 135 | +async def test_base_methods_raise_not_implemented(): |
| 136 | + """Bare MCPServer subclasses that don't override resource methods get NotImplementedError.""" |
| 137 | + from mcp.types import CallToolResult, GetPromptResult, ListPromptsResult |
| 138 | + |
| 139 | + from agents.mcp import MCPServer |
| 140 | + |
| 141 | + class MinimalServer(MCPServer): |
| 142 | + """Minimal subclass implementing only the truly abstract methods.""" |
| 143 | + |
| 144 | + @property |
| 145 | + def name(self) -> str: |
| 146 | + return "minimal" |
| 147 | + |
| 148 | + async def connect(self) -> None: |
| 149 | + pass |
| 150 | + |
| 151 | + async def cleanup(self) -> None: |
| 152 | + pass |
| 153 | + |
| 154 | + async def list_tools(self, run_context=None, agent=None): |
| 155 | + return [] |
| 156 | + |
| 157 | + async def call_tool(self, tool_name, tool_arguments, run_context=None, agent=None): |
| 158 | + return CallToolResult(content=[]) |
| 159 | + |
| 160 | + async def list_prompts(self): |
| 161 | + return ListPromptsResult(prompts=[]) |
| 162 | + |
| 163 | + async def get_prompt(self, name, arguments=None): |
| 164 | + return GetPromptResult(messages=[]) |
| 165 | + |
| 166 | + s = MinimalServer() |
| 167 | + |
| 168 | + with pytest.raises(NotImplementedError, match="list_resources"): |
| 169 | + await s.list_resources() |
| 170 | + |
| 171 | + with pytest.raises(NotImplementedError, match="list_resource_templates"): |
| 172 | + await s.list_resource_templates() |
| 173 | + |
| 174 | + with pytest.raises(NotImplementedError, match="read_resource"): |
| 175 | + await s.read_resource("file:///test.txt") |
0 commit comments