Skip to content

Commit 0e5ae00

Browse files
committed
fix: Tests seem to work with the MCP metadata but are these exhaustive?
1 parent 73ba0a1 commit 0e5ae00

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

pydantic_ai_slim/pydantic_ai/mcp.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,23 @@ async def direct_call_tool(
254254
):
255255
# The MCP SDK wraps primitives and generic types like list in a `result` key, but we want to use the raw value returned by the tool function.
256256
# See https://github.com/modelcontextprotocol/python-sdk#structured-output
257-
if isinstance(structured, dict) and len(structured) == 1 and 'result' in structured:
257+
if isinstance(structured, dict) and (
258+
(len(structured) == 1 and 'result' in structured)
259+
or (len(structured) == 2 and 'result' in structured and '_meta' in structured)
260+
):
258261
return (
259-
messages.ToolReturn(return_value=structured['result'], metadata=result.meta)
260-
if getattr(result, '_meta', None) is not None
262+
messages.ToolReturn(return_value=structured['result'], metadata=structured['_meta'])
263+
if structured.get('_meta', None) is not None
261264
else structured['result']
262265
)
263266
return (
264-
messages.ToolReturn(return_value=structured, metadata=result.meta)
265-
if getattr(result, '_meta', None) is not None
267+
messages.ToolReturn(return_value=structured, metadata=structured['_meta'])
268+
if structured.get('_meta', None) is not None
266269
else structured
267270
)
268271

269272
mapped = [await self._map_tool_result_part(part) for part in result.content]
270-
if getattr(result, '_meta', None) is not None:
273+
if result.meta:
271274
return (
272275
messages.ToolReturn(return_value=mapped[0], metadata=result.meta)
273276
if len(mapped) == 1

tests/mcp_server.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def get_image_resource_link() -> ResourceLink:
6969

7070

7171
@mcp.tool(annotations=ToolAnnotations(title='Collatz Conjecture sequence generator'))
72-
async def collatz_conjecture(n: int) -> TextContent:
72+
async def collatz_conjecture(n: int) -> dict[str, Any]:
7373
"""Generate the Collatz conjecture sequence for a given number.
7474
This tool attaches response metadata.
7575
@@ -88,11 +88,9 @@ async def collatz_conjecture(n: int) -> TextContent:
8888
else:
8989
n = 3 * n + 1
9090
sequence.append(n)
91-
response = TextContent(type='text', text=str(sequence))
91+
response: dict[str, Any] = {'result': str(sequence)}
9292
# attach metadata to the response
93-
if response.meta is None:
94-
response.meta = {}
95-
response.meta['pydantic_ai'] = {'tool': 'collatz_conjecture', 'length': len(sequence)}
93+
response['_meta'] = {'pydantic_ai': {'tool': 'collatz_conjecture', 'length': len(sequence)}}
9694
return response
9795

9896

tests/test_mcp.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ async def test_tool_response_metadata(run_context: RunContext[int]):
100100
# Test calling the Collatz conjecture generator tool
101101
result = await server.direct_call_tool('collatz_conjecture', {'n': 7})
102102
assert isinstance(result, ToolReturn)
103-
assert isinstance(result.return_value, str)
104103
assert result.return_value == '[7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]'
105104
assert result.metadata == {'pydantic_ai': {'tool': 'collatz_conjecture', 'length': 17}}
106105

0 commit comments

Comments
 (0)