In Model Context Protocol (MCP) Server scenarios, requests from the same MCP session must be routed to the same function instance to maintain context consistency. The MCP Server-Sent Events (SSE) affinity feature uses the MCP SSE protocol to intercept the session ID from the first SSE response. The system then routes subsequent requests with the same session ID to the bound instance. This feature is ideal for hosting an MCP Server.
Core configuration
On the function configuration page, in the Advanced Configuration > Isolation and Affinity section, turn on the Session Affinity switch. Then, select MCP SSE Affinity, configure the SSE Path (the default is /sse) and Concurrent Sessions per Instance, and click Deploy to enable the feature.
After you establish an SSE connection with the official MCP client, the system automatically extracts the session ID and enables affinity-based routing.
Applicability
General limits: For more information, see General limits and principles of session affinity.
Runtime limits:
Built-in runtimes: MCP SSE affinity is not supported.
MCP runtimes: Only MCP affinity (including SSE) is supported.
Other runtimes do not have this limit.
Client requirements: You must initiate requests using the official MCP client or software development kit (SDK). Otherwise, a valid affinity connection cannot be established.
Session lifecycle: The maximum session lifecycle is equal to the function's maximum timeout period. If the timeout period is exceeded, the server disconnects the connection. A reconnection generates a new session ID and is not guaranteed to be routed to the original instance.
Access method limits: Access is supported only through HTTP triggers or custom domain names.
Request limits:
The first SSE request does not support query parameters.
A single instance can process multiple sessions simultaneously (default: 20, maximum: 200). When the number of sessions bound to a single instance reaches this limit, the system automatically creates a new instance.
A persistent SSE connection uses one concurrency unit, and message requests use N concurrency units. Together, they share a concurrency quota of 200.
SessionAPI management is not supported.
Configure MCP SSE affinity
Overview
Configuring MCP SSE affinity involves three steps: enabling session affinity, selecting the MCP SSE type, and configuring the SSE path and parameters. After configuration, you must deploy the function. Ensure that your function code implements the MCP protocol specification.
Enable session affinity
Log on to the Function Compute console.
In the function list, select a function or create a function.
When you create a function, in the Advanced Configuration section, find Isolation and Affinity, complete the configurations as described in the following steps, and then create the function.
On the function details page, click the Configuration tab.
In the Advanced Configuration section, find Isolation and Affinity.
Click Isolation and Affinity to expand the configuration panel.
Turn on the Session Affinity switch.
Select the MCP SSE affinity type
In the session affinity configuration section, select the MCP SSE Affinity radio button.
The system automatically displays the configuration options for MCP SSE affinity.
Configure the SSE path
Purpose: Set the request path used to establish SSE connections.
Procedure:
In the SSE Path text box, enter the path for the SSE connection request.
Default value:
/sse.You can customize the path as needed.
Note: Ensure that the SSE path is consistent with the path in your function code.
Configure session parameters
Purpose: Set the number of sessions that a single instance can process simultaneously.
Procedure:
Concurrent Sessions Per Instance: Specifies the maximum number of sessions that a single instance can process simultaneously.
Default value: 20.
Value range: 1 to 200.
Suggestion: Set a smaller value, such as 10, for testing scenarios. Adjust the value for production environments as needed.
Click the Deploy button to save the configuration.
Important:
The first SSE request does not support query parameters.
After you enable session affinity, the system automatically sets Concurrency Per Instance to 200. This is the system default and cannot be changed.
MCP SSE affinity requires that the function code implements the MCP protocol specification. You must use the official MCP client or SDK.
Verify the MCP SSE affinity feature
For more information, see the official MCP document Build an MCP client. To verify the affinity configuration, use the default HTTP trigger domain name or a custom domain name provided by Function Compute to send requests to the function through a standard MCP client.
FAQ
Why is MCP SSE affinity not working?
Possible causes:
The official MCP client or SDK is not used.
The function code does not correctly implement the MCP protocol specification.
The configured SSE path is inconsistent with the path in the function code.
Troubleshooting steps:
Confirm that you are using the official MCP client or SDK.
Check whether the function code correctly implements the MCP protocol specification.
Check whether the initial request path and the custom path configured for the function are consistent.
How do I select an SSE path?
Suggestions:
Use the default path
/sse. This is the standard path for the MCP protocol.If you customize the path, ensure that it is consistent with the path in your function code.
Appendix: Protocol analysis (MCP SSE)
Event format
Field | Description |
| Specifies the event type, such as |
| The data body that contains the actual content, such as JSON or text. |
| Uniquely identifies the event. Used for recovery after a disconnection. |
| The reconnection interval after a disconnection, in milliseconds. |
Example:
event: endpoint
data: {"sessionId": "abc123", "version": "2025-06-18"}How does MCP use SSE?
MCP uses SSE to implement a bidirectional communication channel between the client and the server. The key points are as follows:
Generate and return a session ID when the first SSE connection is established.
Carry this session ID in all subsequent requests to achieve affinity.
Push response results to the client through the original SSE connection.
Key point: SSE is the "message channel", and HTTP POST is the "request channel".
Typical MCP SSE request time series chart
The client initiates a GET /sse request to establish a persistent SSE connection (Connection1).
The server returns an event: endpoint, data: { "sessionId": "abc123" }.
The client uses the session ID to initiate a POST /message?sessionId=abc123 request (Connection2).
The server returns 202 Accepted (no content) to indicate that the request is received.
The server pushes the actual response message through Connection1.
The client initiates a POST /initialized?sessionId=abc123 request (Connection3).
The server returns 202 Accepted (no content).
The client initiates a POST /list tools?sessionId=abc123 request (Connection4).
The server returns a 202 status code with an empty response body.
The server pushes the tool list through Connection1.
The client initiates a POST /call tool?sessionId=abc123 request (Connection5).
The server returns a 202 response with no content.
The server pushes the call result through Connection1.
Legend:
Connection1: The persistent SSE connection used to receive pushes.
Connection2 to 5: The HTTP POST requests used to send instructions.
All responses are returned through the original SSE connection.