Skip to content

Commit b8424d8

Browse files
committed
Allow MCPServer.read_resource() to read resources by Resource.
1 parent e6cb086 commit b8424d8

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

pydantic_ai_slim/pydantic_ai/mcp.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from dataclasses import field, replace
1111
from datetime import timedelta
1212
from pathlib import Path
13-
from typing import Annotated, Any
13+
from typing import Annotated, Any, overload
1414

1515
import anyio
1616
import httpx
@@ -320,18 +320,29 @@ async def list_resource_templates(self) -> list[_mcp.ResourceTemplate]:
320320
result = await self._client.list_resource_templates()
321321
return [_mcp.map_from_mcp_resource_template(t) for t in result.resourceTemplates]
322322

323-
async def read_resource(self, uri: str) -> str | messages.BinaryContent | list[str | messages.BinaryContent]:
323+
@overload
324+
async def read_resource(self, uri: str) -> str | messages.BinaryContent | list[str | messages.BinaryContent]: ...
325+
326+
@overload
327+
async def read_resource(
328+
self, uri: _mcp.Resource
329+
) -> str | messages.BinaryContent | list[str | messages.BinaryContent]: ...
330+
331+
async def read_resource(
332+
self, uri: str | _mcp.Resource
333+
) -> str | messages.BinaryContent | list[str | messages.BinaryContent]:
324334
"""Read the contents of a specific resource by URI.
325335
326336
Args:
327-
uri: The URI of the resource to read.
337+
uri: The URI of the resource to read, or a Resource object.
328338
329339
Returns:
330340
The resource contents. If the resource has a single content item, returns that item directly.
331341
If the resource has multiple content items, returns a list of items.
332342
"""
343+
resource_uri = uri if isinstance(uri, str) else uri.uri
333344
async with self: # Ensure server is running
334-
result = await self._client.read_resource(AnyUrl(uri))
345+
result = await self._client.read_resource(AnyUrl(resource_uri))
335346
return (
336347
self._get_content(result.contents[0])
337348
if len(result.contents) == 1

tests/test_mcp.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
ToolReturnPart,
2424
UserPromptPart,
2525
)
26+
from pydantic_ai._mcp import Resource
2627
from pydantic_ai.agent import Agent
2728
from pydantic_ai.exceptions import ModelRetry, UnexpectedModelBehavior, UserError
2829
from pydantic_ai.mcp import MCPServerStreamableHTTP, load_mcp_servers
@@ -1500,10 +1501,17 @@ async def test_read_text_resource(run_context: RunContext[int]):
15001501
"""Test reading a text resource (converted to string)."""
15011502
server = MCPServerStdio('python', ['-m', 'tests.mcp_server'])
15021503
async with server:
1504+
# Test reading by URI string
15031505
content = await server.read_resource('resource://product_name.txt')
15041506
assert isinstance(content, str)
15051507
assert content == snapshot('Pydantic AI\n')
15061508

1509+
# Test reading by Resource object
1510+
resource = Resource(uri='resource://product_name.txt', name='product_name_resource')
1511+
content_from_resource = await server.read_resource(resource)
1512+
assert isinstance(content_from_resource, str)
1513+
assert content_from_resource == snapshot('Pydantic AI\n')
1514+
15071515

15081516
async def test_read_blob_resource(run_context: RunContext[int]):
15091517
"""Test reading a binary resource (converted to BinaryContent)."""

0 commit comments

Comments
 (0)