Sanity Library Reference Docs
    Preparing search index...

    Function createMcpSessionTelemetry

    • Create the Workflows telemetry for one MCP session.

      Events go to the first project the session binds to. They carry the package version, project id, and dataset, never tool arguments, results, or a user id.

      Nothing is sent when denied is true, or when nothing is logged. Otherwise the account's consent is checked at the first send, retried if that check fails, and cached for this telemetry object. Sending never throws.

      Parameters

      • args: {
            clientFor: (resource: WorkflowResource) => TelemetryIntakeClient;
            denied: boolean;
            nodeEnv?: string;
            sessionId?: string;
        }
        • clientFor: (resource: WorkflowResource) => TelemetryIntakeClient

          Returns an authenticated client for resource. Called once, on the first successful McpSessionTelemetry.bind.

        • denied: boolean

          true when the user or organization opted out.

        • OptionalnodeEnv?: string

          development or test reports as development; anything else, or nothing, reports as production.

        • OptionalsessionId?: string

          Shared id for every telemetry object in one MCP session. Use a random id, never a credential or session handle. Omit to mint one.

      Returns McpSessionTelemetry

      Assumes the host has clientForResource and telemetryEnabled, and that address and client belong to the current tool call.

      import {createMcpSessionTelemetry, createWorkflowEngine} from '@sanity/workflow-mcp'

      const telemetry = createMcpSessionTelemetry({
      clientFor: (resource) => clientForResource(resource),
      denied: !telemetryEnabled,
      })

      // For each tool call:
      telemetry.bind(address.workflowResource)
      const engine = createWorkflowEngine({address, client, telemetry: telemetry.logger})

      // When the session closes:
      await telemetry.flush()
      telemetry.end()