MCP SSE affinity configuration

Updated at:

In Model Context Protocol (MCP) Server scenarios, routing requests from the same MCP session to the same function instance is crucial for maintaining context consistency. This feature, based on the MCP SSE protocol, intercepts the session ID from the first SSE response and routes subsequent requests with the same session ID to the bound instance. This is ideal for hosted MCP Server scenarios.

Core configuration

On the function's configuration page, go to Advanced Settings > Isolation & Affinity. Enable session affinity, select MCP SSE affinity, configure the SSE path (default is /sse) and concurrent sessions per instance, and click Deploy.

After an SSE connection is established using an official MCP client, the system automatically extracts the session ID and applies affinity-based routing.

Usage notes

  • General limitations: Before you begin, read General Limitations and Principles of Session Affinity.

  • Runtime limitations:

    • Built-in runtimes: MCP SSE affinity is not supported.

    • MCP runtime: Only MCP affinity (including SSE) is supported.

    • Other runtimes do not have this limitation.

  • Client requirements: You must use an official MCP client or SDK to initiate requests. Otherwise, the system cannot establish a valid affinity connection.

  • Session lifecycle: A session's maximum lifecycle equals the function's timeout period. If this limit is exceeded, the server disconnects the connection. A reconnection generates a new session ID and does not guarantee routing to the original instance.

  • Access method limitations: This feature can be accessed only through an HTTP trigger or a custom domain name.

  • Request limitations:

    • The initial SSE request does not support query parameters.

    • A single instance can handle multiple sessions simultaneously (default: 20, max: 200). When the number of sessions bound to a single instance reaches its limit, the system automatically creates a new instance.

    • An SSE persistent connection consumes 1 concurrency unit. Message requests consume N concurrency units. The combined concurrency is capped at 200.

  • Session API management is not supported.

Configure MCP SSE affinity

Process overview

Configuring MCP SSE affinity involves three main steps: enabling session affinity, selecting the MCP SSE type, and configuring the SSE path and parameters before deploying. You must also ensure that your function code adheres to the MCP protocol specification.

Enable session affinity

  1. Log on to the Function Compute console.

  2. In the function list, select a target function or create a function.

    When creating a function, you can configure Isolation & Affinity directly in the Advanced Settings section.
  3. On the function details page, click the Configuration tab.

  4. In the Advanced Settings section, find the Isolation & Affinity setting.

  5. Click Isolation & Affinity to expand the configuration panel.

  6. Enable the session affinity switch.

Select the MCP SSE affinity type

  1. In the session affinity configuration area, select the MCP SSE affinity radio button.

  2. The system automatically displays the configuration options for MCP SSE affinity.

Configure the SSE path

Purpose: Set the request path used to establish the SSE connection.

Procedure:

  1. In the SSE path input box, enter the path for SSE connection requests.

    • Default value: /sse

    • You can customize the path based on your requirements.

    • Note: Ensure the SSE path matches the path defined in your function code.

Configure session parameters

Purpose: Set the number of sessions that a single instance can handle concurrently.

Procedure:

  1. Concurrent sessions per instance: The maximum number of sessions a single instance can handle simultaneously.

    • Default value: 20

    • Value range: 1–200

    • Recommendation: For testing scenarios, use a smaller value such as 10. For production environments, adjust the value based on your business needs.

  2. Click the Deploy button to save your configuration.

Important:

  • The initial SSE request does not support query parameters.

  • When session affinity is enabled, the system automatically sets the concurrency per instance to 200. This value is a system default and cannot be changed.

  • MCP SSE affinity requires that your function code implements the MCP protocol specification. You must use an official MCP client or SDK.

Verify MCP SSE affinity

To verify the affinity configuration, use a standard MCP client to send requests to the function's HTTP trigger endpoint or custom domain name. For client setup, refer to the official MCP documentation, Build an MCP client.

FAQ

Why is MCP SSE affinity not working?

Possible causes:

  1. You are not using an official MCP client or SDK.

  2. Your function code does not correctly implement the MCP protocol specification.

  3. The SSE path configuration does not match the path in your function code.

Troubleshooting steps:

  1. Confirm that you are using an official MCP client or SDK.

  2. Check if your function code correctly implements the MCP protocol specification.

  3. Verify that the client's request path matches the SSE path configured for the function.

How do I choose an SSE path?

Recommendations:

  • Use the default path, /sse, which is the standard path for the MCP protocol.

  • If you need to customize the path, ensure it matches the path in your function code.

Appendix: Protocol analysis

Event format

Field

Description

event

Specifies the event type, such as endpoint, message, or error.

data

The data payload that contains the actual content, such as JSON or text.

id (Optional)

A unique identifier for the event, used for recovery after a disconnection.

retry (Optional)

The reconnection time interval in milliseconds after a disconnection.

Example:

event: endpoint
data: {"sessionId": "abc123", "version": "2025-06-18"}

How MCP uses SSE

MCP uses SSE to implement a bidirectional communication channel between the client and server. The key mechanisms are:

  1. Generating and returning a session ID when the SSE connection is first established.

  2. Carrying this session ID in all subsequent requests to maintain affinity.

  3. Pushing response results back to the client through the original SSE connection.

Key point: SSE acts as the "message channel," while HTTP POST serves as the "request channel."

MCP SSE sequence diagram

image.svg

  1. The client sends a GET /sse request to establish a persistent SSE connection (Connection1).

  2. The server responds with event: endpoint, data: { "sessionId": "abc123" }.

  3. The client uses the session ID to send a POST /message?sessionId=abc123 request (Connection2).

  4. The server returns a 202 Accepted response with an empty body, which indicates that the request was received.

  5. The server pushes the actual response message through Connection1.

  6. The client sends a POST /initialized?sessionId=abc123 request (Connection3).

  7. The server returns a 202 Accepted response with an empty body.

  8. The client sends a POST /list tools?sessionId=abc123 request (Connection4).

  9. The server returns a 202 Accepted response with an empty body.

  10. The server pushes the list of tools through Connection1.

  11. The client sends a POST /call tool?sessionId=abc123 request (Connection5).

  12. The server returns a 202 Accepted response with an empty body.

  13. The server pushes the call result through Connection1.

Diagram notes:

  • Connection1: The persistent SSE connection used to receive pushed messages.

  • Connection2–5: The HTTP POST requests used to send commands.

  • All responses are returned through the original SSE connection.