Skip to content
Context docs
Build

Approve or deny tool calls

The scripts p3-tool-approvals.sh, p3-tool-approvals.py and p3-tool-approvals.ts are in full under Complete script.

Before you start

Set CONTEXT_BASE_URL, CONTEXT_API_KEY and AGENT_ID as in the Quickstart, with two changes:

  • The key needs tasks:approve, which is off by default and can be turned on only when the key is created.
  • The agent needs a tool set to Ask in the app, for example bash.
Client setup used by every step
#!/usr/bin/env bash set -euo pipefail : "${CONTEXT_BASE_URL:?Set CONTEXT_BASE_URL, for example https://api.context.ai}" : "${CONTEXT_API_KEY:?Set CONTEXT_API_KEY to a personal Task API key}" : "${AGENT_ID:?Set AGENT_ID to an agent with a tool set to Ask in the app}" api() { local path=$1; shift curl -sS --fail-with-body "$CONTEXT_BASE_URL/v1$path" -H "x-api-key: $CONTEXT_API_KEY" "$@" } fail() { echo "error: $*" >&2; exit 1; } # Opens the session's event stream; exec makes the background job's pid the curl process itself. stream() { exec curl -sS --fail-with-body -N "$CONTEXT_BASE_URL/v1/sessions/$1/events/stream" \ -H "x-api-key: $CONTEXT_API_KEY" "${@:2}" } # The JSON of every event received so far on a stream saved to a file. Ping frames carry no id and are skipped. events_in() { sed -n 's/^data: //p' "$1" | jq -c 'select(.id)'; } # Waits for the newest session.status_idle on the stream file and prints its stop_reason JSON. wait_for_idle() { local after=$1 file=$2 for _ in $(seq 1 300); do local idle grep -q '^event: error' "$file" && fail "the stream ended with an error: $(grep -A2 '^event: error' "$file" | sed -n 's/^data: //p')" idle=$(events_in "$file" | jq -sc --argjson after "$after" '[.[] | select(.type == "session.status_idle")] | .[$after:] | .[0].stop_reason // empty') [[ -n $idle ]] && { echo "$idle"; return 0; } sleep 1 done fail "no session.status_idle within 300 seconds" } WORK=$(mktemp -d)
  1. Create a session and open the stream

    Open the stream before the first message so the approval request is not missed.

    SESSION_ID=$(api /sessions -H "content-type: application/json" \ -d "$(jq -n --arg agent "$AGENT_ID" '{agent: $agent, environment_id: "env_default", title: "Approvals demo"}')" | jq -r .id) [[ -n $SESSION_ID && $SESSION_ID != null ]] || fail "no session id" stream "$SESSION_ID" > "$WORK/stream.txt" & STREAM_PID=$! sleep 1 kill -0 "$STREAM_PID" 2>/dev/null || fail "the stream did not open: $(cat "$WORK/stream.txt")" echo "step 1: session $SESSION_ID, stream open"

    Sample output:

    Code
    step 1: session dd13d728-93e3-440a-874c-4efeb58c97e0, stream open
  2. Ask for work that needs a tool

    When the agent wants a tool that needs approval, the session goes idle with stop_reason.type set to requires_action. stop_reason.event_ids lists the agent.tool_use ids waiting for you.

    api "/sessions/$SESSION_ID/events" -H "content-type: application/json" -d '{"events": [{"type": "user.message", "content": [{"type": "text", "text": "Run this bash command and tell me the output: echo approved"}]}]}' > /dev/null STOP=$(wait_for_idle 0 "$WORK/stream.txt") [[ $(jq -r .type <<<"$STOP") == "requires_action" ]] || fail "expected requires_action, got $STOP" echo "step 2: waiting for approval of $(jq -r '.event_ids | join(", ")' <<<"$STOP")"

    Sample output:

    Code
    step 2: waiting for approval of sevt_29_0
  3. Approve each waiting tool call

    Send one user.tool_confirmation per id, with that id as tool_use_id. Set result to allow to approve that one call, or to deny to refuse it. With deny, an optional deny_message is stored with the answer and echoed on the event.

    for TOOL_USE_ID in $(jq -r '.event_ids[]' <<<"$STOP"); do api "/sessions/$SESSION_ID/events" -H "content-type: application/json" -d "$(jq -n --arg id "$TOOL_USE_ID" \ '{events: [{type: "user.tool_confirmation", tool_use_id: $id, result: "allow"}]}')" > /dev/null # To refuse: result: "deny", with an optional deny_message that is stored with the answer and echoed on the event done echo "step 3: allowed $(jq '.event_ids | length' <<<"$STOP") tool call(s)"

    Sample output:

    Code
    step 3: allowed 1 tool call(s)
  4. Read to the end of the turn

    After the last confirmation, the agent runs the tool and continues until end_turn.

    STOP=$(wait_for_idle 1 "$WORK/stream.txt") kill "$STREAM_PID" [[ $(jq -r .type <<<"$STOP") == "end_turn" ]] || fail "expected end_turn, got $STOP" ANSWER=$(events_in "$WORK/stream.txt" | jq -r 'select(.type == "agent.message") | .content[] | select(.type == "text") | .text') echo "step 4: end_turn, agent said: $ANSWER"

    Sample output:

    Code
    step 4: end_turn, agent said: The command printed: approved
  5. Archive the session

    Archiving ends the session. Its status becomes terminated and its history stays readable.

    ARCHIVED=$(api "/sessions/$SESSION_ID/archive" -X POST) [[ $(jq -r .status <<<"$ARCHIVED") == "terminated" ]] || fail "expected terminated: $ARCHIVED" [[ $(jq -r .archived_at <<<"$ARCHIVED") != null ]] || fail "archived_at is not set: $ARCHIVED" echo "step 5: session terminated at $(jq -r .archived_at <<<"$ARCHIVED")"

    Sample output:

    Code
    step 5: session terminated at 2026-10-21T01:47:50.964Z

