Skip to content

Commit 3866935

Browse files
Handle MCP naming, image options mapping, and connector tool content
Co-authored-by: eavanvalkenburg <[email protected]>
1 parent 1625f15 commit 3866935

6 files changed

Lines changed: 180 additions & 32 deletions

File tree

‎python/packages/anthropic/agent_framework_anthropic/_chat_client.py‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
CitationAnnotation,
1616
Contents,
1717
FinishReason,
18+
CodeInterpreterToolCallContent,
19+
CodeInterpreterToolResultContent,
1820
FunctionCallContent,
1921
FunctionResultContent,
2022
HostedCodeInterpreterTool,
2123
HostedFileContent,
2224
HostedMCPTool,
2325
HostedWebSearchTool,
26+
MCPServerToolCallContent,
27+
MCPServerToolResultContent,
2428
Role,
2529
TextContent,
2630
TextReasoningContent,
@@ -588,21 +592,39 @@ def _parse_contents_from_anthropic(
588592
)
589593
case "tool_use" | "mcp_tool_use" | "server_tool_use":
590594
self._last_call_id_name = (content_block.id, content_block.name)
591-
contents.append(
592-
FunctionCallContent(
593-
call_id=content_block.id,
594-
name=content_block.name,
595-
arguments=content_block.input,
596-
raw_representation=content_block,
595+
if content_block.type == "mcp_tool_use":
596+
contents.append(
597+
MCPServerToolCallContent(
598+
call_id=content_block.id,
599+
tool_name=content_block.name,
600+
server_name=None,
601+
arguments=content_block.input,
602+
raw_representation=content_block,
603+
)
604+
)
605+
elif "code_execution" in (content_block.name or "") or content_block.type == "tool_use":
606+
contents.append(
607+
CodeInterpreterToolCallContent(
608+
call_id=content_block.id,
609+
inputs=[TextContent(text=str(content_block.input), raw_representation=content_block)],
610+
raw_representation=content_block,
611+
)
612+
)
613+
else:
614+
contents.append(
615+
FunctionCallContent(
616+
call_id=content_block.id,
617+
name=content_block.name,
618+
arguments=content_block.input,
619+
raw_representation=content_block,
620+
)
597621
)
598-
)
599622
case "mcp_tool_result":
600623
call_id, name = self._last_call_id_name or (None, None)
601624
contents.append(
602-
FunctionResultContent(
625+
MCPServerToolResultContent(
603626
call_id=content_block.tool_use_id,
604-
name=name if name and call_id == content_block.tool_use_id else "mcp_tool",
605-
result=self._parse_contents_from_anthropic(content_block.content)
627+
output=self._parse_contents_from_anthropic(content_block.content)
606628
if isinstance(content_block.content, list)
607629
else content_block.content,
608630
raw_representation=content_block,
@@ -638,10 +660,9 @@ def _parse_contents_from_anthropic(
638660
HostedFileContent(file_id=result_content.file_id, raw_representation=result_content)
639661
)
640662
contents.append(
641-
FunctionResultContent(
663+
CodeInterpreterToolResultContent(
642664
call_id=content_block.tool_use_id,
643-
name=name if name and call_id == content_block.tool_use_id else "code_execution_tool",
644-
result=content_block.content,
665+
outputs=None,
645666
raw_representation=content_block,
646667
)
647668
)

‎python/packages/core/agent_framework/_tools.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Final,
1616
Generic,
1717
Literal,
18+
TypedDict,
1819
Protocol,
1920
TypeVar,
2021
cast,
@@ -325,13 +326,24 @@ def __init__(
325326
super().__init__(**args)
326327

327328

329+
class HostedImageGenerationToolOptions(TypedDict, total=False):
330+
"""Options for HostedImageGenerationTool."""
331+
332+
count: int
333+
image_size: str
334+
media_type: str
335+
model_id: str
336+
response_format: Literal["uri", "data", "hosted"]
337+
streaming_count: int
338+
339+
328340
class HostedImageGenerationTool(BaseTool):
329341
"""Represents a hosted tool that can be specified to an AI service to enable it to perform image generation."""
330342

331343
def __init__(
332344
self,
333345
*,
334-
options: dict[str, Any] | None = None,
346+
options: HostedImageGenerationToolOptions | None = None,
335347
description: str | None = None,
336348
additional_properties: dict[str, Any] | None = None,
337349
**kwargs: Any,

‎python/packages/core/agent_framework/_types.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
"ImageGenerationToolCallContent",
5454
"ImageGenerationToolResultContent",
5555
"HostedFileContent",
56-
"McpServerToolCallContent",
57-
"McpServerToolResultContent",
56+
"MCPServerToolCallContent",
57+
"MCPServerToolResultContent",
5858
"HostedVectorStoreContent",
5959
"Role",
6060
"TextContent",
@@ -136,9 +136,9 @@ def _parse_content(content_data: MutableMapping[str, Any]) -> "Contents":
136136
case "image_generation_tool_result":
137137
return ImageGenerationToolResultContent.from_dict(content_data)
138138
case "mcp_server_tool_call":
139-
return McpServerToolCallContent.from_dict(content_data)
139+
return MCPServerToolCallContent.from_dict(content_data)
140140
case "mcp_server_tool_result":
141-
return McpServerToolResultContent.from_dict(content_data)
141+
return MCPServerToolResultContent.from_dict(content_data)
142142
case "function_approval_request":
143143
return FunctionApprovalRequestContent.from_dict(content_data)
144144
case "function_approval_response":
@@ -1794,7 +1794,7 @@ def __init__(
17941794
self.type: Literal["image_generation_tool_result"] = "image_generation_tool_result"
17951795

17961796

1797-
class McpServerToolCallContent(BaseContent):
1797+
class MCPServerToolCallContent(BaseContent):
17981798
"""Represents a tool call request to a MCP server."""
17991799

18001800
def __init__(
@@ -1826,7 +1826,7 @@ def __init__(
18261826
self.type: Literal["mcp_server_tool_call"] = "mcp_server_tool_call"
18271827

18281828

1829-
class McpServerToolResultContent(BaseContent):
1829+
class MCPServerToolResultContent(BaseContent):
18301830
"""Represents the result of a MCP server tool call."""
18311831

18321832
def __init__(
@@ -2042,8 +2042,8 @@ def create_response(self, approved: bool) -> "FunctionApprovalResponseContent":
20422042
| CodeInterpreterToolResultContent
20432043
| ImageGenerationToolCallContent
20442044
| ImageGenerationToolResultContent
2045-
| McpServerToolCallContent
2046-
| McpServerToolResultContent
2045+
| MCPServerToolCallContent
2046+
| MCPServerToolResultContent
20472047
| FunctionApprovalRequestContent
20482048
| FunctionApprovalResponseContent
20492049
)

‎python/packages/core/agent_framework/openai/_assistants_client.py‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
ChatResponse,
3030
ChatResponseUpdate,
3131
Contents,
32+
CodeInterpreterToolCallContent,
33+
MCPServerToolCallContent,
3234
FunctionCallContent,
3335
FunctionResultContent,
3436
Role,
@@ -378,9 +380,35 @@ def _parse_function_calls_from_assistants(self, event_data: Run, response_id: st
378380
if event_data.required_action is not None:
379381
for tool_call in event_data.required_action.submit_tool_outputs.tool_calls:
380382
call_id = json.dumps([response_id, tool_call.id])
381-
function_name = tool_call.function.name
382-
function_arguments = json.loads(tool_call.function.arguments)
383-
contents.append(FunctionCallContent(call_id=call_id, name=function_name, arguments=function_arguments))
383+
tool_type = getattr(tool_call, "type", None)
384+
if tool_type == "code_interpreter" and getattr(tool_call, "code_interpreter", None):
385+
code_input = getattr(tool_call.code_interpreter, "input", None)
386+
inputs = (
387+
[TextContent(text=code_input, raw_representation=tool_call)]
388+
if code_input is not None
389+
else None
390+
)
391+
contents.append(
392+
CodeInterpreterToolCallContent(
393+
call_id=call_id,
394+
inputs=inputs,
395+
raw_representation=tool_call,
396+
)
397+
)
398+
elif tool_type == "mcp":
399+
contents.append(
400+
MCPServerToolCallContent(
401+
call_id=call_id,
402+
tool_name=getattr(tool_call, "name", "") or "",
403+
server_name=getattr(tool_call, "server_label", None),
404+
arguments=getattr(tool_call, "args", None),
405+
raw_representation=tool_call,
406+
)
407+
)
408+
else:
409+
function_name = tool_call.function.name
410+
function_arguments = json.loads(tool_call.function.arguments)
411+
contents.append(FunctionCallContent(call_id=call_id, name=function_name, arguments=function_arguments))
384412

385413
return contents
386414

‎python/packages/core/agent_framework/openai/_responses_client.py‎

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
HostedVectorStoreContent,
5656
ImageGenerationToolCallContent,
5757
ImageGenerationToolResultContent,
58+
MCPServerToolCallContent,
59+
MCPServerToolResultContent,
5860
Role,
5961
TextContent,
6062
TextReasoningContent,
@@ -322,7 +324,17 @@ def _prepare_tools_for_openai(
322324
case HostedImageGenerationTool():
323325
mapped_tool: dict[str, Any] = {"type": "image_generation"}
324326
if tool.options:
325-
mapped_tool.update(tool.options)
327+
option_mapping = {
328+
"count": "n",
329+
"image_size": "size",
330+
"media_type": "media_type",
331+
"model_id": "model",
332+
"response_format": "response_format",
333+
"streaming_count": "partial_images",
334+
}
335+
for key, value in tool.options.items():
336+
mapped_key = option_mapping.get(key, key)
337+
mapped_tool[mapped_key] = value
326338
if tool.additional_properties:
327339
mapped_tool.update(tool.additional_properties)
328340
response_tools.append(mapped_tool)
@@ -334,7 +346,19 @@ def _prepare_tools_for_openai(
334346

335347
# Special handling for image_generation tools
336348
if tool_dict.get("type") == "image_generation":
337-
response_tools.append(tool_dict.copy())
349+
mapped_tool = tool_dict.copy()
350+
option_mapping = {
351+
"count": "n",
352+
"image_size": "size",
353+
"media_type": "media_type",
354+
"model_id": "model",
355+
"response_format": "response_format",
356+
"streaming_count": "partial_images",
357+
}
358+
for key, value in list(mapped_tool.items()):
359+
if key in option_mapping:
360+
mapped_tool[option_mapping[key]] = mapped_tool.pop(key)
361+
response_tools.append(mapped_tool)
338362
else:
339363
response_tools.append(tool_dict)
340364
return response_tools
@@ -802,6 +826,37 @@ def _parse_response_from_openai(
802826
),
803827
)
804828
)
829+
case "mcp_call":
830+
call_id = getattr(item, "id", None) or getattr(item, "call_id", None) or ""
831+
contents.append(
832+
MCPServerToolCallContent(
833+
call_id=call_id,
834+
tool_name=getattr(item, "name", "") or "",
835+
server_name=getattr(item, "server_label", None),
836+
arguments=getattr(item, "arguments", None),
837+
raw_representation=item,
838+
)
839+
)
840+
result_output = (
841+
getattr(item, "result", None)
842+
or getattr(item, "output", None)
843+
or getattr(item, "outputs", None)
844+
)
845+
parsed_output: list[Contents] | None = None
846+
if result_output:
847+
normalized = (
848+
result_output
849+
if isinstance(result_output, Sequence) and not isinstance(result_output, (str, bytes, MutableMapping))
850+
else [result_output]
851+
)
852+
parsed_output = [_parse_content(output_item) for output_item in normalized]
853+
contents.append(
854+
MCPServerToolResultContent(
855+
call_id=call_id,
856+
output=parsed_output,
857+
raw_representation=item,
858+
)
859+
)
805860
case "image_generation_call": # ResponseOutputImageGenerationCall
806861
outputs: list["Contents"] = []
807862
if item.result:
@@ -992,6 +1047,38 @@ def _parse_chunk_from_openai(
9921047
),
9931048
)
9941049
)
1050+
case "mcp_call":
1051+
call_id = getattr(event_item, "id", None) or getattr(event_item, "call_id", None) or ""
1052+
contents.append(
1053+
MCPServerToolCallContent(
1054+
call_id=call_id,
1055+
tool_name=getattr(event_item, "name", "") or "",
1056+
server_name=getattr(event_item, "server_label", None),
1057+
arguments=getattr(event_item, "arguments", None),
1058+
raw_representation=event_item,
1059+
)
1060+
)
1061+
result_output = (
1062+
getattr(event_item, "result", None)
1063+
or getattr(event_item, "output", None)
1064+
or getattr(event_item, "outputs", None)
1065+
)
1066+
parsed_output: list[Contents] | None = None
1067+
if result_output:
1068+
normalized = (
1069+
result_output
1070+
if isinstance(result_output, Sequence)
1071+
and not isinstance(result_output, (str, bytes, MutableMapping))
1072+
else [result_output]
1073+
)
1074+
parsed_output = [_parse_content(output_item) for output_item in normalized]
1075+
contents.append(
1076+
MCPServerToolResultContent(
1077+
call_id=call_id,
1078+
output=parsed_output,
1079+
raw_representation=event_item,
1080+
)
1081+
)
9951082
case "code_interpreter_call": # ResponseOutputCodeInterpreterCall
9961083
call_id = getattr(event_item, "call_id", None) or getattr(event_item, "id", None)
9971084
outputs: list[Contents] = []

‎python/packages/core/tests/core/test_types.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
ImageGenerationToolResultContent,
3232
HostedFileContent,
3333
HostedVectorStoreContent,
34-
McpServerToolCallContent,
35-
McpServerToolResultContent,
34+
MCPServerToolCallContent,
35+
MCPServerToolResultContent,
3636
Role,
3737
TextContent,
3838
TextReasoningContent,
@@ -335,16 +335,16 @@ def test_image_generation_tool_contents():
335335

336336

337337
def test_mcp_server_tool_call_and_result():
338-
call = McpServerToolCallContent(call_id="c-1", tool_name="tool", server_name="server", arguments={"x": 1})
338+
call = MCPServerToolCallContent(call_id="c-1", tool_name="tool", server_name="server", arguments={"x": 1})
339339
assert call.type == "mcp_server_tool_call"
340340
assert call.arguments == {"x": 1}
341341

342-
result = McpServerToolResultContent(call_id="c-1", output=[{"type": "text", "text": "done"}])
342+
result = MCPServerToolResultContent(call_id="c-1", output=[{"type": "text", "text": "done"}])
343343
assert result.type == "mcp_server_tool_result"
344344
assert result.output and isinstance(result.output[0], TextContent)
345345

346346
with raises(ValueError):
347-
McpServerToolCallContent(call_id="", tool_name="tool")
347+
MCPServerToolCallContent(call_id="", tool_name="tool")
348348

349349

350350
# region: HostedVectorStoreContent

0 commit comments

Comments
 (0)