Stateful MCP

Configure stateful or stateless session routing to MCP backends.

About MCP sessions

An MCP session is a set of related interactions between a client and an MCP server that starts with the client-server initialization phase. According to the MCP specification, a session for the Streamable HTTP transport is established if the server sends back an Mcp-Session-Id header during the initialization phase. Clients include this header in subsequent requests to keep the stateful session.

If you run multiple agentgateway proxy instances without stateful sessions, the first request might be forwarded to one instance and a session is created for that instance. However, later requests might be forwarded to a different proxy instance that does not know about the session.

To ensure that subsequent requests are routed to the same agentgateway proxy instance, the proxy exposes streamable HTTP endpoints as stateful endpoints by default and sends back the session ID in the Mcp-Session-Id header. You can disable this setting and instead use stateless MCP servers by using the sessionRouting: Stateless setting in the AgentgatewayBackend resource.

Stateless

The following configuration disables stateful session routing for a streamable HTTP endpoint. You typically use this configuration if you have stateless agents that connect to your MCP server or where the state is handled in the client directly. The MCP server treats every request as a new request and therefore requires the entire context to be sent as part of the request.

apiVersion: agentgateway.dev/v1alpha1
kind: AgentgatewayBackend
metadata:
  namespace: default
  name: mcp-static-no-protocol
spec:
  mcp:
    targets:
    - name: mcp-target
      static:
        host: mcp-website-fetcher.default.svc.cluster.local
        port: 80
        protocol: SSE   
    sessionRouting: Stateless

Stateful (default)

The following configuration enables stateful session routing for a streamable HTTP endpoint. During the client-server handshake, the agentgateway proxy instance that handles the request sends back the mcp-session-id header with the session ID that was assigned. The client includes this header in subsequent requests to keep the session alive.

apiVersion: agentgateway.dev/v1alpha1
kind: AgentgatewayBackend
metadata:
  namespace: default
  name: mcp-static-no-protocol
spec:
  mcp:
    targets:
    - name: mcp-target
      static:
        host: mcp-website-fetcher.default.svc.cluster.local
        port: 80
        protocol: SSE   
    sessionRouting: Stateful
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.