> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lexera.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Execution Lifecycle: Stream, Dispatch, Retrieve

> Follow Lexera's three-step SSE lifecycle: open a persistent stream, receive a session ID, dispatch JSON-RPC, and retrieve results asynchronously.

Every interaction with the Lexera Protocol follows the same asynchronous lifecycle. You open a persistent Server-Sent Events connection, the server assigns you a session, you POST a JSON-RPC tool call, and the result streams back over the same connection. This design keeps your agent or enterprise system responsive while human attorneys perform verification.

<Steps>
  <Step title="Initialize the SSE stream">
    Open a persistent `GET` request to the MCP server endpoint. Include your Bearer token in the `Authorization` header. The connection stays open for the duration of the session.

    ```bash theme={null}
    curl -N -H "Authorization: Bearer lex_82bea57b8c3fd39f..." \
         -H "Accept: text/event-stream" \
         https://lexera.dev/mcp-server/sse
    ```
  </Step>

  <Step title="Receive the session ID">
    The server streams an `event: endpoint` message containing your unique session URL. Parse the `data` line to extract the path and prepend the base URL.

    ```text theme={null}
    event: endpoint
    data: /mcp-server/messages?sessionId=550e8400-e29b-41d4-a716-446655440000
    ```

    Your session URL becomes:

    ```text theme={null}
    https://lexera.dev/mcp-server/messages?sessionId=550e8400-e29b-41d4-a716-446655440000
    ```
  </Step>

  <Step title="Dispatch the JSON-RPC call">
    POST your JSON-RPC 2.0 payload to the session URL. The server immediately returns `HTTP 202 Accepted`. The actual result, such as a `DISPATCH_SUCCESS` payload or a completed order, arrives later as an SSE message on the open stream.

    ```bash theme={null}
    curl -X POST \
         -H "Authorization: Bearer lex_82bea57b8c3fd39f..." \
         -H "Content-Type: application/json" \
         -d '{
           "jsonrpc": "2.0",
           "id": 1,
           "method": "tools/call",
           "params": {
             "name": "lexera_route_to_specialist",
             "arguments": {
               "document_text": "Vendor contract with uncapped liability clause...",
               "target_jurisdiction": "UAE",
               "specialist_instructions": "Cap liability at 100% of contract value."
             }
           }
         }' \
         https://lexera.dev/mcp-server/messages?sessionId=550e8400-e29b-41d4-a716-446655440000
    ```
  </Step>
</Steps>

## JavaScript example: opening the stream and extracting the session URL

The following example uses the native `fetch` API to open the SSE stream, read chunks, and extract the session URL with a regular expression.

```js theme={null}
const response = await fetch("https://lexera.dev/mcp-server/sse", {
  headers: { "Authorization": "Bearer lex_82bea57b8c3fd39f..." }
});

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
  const { value, done } = await reader.read();
  if (done) break;

  const text = decoder.decode(value);
  if (text.includes("event: endpoint")) {
    const match = text.match(/data: (\/mcp-server\/messages\?sessionId=[\w-]+)/);
    const sessionUrl = `https://lexera.dev${match[1]}`;
    // Proceed to dispatch tool...
  }
}
```

<Warning>
  Keep the SSE connection open until the final result arrives. Closing the stream prematurely disconnects your session and you will not receive the tool call result.
</Warning>

## What happens after dispatch

After the POST returns `202 Accepted`, the server processes the request asynchronously. For a human-in-the-loop tool such as `lexera_route_to_specialist`, this means:

1. The server places a temporary AED wallet hold.
2. The document enters the assigned law firm's triage queue.
3. Once the attorney completes review, the server streams the finalized payload back over your open SSE connection.

Because the result arrives as an SSE event, your integration should listen continuously after every dispatch.

## Next steps

* Browse the available MCP tools in [Tools Overview](/tools/introduction)
* Understand how pricing and wallet holds work in [Wallet & Pricing](/concepts/wallet-and-pricing)
