Does Cancelling an Azure AI Foundry Agent Stream Stop Server-Side MCP Tool Execution?
Understanding Client-Side Stream Cancellation in Azure AI Foundry
When working with agentic workflows in Azure AI Foundry using the Responses API, streaming agent outputs via Python's asyncio is a common architectural pattern. However, a crucial question arises when handling timeouts or consumer disconnections: Does cancelling the local client task actually stop server-side Model Context Protocol (MCP) tool execution?
The short answer is: Not immediately. When you cancel a client-side streaming task, you are severing the local HTTP connection. While the server will eventually handle the broken pipe, ongoing MCP tool calls (like web searches or API requests) may continue to run to completion on the server, potentially consuming quota and compute.
What Happens Under the Hood?
To understand why this happens, it helps to distinguish between client-side connection teardown and server-side execution management:
- HTTP Connection Teardown: Cancelling an
asyncio.Taskclosing the client stream simply closes the socket on the client side. The Azure AI Foundry server receives aConnectionResetor broken pipe notification next time it attempts to push a chunk. - MCP Tool Execution: If the agent is currently waiting on an external MCP tool call (for example, a web search or custom API invocation), the tool execution is managed as an independent synchronous or asynchronous operation on the backend server. The backend typically finishes processing the active tool call before evaluating whether the downstream HTTP pipe is still open.
As a result, your agent might finish executing the tool and attempting to generate tokens before realizing the client disconnected, leading to unnecessary token usage and API costs.
How to Correctly Stop Server-Side Agent Execution
If guaranteed cancellation and cost control are critical to your application, you should rely on explicit server-side cancellation patterns rather than unmanaged socket drops.
Solution 1: Use Background Responses with Explicit Cancellation
For operations where client-driven aborts are common, initialize the response using background execution mode. This gives you a valid response_id that you can pass to the server-side cancellation endpoint.