The Root Cause
Copilot Studio and custom connectors work best with stateless APIs. They currently have a hard limitation: you cannot extract a custom header from an API response and dynamically map it into a variable to use downstream.
Even if you define the headers in your Swagger file, Copilot Studio cannot pass that session state across different actions.
Direct Answers
Can response headers be mapped to agent variables?
No, not natively today.
Can a custom connector dynamically populate request headers from outputs of previous actions?
You can pass inputs into request headers if they are explicitly defined as parameters, but since you cannot capture the response header from the previous step, you cannot pass it dynamically.
Is an Azure Function / APIM proxy the recommended approach for managing MCP sessions?
Yes. For any stateful or session-based protocol like Model Context Protocol (MCP), an external layer is the standard pattern.
Recommended Architectures
Option 1: Use an Azure Function or APIM Proxy (Best Practice)
Let a proxy layer handle the state. Copilot Studio talks to the proxy, and the proxy manages the session with the MCP server:
Copilot Studio > Custom Connector > Azure Function > MCP Server
The Azure Function handles the initialize call, catches and stores the mcp-session-id, and automatically attaches it to all subsequent backend requests. Copilot Studio stays completely stateless.
Option 2: Return the Session ID in the Response Body
If you have control over the API design, ask the developers to return the mcp-session-id inside the JSON response body instead of the header. Once it is in the body, Copilot Studio can easily parse it, store it in a topic variable, and pass it as an input parameter to the next action.
Summary
The moment an API requires cross-call session continuity via headers, you need to lift that state management out of Copilot Studio. Go with the Azure Function proxy pattern for the cleanest, production-ready setup.
✅ If this answer helped resolve your issue, please mark it as Accepted so it can help others with the same problem.
👍 Feel free to Like the post if you found it useful.