@@ -347,7 +347,7 @@ def capabilities(self) -> ServerCapabilities:
347347 f'The `{ self .__class__ .__name__ } .capabilities` is only instantiated after initialization.'
348348 )
349349 return self ._server_capabilities
350-
350+
351351 @property
352352 def instructions (self ) -> str | None :
353353 """Access the instructions sent by the MCP server during initialization."""
@@ -504,18 +504,16 @@ async def list_resource_templates(self) -> list[ResourceTemplate]:
504504 return [_mcp .map_from_mcp_resource_template (t ) for t in result .resourceTemplates ]
505505
506506 @overload
507- async def read_resource (
508- self , uri : str
509- ) -> str | messages .BinaryContent | list [str | messages .BinaryContent ] | None : ...
507+ async def read_resource (self , uri : str ) -> str | messages .BinaryContent | list [str | messages .BinaryContent ]: ...
510508
511509 @overload
512510 async def read_resource (
513511 self , uri : Resource
514- ) -> str | messages .BinaryContent | list [str | messages .BinaryContent ] | None : ...
512+ ) -> str | messages .BinaryContent | list [str | messages .BinaryContent ]: ...
515513
516514 async def read_resource (
517515 self , uri : str | Resource
518- ) -> str | messages .BinaryContent | list [str | messages .BinaryContent ] | None :
516+ ) -> str | messages .BinaryContent | list [str | messages .BinaryContent ]:
519517 """Read the contents of a specific resource by URI.
520518
521519 Args:
@@ -524,26 +522,17 @@ async def read_resource(
524522 Returns:
525523 The resource contents. If the resource has a single content item, returns that item directly.
526524 If the resource has multiple content items, returns a list of items.
527- Returns `None` if the server does not support resources or the resource is not found.
528525
529526 Raises:
530- MCPError: If the server returns an error other than resource not found .
527+ MCPError: If the server returns an error.
531528 """
532529 resource_uri = uri if isinstance (uri , str ) else uri .uri
533530 async with self : # Ensure server is running
534- if not self .capabilities .resources :
535- return None
536531 try :
537532 result = await self ._client .read_resource (AnyUrl (resource_uri ))
538533 except mcp_exceptions .McpError as e :
539- # As per https://modelcontextprotocol.io/specification/2025-06-18/server/resources#error-handling
540- if e .error .code == - 32002 :
541- return None
542534 raise MCPError .from_mcp_sdk_error (e ) from e
543535
544- if not result .contents :
545- return None
546-
547536 return (
548537 self ._get_content (result .contents [0 ])
549538 if len (result .contents ) == 1
@@ -646,12 +635,8 @@ async def _map_tool_result_part(
646635 resource = part .resource
647636 return self ._get_content (resource )
648637 elif isinstance (part , mcp_types .ResourceLink ):
649- result = await self .read_resource (str (part .uri ))
650- # Rather than hide an invalid resource link, we raise an error so it's consistent with any
651- # other error that could happen during resource reading.
652- if result is None :
653- raise MCPError (message = f'Invalid ResourceLink { part .uri } returned by tool' , code = - 32002 )
654- return result
638+ # read_resource will raise MCPError if the resource is not found or has any other error
639+ return await self .read_resource (str (part .uri ))
655640 else :
656641 assert_never (part )
657642
0 commit comments