Skip to content

Tool calls and approvals in chat

The gateway never runs tools on the model’s behalf in a chat. Your client does: the model answers with tool_calls, your client executes each one against the MCP server through the Core Proxy, appends the results, and asks the model again. Because every tool call goes through /v1/proxy/mcp/{server_id} with the user’s token, the gateway applies the workspace’s capability policy, argument policies, guardrails and approval rules to it, and records it in the same run as the LLM calls.

Take each entry of GET /v1/portal/servers?group_id=... (catalog) and connect to its mcp_server.base_url with MCP Streamable HTTP (any MCP client SDK works; the shipped portal uses @modelcontextprotocol/sdk). Send the portal headers on every request:

Terminal window
MCP=http://localhost:8100/v1/proxy/mcp/mcp-01M61N37VYTSY0ETNQWW0DMRFY
H=(-H "Content-Type: application/json" -H "Accept: application/json, text/event-stream"
-H "X-Tenant-ID: default" -H "X-Gateway-Authorization: Bearer $JWT"
-H "X-Resource-Group: group-01M61N376V2RR6J2TV7Q7EVRET")
curl -s "$MCP" "${H[@]}" -d '{"jsonrpc": "2.0", "id": 1, "method": "initialize",
"params": {"protocolVersion": "2025-06-18", "capabilities": {},
"clientInfo": {"name": "acme-chat", "version": "1.4.0"}}}'
curl -s "$MCP" "${H[@]}" -d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}'
{"jsonrpc": "2.0", "id": 1, "result": {"protocolVersion": "2025-06-18", "capabilities": {"tools": {"listChanged": false}, "prompts": {"listChanged": false}, "resources": {"subscribe": false, "listChanged": false}}, "serverInfo": {"name": "brutor-demo-applications-mcp", "version": "1.30.0"}, "instructions": "Mock loan origination system ..."}}
{"jsonrpc": "2.0", "id": 2, "result": {"tools": [{"name": "applications_list_pending", "description": "List applications that have not been screened yet ...", "inputSchema": {"type": "object", "properties": {"limit": {"type": "integer", "default": 10}}}, "annotations": {"readOnlyHint": true, "idempotentHint": true}}]}}

If the server returns an Mcp-Session-Id header on initialize, echo it on later calls, as the MCP spec requires. tools/list answers only with the tools the workspace’s capability policy allows; tools that need approval are listed and marked approval_required in the catalog response.

  1. Offer the tools to the model in OpenAI form, one entry per MCP tool:

    {
    "type": "function",
    "function": {
    "name": "applications_list_pending",
    "description": "List applications that have not been screened yet ...",
    "parameters": {"type": "object", "properties": {"limit": {"type": "integer", "default": 10}}}
    }
    }

    Tool names are unique within a server but not across servers. Keep a map from the name you gave the model to (server, tool) so a call routes to the server that owns it; if two servers share a tool name, prefix one.

  2. Stream the answer. When the model wants tools, the stream carries delta.tool_calls fragments (an id, the function name, and arguments in pieces) and ends with finish_reason: "tool_calls". Concatenate the argument fragments per index, then parse the JSON.

  3. Execute each call with tools/call on the owning server, asserting the turn’s X-Brutor-Run-Id so the call joins the turn’s run:

    Terminal window
    curl -s "$MCP" "${H[@]}" -H "X-Brutor-Run-Id: 3f2b8c1e-6d4a-4b9e-9c1f-2a7d5e8b0c34" \
    -d '{"jsonrpc": "2.0", "id": 3, "method": "tools/call",
    "params": {"name": "applications_list_pending", "arguments": {"limit": 5}}}'
    {"jsonrpc": "2.0", "id": 3, "result": {"content": [{"type": "text", "text": "[{\"application_id\": \"APP-20261027-0481\", \"received_at\": \"2026-10-27T09:12:00Z\", \"amount_eur\": 21300, \"purpose_short\": \"car\"}]"}], "isError": false}}
  4. Send the results back and stream again: append the assistant message with its tool_calls, then one role: "tool" message per call, and post the whole conversation to /v1/proxy/llm/chat/completions with the same X-Brutor-Run-Id.

    [
    {"role": "user", "content": "Which applications are waiting?"},
    {"role": "assistant", "content": null, "tool_calls": [{"id": "call_7Qx", "type": "function", "function": {"name": "applications_list_pending", "arguments": "{\"limit\": 5}"}}]},
    {"role": "tool", "tool_call_id": "call_7Qx", "content": "[{\"application_id\": \"APP-20261027-0481\", ...}]"}
    ]
  5. Repeat until the model finishes with finish_reason: "stop". Cap the number of rounds in your client. A failing tool should become a role: "tool" message carrying the error text, so the model can explain it, instead of aborting the turn.

The shipped portal implements this loop with the Vercel AI SDK (sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls in src/hooks/useBrutorChat.ts, the MCP bridge in src/lib/mcpToolAdapter.ts).

