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

# Sessions

> Group related events with automatic or explicit session tracking

Sessions group related events together. mcp-tap uses two strategies.

## Automatic session detection

When `init()` is called, a `SessionManager` is created that tracks sessions via message fingerprinting. For LLM requests, it hashes the first user message content (SHA-256, first 12 hex chars). If the fingerprint changes between requests, a new session ID is generated.

## Explicit sessions

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    import { session } from "mcp-tap";

    const result = await session("checkout-flow", async () => {
      const a = await llm.create(...);
      const b = await llm.create(...);
      return b;
    });
    ```

    Sessions nest correctly — when an outer session is active, starting an inner session overrides it for the duration of the inner function, then restores the outer.

    If called with only a function (no name), a random session ID is generated:

    ```typescript theme={null}
    const result = await session(async () => {
      // auto-generated session ID
    });
    ```
  </Tab>

  <Tab title="Python">
    Use the context manager:

    ```python theme={null}
    import mcp_tap

    with mcp_tap.session("checkout-flow") as sid:
        response1 = client.messages.create(...)
        response2 = client.messages.create(...)
    ```

    Or the imperative API:

    ```python theme={null}
    import mcp_tap

    mcp_tap.start_session("checkout-flow")
    response = client.messages.create(...)
    mcp_tap.end_session()
    ```

    If `name` is `None`, a random session ID is generated.
  </Tab>
</Tabs>
