1010
1111from agents import (
1212 AgentOutputSchemaBase ,
13+ CodeInterpreterTool ,
1314 FileSearchTool ,
1415 FunctionTool ,
1516 Handoff ,
17+ HostedMCPTool ,
18+ ImageGenerationTool ,
1619 ModelProvider ,
1720 ModelResponse ,
1821 ModelSettings ,
2124 Tool ,
2225 TResponseInputItem ,
2326 UserError ,
24- WebSearchTool , ImageGenerationTool , CodeInterpreterTool ,
27+ WebSearchTool ,
2528)
2629from agents .models .multi_provider import MultiProvider
30+ from openai .types .responses .tool_param import Mcp
31+ from pydantic_core import to_json , to_jsonable_python
2732from typing_extensions import Required , TypedDict
2833
2934from temporalio import activity
@@ -51,7 +56,21 @@ class FunctionToolInput:
5156 strict_json_schema : bool = True
5257
5358
54- ToolInput = Union [FunctionToolInput , FileSearchTool , WebSearchTool , ImageGenerationTool , CodeInterpreterTool ]
59+ @dataclass
60+ class HostedMCPToolInput :
61+ """Data conversion friendly representation of a HostedMCPTool."""
62+
63+ tool_config : Mcp
64+
65+
66+ ToolInput = Union [
67+ FunctionToolInput ,
68+ FileSearchTool ,
69+ WebSearchTool ,
70+ ImageGenerationTool ,
71+ CodeInterpreterTool ,
72+ HostedMCPToolInput ,
73+ ]
5574
5675
5776@dataclass
@@ -137,22 +156,28 @@ async def empty_on_invoke_handoff(
137156 ) -> Any :
138157 return None
139158
140- # workaround for https://github.com/pydantic/pydantic/issues/9541
141- # ValidatorIterator returned
142- input_json = json .dumps (input ["input" ], default = str )
143- input_input = json .loads (input_json )
144-
145159 def make_tool (tool : ToolInput ) -> Tool :
146- if isinstance (tool , (FileSearchTool , WebSearchTool , ImageGenerationTool , CodeInterpreterTool )):
160+ if isinstance (
161+ tool ,
162+ (
163+ FileSearchTool ,
164+ WebSearchTool ,
165+ ImageGenerationTool ,
166+ CodeInterpreterTool ,
167+ ),
168+ ):
147169 return cast (Tool , tool )
170+ elif isinstance (tool , HostedMCPToolInput ):
171+ return HostedMCPTool (
172+ tool_config = tool .tool_config ,
173+ )
148174 elif isinstance (tool , FunctionToolInput ):
149- t = cast (FunctionToolInput , tool )
150175 return FunctionTool (
151- name = t .name ,
152- description = t .description ,
153- params_json_schema = t .params_json_schema ,
176+ name = tool .name ,
177+ description = tool .description ,
178+ params_json_schema = tool .params_json_schema ,
154179 on_invoke_tool = empty_on_invoke_tool ,
155- strict_json_schema = t .strict_json_schema ,
180+ strict_json_schema = tool .strict_json_schema ,
156181 )
157182 else :
158183 raise UserError (f"Unknown tool type: { tool .name } " )
@@ -171,7 +196,7 @@ def make_tool(tool: ToolInput) -> Tool:
171196 ]
172197 return await model .get_response (
173198 system_instructions = input .get ("system_instructions" ),
174- input = input_input ,
199+ input = input [ "input" ] ,
175200 model_settings = input ["model_settings" ],
176201 tools = tools ,
177202 output_schema = input .get ("output_schema" ),
0 commit comments