@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
84 lines • 3.85 kB
JavaScript
import { RunToolCallItem } from "../items.mjs";
import { getHostedMcpApprovalRequestIdentity, getHostedMcpApprovalRequestKey, } from "../toolIdentity.mjs";
import { getHostedMcpApprovalToolName } from "../toolInvocation.mjs";
/**
* Normalizes hosted MCP approval flows so streaming and non-streaming loops share identical
* behavior. Handles synchronous approvals, previously decided approvals, and pending approvals.
*/
export async function handleHostedMcpApprovals({ requests, agent, state, functionResults, appendIfNew, resolveApproval, resolveRejectionMessage, }) {
const pendingApprovals = new Set();
const pendingApprovalKeys = new Set();
for (const approvalRequest of requests) {
const rawItem = approvalRequest.requestItem.rawItem;
if (rawItem.type !== 'hosted_tool_call') {
continue;
}
const providerData = rawItem.providerData;
if (!providerData) {
continue;
}
const toolData = approvalRequest.mcpTool.providerData;
const approvalIdentity = getHostedMcpApprovalRequestIdentity(rawItem);
const approvalRequestId = approvalIdentity?.requestId;
const approvalDecision = typeof resolveApproval === 'function'
? resolveApproval(rawItem)
: undefined;
if (typeof approvalDecision !== 'undefined' && approvalRequestId) {
const rejectionReason = approvalDecision === false
? typeof resolveRejectionMessage === 'function'
? resolveRejectionMessage(rawItem)
: state._context._getToolInvocationRejectionMessage(agent, getHostedMcpApprovalToolName(rawItem.name, rawItem), rawItem)
: undefined;
const approvalResponseData = {
approve: approvalDecision,
approval_request_id: approvalRequestId,
reason: rejectionReason,
};
appendIfNew(new RunToolCallItem({
type: 'hosted_tool_call',
name: 'mcp_approval_response',
providerData: approvalResponseData,
...(rawItem.caller ? { caller: rawItem.caller } : {}),
}, agent));
continue;
}
if (toolData?.on_approval && approvalRequestId) {
const approvalResult = await toolData.on_approval(state._context, approvalRequest.requestItem);
if (approvalResult.approve) {
state.approve(approvalRequest.requestItem);
}
else {
state.reject(approvalRequest.requestItem, {
message: approvalResult.reason,
});
}
const approvalResponseData = {
approve: approvalResult.approve,
approval_request_id: approvalRequestId,
reason: approvalResult.reason,
};
appendIfNew(new RunToolCallItem({
type: 'hosted_tool_call',
name: 'mcp_approval_response',
providerData: approvalResponseData,
...(rawItem.caller ? { caller: rawItem.caller } : {}),
}, agent));
continue;
}
functionResults.push({
type: 'hosted_mcp_tool_approval',
tool: approvalRequest.mcpTool,
runItem: approvalRequest.requestItem,
});
appendIfNew(approvalRequest.requestItem);
pendingApprovals.add(approvalRequest.requestItem);
const approvalRequestKey = approvalIdentity
? getHostedMcpApprovalRequestKey(approvalIdentity)
: undefined;
if (approvalRequestKey) {
pendingApprovalKeys.add(approvalRequestKey);
}
}
return { pendingApprovals, pendingApprovalKeys };
}
//# sourceMappingURL=mcpApprovals.mjs.map