Response Meaning Your client
JSON-RPC error.code: -32000 with error.data.code: "guardrail_blocked" A guardrail blocked the arguments (direction: "input") or the result ("output") Return error.message to the model as the tool result
HTTP 403 with a JSON-RPC body, error.code: -32000 and error.data.argument_policy (policy, tool, argument, rule) An argument policy denied these arguments Return error.message to the model; it may retry with other arguments
HTTP 403 {"error": "Access denied: tool 'x' is disabled for your group"} The tool is disabled for this workspace (it was not in tools/list either) Drop the tool from what you offer the model
HTTP 428 The call is held for a human The approval flow below
HTTP 401 {"error": "oauth_reauth_required", ...} The user’s token for this server expired or was revoked Re-run the OAuth flow for that server

A tool the workspace marks approval_required, or a call an argument policy sends to a human, is not forwarded. The gateway creates an approval request, notifies every member of the workspace, and answers. What it answers depends on who called:

  • Interactive callers (a portal JWT, which is your chat client) get 428 Precondition Required: the person is right there.
  • Non-interactive callers (an API key or an agent identity) get 202 Accepted and poll.

The body is the same for both:

{
"approval_required": true,
"approval_request_id": "01m61n3w4rkvkhhb598prj54b8",
"capability_type": "tool",
"capability_name": "applications_set_recommendation",
"server_name": "brutor-demo-applications",
"surface": "mcp",
"timeout_seconds": 86400,
"expires_at": "2026-10-29T16:22:27.736669+00:00",
"poll_url": "/v1/portal/approvals/01m61n3w4rkvkhhb598prj54b8/poll",
"message": "Tool 'applications_set_recommendation' requires approval. Submit with X-Approval-Token header after approval."
}

It is a plain JSON body on the HTTP response, not a JSON-RPC error: check the status before parsing JSON-RPC.

  1. Ask the user. Show the tool, the server and the arguments the model wants to send. A user who is a member of the workspace may decide the request themselves.

  2. Decide it. Approve, and the response carries the one-time token:

    Terminal window
    curl -s -X POST http://localhost:8100/v1/portal/approvals/01m61n3w4rkvkhhb598prj54b8/approve \
    -H "Content-Type: application/json" -H "X-Tenant-ID: default" -H "Authorization: Bearer $JWT" \
    -d '{"note": "Checked the rationale, proceed"}'
    {
    "id": "01m61n3w4rkvkhhb598prj54b8",
    "status": "approved",
    "resolved_by": "2",
    "resolved_at": "2026-10-28T16:40:12.221Z",
    "approval_token": "q3V9n0dJ7cK2...Xw",
    "message": "Tool call 'applications_set_recommendation' approved"
    }

    Or reject it (POST .../reject, same body) and return a “rejected by the user” result to the model.

    If someone else should decide (the user may not, or wants a colleague to), leave it in the inbox and poll GET /v1/portal/approvals/{id}/poll until decision is no longer pending; once approved, the poll response carries approval_token.

  3. Retry the identical call with X-Approval-Token:

    Terminal window
    curl -s "$MCP" "${H[@]}" \
    -H "X-Brutor-Run-Id: 3f2b8c1e-6d4a-4b9e-9c1f-2a7d5e8b0c34" \
    -H "X-Approval-Token: q3V9n0dJ7cK2...Xw" \
    -d '{"jsonrpc": "2.0", "id": 4, "method": "tools/call",
    "params": {"name": "applications_set_recommendation",
    "arguments": {"application_id": "APP-20261027-0481", "recommendation": "decline", "amount_eur": 21300}}}'

    Feed the result to the model as usual. The shipped portal does exactly this in callToolWithApprovalToken (src/lib/mcpClient.ts).

When a catalog entry has oauth_required: true, the MCP server calls its upstream with the user’s identity: the user signs in to the server’s identity provider once, and the gateway stores and refreshes the token. oauth_client gives your client what it needs to start an authorization-code flow with PKCE: client_id, scopes, idp.authorization_endpoint, and the gateway-proxied token_endpoint (/v1/proxy/oauth/{mcp_server_id}/{idp_id}/token). The full flow is on OAuth for MCP servers. When a stored token stops working, tool calls answer 401 with "error": "oauth_reauth_required" and the mcp_server_id; offer a “Re-authorize” action for that server.

A tool result can carry an interactive UI (MCP Apps): its _meta.ui.resourceUri names a ui:// resource. Fetch it through the portal route, which applies the same workspace access check and returns raw HTML:

Terminal window
curl -s "http://localhost:8100/v1/portal/mcp-apps/mcp-01M61N37VYTSY0ETNQWW0DMRFY/resource?uri=ui%3A%2F%2Fmap%2Fmcp-app.html" \
-H "X-Tenant-ID: default" -H "Authorization: Bearer $JWT" -H "Accept: text/html"

The response is text/html; charset=utf-8 (at most 8 MB, Cache-Control: private, max-age=300). Mount it with iframe.srcdoc and sandbox="allow-scripts" only, never allow-same-origin. Errors: 403 no access, 404 server or resource not found, 415 the resource is not an MCP App (MIME text/html;profile=mcp-app or text/html+mcp-ui), 502/504 upstream failure or timeout. When the app asks the host to call a tool, send that tools/call with X-Brutor-Capability: mcp_app_callback so the audit log can tell app-initiated calls from chat-initiated ones.