Skip to content

Commit 386ed28

Browse files
committed
Update code sample for always require approval
1 parent 5e835d1 commit 386ed28

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

docs/guides/tools-connectors-mcp/responses/approvals-always-require-approval.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,26 @@
1111
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
1212
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
1313

14-
ResponseCreationOptions options1 = new();
15-
options1.Tools.Add(ResponseTool.CreateMcpTool(
16-
serverLabel: "dmcp",
17-
serverUri: new Uri("https://dmcp-server.deno.dev/sse")
18-
));
19-
20-
McpToolCallApprovalRequestItem request1 = ResponseItem.CreateMcpApprovalRequestItem(
21-
serverLabel: "dmcp",
22-
name: "roll",
23-
arguments: BinaryData.FromObjectAsJson(new { diceRollExpression = "2d4+1" })
24-
);
25-
26-
OpenAIResponse response1 = (OpenAIResponse)client.CreateResponse([
27-
request1
28-
], options1);
29-
30-
ResponseCreationOptions options2 = new();
31-
options2.Tools.Add(ResponseTool.CreateMcpTool(
14+
ResponseCreationOptions options = new();
15+
options.Tools.Add(ResponseTool.CreateMcpTool(
3216
serverLabel: "dmcp",
3317
serverUri: new Uri("https://dmcp-server.deno.dev/sse"),
3418
toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.AlwaysRequireApproval)
3519
));
3620

37-
options2.PreviousResponseId = response1.Id;
21+
// STEP 1: Create response that requests tool call approval
22+
OpenAIResponse response1 = (OpenAIResponse)client.CreateResponse([
23+
ResponseItem.CreateUserMessageItem([
24+
ResponseContentPart.CreateInputTextPart("Roll 2d4+1")
25+
])
26+
], options);
27+
28+
McpToolCallApprovalRequestItem? approvalRequestItem = response1.OutputItems.Last() as McpToolCallApprovalRequestItem;
3829

30+
// STEP 2: Approve the tool call request and get final response
31+
options.PreviousResponseId = response1.Id;
3932
OpenAIResponse response2 = (OpenAIResponse)client.CreateResponse([
40-
request1,
41-
ResponseItem.CreateMcpApprovalResponseItem(request1.Id, approved: true),
42-
], options2);
33+
ResponseItem.CreateMcpApprovalResponseItem(approvalRequestItem!.Id, approved: true),
34+
], options);
4335

4436
Console.WriteLine(response2.GetOutputText());

0 commit comments

Comments
 (0)