If something goes wrong

What you seeWhyWhat to do
The turn ends with end_turn and step 2 failsThe agent’s tools do not ask for permission, or the agent answered without a tool.In the app, set the agent’s bash tool to Ask, then run again.
403 permission_error on user.tool_confirmationThe key lacks tasks:approve, or it is a service account key. Only a personal key with tasks:approve, owned by the session’s owner, can answer.Have the session’s owner create a new personal key with tasks:approve. For a service account key, see the next row.
A session started with a service account key sits idle with requires_actionA service account key cannot answer a tool call, so a tool set to Ask is never approved and the session waits.In the app, set the tools the agent uses to Allow before you start sessions with that key. To approve tool calls yourself, use a personal key with tasks:approve.
400 invalid_request_error: tool_use_id ... has no confirmation waitingThe id was already answered, or it is not from the latest stop_reason.event_ids.Send exactly the ids from the latest requires_action, once each.
409 invalid_request_error on user.tool_confirmationAnother client answered the tool call first, or the session is stopping or moving to another computer.List the session’s events, find the current requires_action idle and answer its event_ids.
The session stays idle after approvingNot every id in event_ids was answered.Answer every id in stop_reason.event_ids. The agent resumes after the last one.

Complete script

#!/usr/bin/env bash set -euo pipefail : "${CONTEXT_BASE_URL:?Set CONTEXT_BASE_URL, for example https://api.context.ai}" : "${CONTEXT_API_KEY:?Set CONTEXT_API_KEY to a personal Task API key}" : "${AGENT_ID:?Set AGENT_ID to an agent with a tool set to Ask in the app}" api() { local path=$1; shift curl -sS --fail-with-body "$CONTEXT_BASE_URL/v1$path" -H "x-api-key: $CONTEXT_API_KEY" "$@" } fail() { echo "error: $*" >&2; exit 1; } # Opens the session's event stream; exec makes the background job's pid the curl process itself. stream() { exec curl -sS --fail-with-body -N "$CONTEXT_BASE_URL/v1/sessions/$1/events/stream" \ -H "x-api-key: $CONTEXT_API_KEY" "${@:2}" } # The JSON of every event received so far on a stream saved to a file. Ping frames carry no id and are skipped. events_in() { sed -n 's/^data: //p' "$1" | jq -c 'select(.id)'; } # Waits for the newest session.status_idle on the stream file and prints its stop_reason JSON. wait_for_idle() { local after=$1 file=$2 for _ in $(seq 1 300); do local idle grep -q '^event: error' "$file" && fail "the stream ended with an error: $(grep -A2 '^event: error' "$file" | sed -n 's/^data: //p')" idle=$(events_in "$file" | jq -sc --argjson after "$after" '[.[] | select(.type == "session.status_idle")] | .[$after:] | .[0].stop_reason // empty') [[ -n $idle ]] && { echo "$idle"; return 0; } sleep 1 done fail "no session.status_idle within 300 seconds" } WORK=$(mktemp -d) # @step 1 Create a session and open the stream SESSION_ID=$(api /sessions -H "content-type: application/json" \ -d "$(jq -n --arg agent "$AGENT_ID" '{agent: $agent, environment_id: "env_default", title: "Approvals demo"}')" | jq -r .id) [[ -n $SESSION_ID && $SESSION_ID != null ]] || fail "no session id" stream "$SESSION_ID" > "$WORK/stream.txt" & STREAM_PID=$! sleep 1 kill -0 "$STREAM_PID" 2>/dev/null || fail "the stream did not open: $(cat "$WORK/stream.txt")" echo "step 1: session $SESSION_ID, stream open" # @step 2 Ask for work that needs a tool api "/sessions/$SESSION_ID/events" -H "content-type: application/json" -d '{"events": [{"type": "user.message", "content": [{"type": "text", "text": "Run this bash command and tell me the output: echo approved"}]}]}' > /dev/null STOP=$(wait_for_idle 0 "$WORK/stream.txt") [[ $(jq -r .type <<<"$STOP") == "requires_action" ]] || fail "expected requires_action, got $STOP" echo "step 2: waiting for approval of $(jq -r '.event_ids | join(", ")' <<<"$STOP")" # @step 3 Approve each waiting tool call for TOOL_USE_ID in $(jq -r '.event_ids[]' <<<"$STOP"); do api "/sessions/$SESSION_ID/events" -H "content-type: application/json" -d "$(jq -n --arg id "$TOOL_USE_ID" \ '{events: [{type: "user.tool_confirmation", tool_use_id: $id, result: "allow"}]}')" > /dev/null # To refuse: result: "deny", with an optional deny_message that is stored with the answer and echoed on the event done echo "step 3: allowed $(jq '.event_ids | length' <<<"$STOP") tool call(s)" # @step 4 Read to the end of the turn STOP=$(wait_for_idle 1 "$WORK/stream.txt") kill "$STREAM_PID" [[ $(jq -r .type <<<"$STOP") == "end_turn" ]] || fail "expected end_turn, got $STOP" ANSWER=$(events_in "$WORK/stream.txt" | jq -r 'select(.type == "agent.message") | .content[] | select(.type == "text") | .text') echo "step 4: end_turn, agent said: $ANSWER" # @step 5 Archive the session ARCHIVED=$(api "/sessions/$SESSION_ID/archive" -X POST) [[ $(jq -r .status <<<"$ARCHIVED") == "terminated" ]] || fail "expected terminated: $ARCHIVED" [[ $(jq -r .archived_at <<<"$ARCHIVED") != null ]] || fail "archived_at is not set: $ARCHIVED" echo "step 5: session terminated at $(jq -r .archived_at <<<"$ARCHIVED")" # @end