Skip to content

Commit c155d98

Browse files
authored
Lab 3 MCP (#202)
* Implement MCP approval request handling Added code to process MCP approval requests and send responses. * Clean up commented code in agent.py Removed commented-out code related to sending approval responses.
1 parent 906eb87 commit c155d98

2 files changed

Lines changed: 29 additions & 29 deletions

File tree

Instructions/Exercises/03-mcp-integration.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
230234
1. Find the comment **Clean up resources by deleting the agent version** and add the following code:
231235
232236
```python

Labfiles/03-mcp-integration/Python/agent.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525

2626

2727
# Process any MCP approval requests that were generated
28-
29-
30-
# Send the approval response back and retrieve a response
31-
3228

3329
# Clean up resources by deleting the agent version
34-
30+

0 commit comments

Comments
 (0)