@modelcontextprotocol/sdk
    Preparing search index...

    Class StreamableHTTPServerTransport

    Server transport for Streamable HTTP: this implements the MCP Streamable HTTP transport specification. It supports both SSE streaming and direct HTTP responses.

    Usage example:

    // Stateful mode - server sets the session ID
    const statefulTransport = new StreamableHTTPServerTransport({
    sessionIdGenerator: () => randomUUID(),
    });

    // Stateless mode - explicitly set session ID to undefined
    const statelessTransport = new StreamableHTTPServerTransport({
    sessionIdGenerator: undefined,
    });

    // Using with pre-parsed request body
    app.post('/mcp', (req, res) => {
    transport.handleRequest(req, res, req.body);
    });

    In stateful mode:

    • Session ID is generated and included in response headers
    • Session ID is always included in initialization responses
    • Requests with invalid session IDs are rejected with 404 Not Found
    • Non-initialization requests without a session ID are rejected with 400 Bad Request
    • State is maintained in-memory (connections, message history)

    In stateless mode:

    • No Session ID is included in any responses
    • No session validation is performed

    Implements

    Index

    Constructors

    Properties

    onclose?: () => void

    Callback for when the connection is closed for any reason.

    This should be invoked when close() is called as well.

    onerror?: (error: Error) => void

    Callback for when an error occurs.

    Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band.

    onmessage?: (message: JSONRPCMessage, extra?: { authInfo?: AuthInfo }) => void

    Callback for when a message (request or response) is received over the connection.

    Includes the authInfo if the transport is authenticated.

    sessionId?: string

    The session ID generated for this connection.

    Methods

    • Handles an incoming HTTP request, whether GET or POST

      Parameters

      • req: IncomingMessage & { auth?: AuthInfo }
      • res: ServerResponse
      • OptionalparsedBody: unknown

      Returns Promise<void>

    • Sends a JSON-RPC message (request or response).

      If present, relatedRequestId is used to indicate to the transport which incoming request to associate this outgoing message with.

      Parameters

      Returns Promise<void>

    • Starts the transport. This is required by the Transport interface but is a no-op for the Streamable HTTP transport as connections are managed per-request.

      Returns Promise<void>