Skip to content

Commit eb3dd47

Browse files
committed
push
1 parent 03393fc commit eb3dd47

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

src/mcp/server/mcpserver/resources/resource_manager.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,8 @@ def add_template(
8282
return template
8383

8484
async def get_resource(
85-
self,
86-
uri: AnyUrl | str,
87-
context: Context[ServerSessionT, LifespanContextT, RequestT] | None = None,
88-
) -> Resource | None:
85+
self, uri: AnyUrl | str, context: Context[ServerSessionT, LifespanContextT, RequestT] | None = None
86+
) -> Resource:
8987
"""Get resource by URI, checking concrete resources first, then templates."""
9088
uri_str = str(uri)
9189
logger.debug("Getting resource", extra={"uri": uri_str})

src/mcp/server/mcpserver/server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,9 @@ async def read_resource(self, uri: AnyUrl | str) -> Iterable[ReadResourceContent
347347
"""Read a resource by URI."""
348348

349349
context = self.get_context()
350-
resource = await self._resource_manager.get_resource(uri, context=context)
351-
if not resource:
350+
try:
351+
resource = await self._resource_manager.get_resource(uri, context=context)
352+
except ValueError:
352353
raise ResourceError(f"Unknown resource: {uri}")
353354

354355
try:

tests/issues/test_141_resource_templates.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from mcp import Client
44
from mcp.server.mcpserver import MCPServer
5+
from mcp.server.mcpserver.exceptions import ResourceError
56
from mcp.types import (
67
ListResourceTemplatesResult,
78
TextResourceContents,
@@ -54,10 +55,10 @@ def get_user_profile_missing(user_id: str) -> str: # pragma: no cover
5455
assert result_list[0].mime_type == "text/plain"
5556

5657
# Verify invalid parameters raise error
57-
with pytest.raises(ValueError, match="Unknown resource"):
58+
with pytest.raises(ResourceError, match="Unknown resource"):
5859
await mcp.read_resource("resource://users/123/posts") # Missing post_id
5960

60-
with pytest.raises(ValueError, match="Unknown resource"):
61+
with pytest.raises(ResourceError, match="Unknown resource"):
6162
await mcp.read_resource("resource://users/123/posts/456/extra") # Extra path component
6263

6364

tests/server/mcpserver/test_server.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ async def test_resource_decorator_with_metadata(self):
929929
mcp = MCPServer()
930930

931931
@mcp.resource("resource://config", meta={"ui": {"component": "file-viewer"}, "priority": "high"})
932-
def get_config() -> str: ...
932+
def get_config() -> str: ... # pragma: no branch
933933

934934
resources = await mcp.list_resources()
935935
assert resources == snapshot(
@@ -950,7 +950,7 @@ async def test_resource_template_decorator_with_metadata(self):
950950
mcp = MCPServer()
951951

952952
@mcp.resource("resource://{city}/weather", meta={"api_version": "v2", "deprecated": False})
953-
def get_weather(city: str) -> str: ...
953+
def get_weather(city: str) -> str: ... # pragma: no branch
954954

955955
templates = await mcp.list_resource_templates()
956956
assert templates == snapshot(
@@ -1277,15 +1277,14 @@ def test_prompt_decorator_error(self):
12771277
with pytest.raises(TypeError, match="decorator was used incorrectly"):
12781278

12791279
@mcp.prompt # type: ignore
1280-
def fn() -> str:
1281-
return "Hello, world!"
1280+
def fn() -> str: ... # pragma: no branch
12821281

12831282
async def test_list_prompts(self):
12841283
"""Test listing prompts through MCP protocol."""
12851284
mcp = MCPServer()
12861285

12871286
@mcp.prompt()
1288-
def fn(name: str, optional: str = "default") -> str: ...
1287+
def fn(name: str, optional: str = "default") -> str: ... # pragma: no branch
12891288

12901289
async with Client(mcp) as client:
12911290
result = await client.list_prompts()
@@ -1395,7 +1394,7 @@ async def test_get_prompt_missing_args(self):
13951394
mcp = MCPServer()
13961395

13971396
@mcp.prompt()
1398-
def prompt_fn(name: str) -> str: ...
1397+
def prompt_fn(name: str) -> str: ... # pragma: no branch
13991398

14001399
async with Client(mcp) as client:
14011400
with pytest.raises(MCPError, match="Missing required arguments"):

0 commit comments

Comments
 (0)