@@ -195,38 +195,42 @@ In this task, you'll connect to a remote MCP server, prepare the AI agent, and r
195195
196196 ```python
197197 # Process any MCP approval requests that were generated
198- input_list: ResponseInputParam = []
199- for item in response.output:
200- if item.type == "mcp_approval_request":
201- if item.server_label == "api-specs" and item.id:
202- # Automatically approve the MCP request to allow the agent to proceed
203- input_list.append(
204- McpApprovalResponse(
205- type="mcp_approval_response",
206- approve=True,
207- approval_request_id=item.id,
198+ # The agent may issue several tool calls, each needing its own approval,
199+ # so we loop until there are none left.
200+ while True:
201+ # Collect any MCP approval requests from the latest response
202+ input_list: ResponseInputParam = []
203+ for item in response.output:
204+ if item.type == "mcp_approval_request":
205+ if item.server_label == "api-specs" and item.id:
206+ # Automatically approve the MCP request to allow the agent to proceed
207+ input_list.append(
208+ McpApprovalResponse(
209+ type="mcp_approval_response",
210+ approve=True,
211+ approval_request_id=item.id,
212+ )
208213 )
209- )
210214
211- print("Final input:")
212- print( input_list)
213- ```
215+ # No more approvals needed -> the agent has produced its final response
216+ if not input_list:
217+ break
214218
215- This code listens for any MCP approval requests in the agent's response and automatically approves them.
219+ print("Final input:")
220+ print(input_list)
216221
217- 1. Find the comment **Send the approval response back and retrieve a response** and add the following code:
218-
219- ```python
220- # Send the approval response back and retrieve a response
221- response = openai_client.responses.create(
222- input=input_list,
223- previous_response_id=response.id,
224- extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
225- )
222+ # Send the approval response back and retrieve the next response
223+ response = openai_client.responses.create(
224+ input=input_list,
225+ previous_response_id=response.id,
226+ extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
227+ )
226228
227229 print(f"\nAgent response: {response.output_text}")
228230 ```
229231
232+ This code listens for any MCP approval requests in the agent's response and automatically approves them.
233+
2302341. Find the comment **Clean up resources by deleting the agent version** and add the following code:
231235
232236 ```python
0 commit comments