Mux TypeScript SDK

repository·main·Indexed 41 days ago

https://github.com/muxinc/mux-node-sdk

The official TypeScript library (@mux/ts v15.2.0) for interacting with the Mux REST API from server-side TypeScript or JavaScript environments. Includes a Model Context Protocol (MCP) server for AI agents, JWT helpers for signing playback IDs, and utilities for parsing and verifying webhook payloads.

Tokens
45K
Snippets
147
Records
212
Agent score
52%

What's inside Mux

  1. Configure Code Execution Modes

    main

    The Mux MCP server supports two execution modes for the code tool:

    • Local Mode (Default): Code runs on the MCP server machine inside an isolated Deno sandbox. If Deno cannot be automatically installed, you can set the DENO_PATH environment variable to a Deno 2.9 or newer executable.
    • Remote Mode: Code is sent to a remote sandbox service. This is enabled via --code-execution-mode=remote or the MCP_SERVER_CODE_EXECUTION_MODE environment variable.

    When using Remote Mode, you must provide:

    • CODE_SANDBOX_URL (or --code-sandbox-url): The URL of the sandbox service.
    • CODE_SANDBOX_API_KEY (or --code-sandbox-api-key): The API key for the service, sent as a Bearer token.
  2. Handle URI encoded path parameters in v15

    main

    Path parameters are now automatically encoded by default. If you were manually calling encodeURIComponent() on parameters before passing them to the SDK, you must stop doing so to avoid double-encoding.

    - client.example.retrieve(encodeURIComponent('string/with/slash'))
    + client.example.retrieve('string/with/slash') // retrieves /example/string%2Fwith%2Fslash
  3. How Code Mode works in the Mux MCP Server

    main

    The Mux MCP server uses a "Code Mode" tool scheme designed for agents to interact with the Mux TypeScript SDK. The server exposes two primary tools:

    1. Docs Search Tool: Allows the agent to query documentation about the API/SDK.
    2. Code Tool: Allows the agent to write code against the TypeScript SDK. This code is executed in an isolated sandbox without web or filesystem access. The results or logs from the execution are returned to the agent.

    This allows agents to perform complex, deterministic tasks by writing and running SDK code in a controlled environment.

  4. Iterate through paginated lists

    main

    Mux API list methods are paginated. You can use the for await ... of syntax to automatically fetch all pages of results.

    Alternatively, you can manually paginate using page.hasNextPage() and page.getNextPage().

    // Option 1: Auto-pagination with for await ... of
    async function fetchAllDeliveryReports(params) {
      const allDeliveryReports = [];
      for await (const deliveryReport of client.video.deliveryUsage.list()) {
        allDeliveryReports.push(deliveryReport);
      }
      return allDeliveryReports;
    }
    
    // Option 2: Manual pagination
    let page = await client.video.deliveryUsage.list();
    for (const deliveryReport of page.data) {
      console.log(deliveryReport);
    }
    
    while (page.hasNextPage()) {
      page = await page.getNextPage();
    }
  5. Make custom or undocumented requests

    main

    If you need to access undocumented endpoints or parameters, the library provides low-level HTTP verb methods (client.get, client.post, etc.).

    • Undocumented endpoints: Use client.post('/path', { body: ... }).
    • Undocumented params: Use // @ts-expect-error to bypass type checking. Extra parameters in GET requests are sent as query params; in other requests, they are sent in the body.
    • Undocumented response properties: Use // @ts-expect-error or type casting to access extra fields returned by the API.
    // Undocumented endpoint
    await client.post('/some/path', {
      body: { some_prop: 'foo' },
      query: { some_query_arg: 'bar' },
    });
    
    // Undocumented parameter
    client.video.assets.create({
      // @ts-expect-error baz is not yet public
      baz: 'undocumented option',
    });
  6. Update Robots API accessors (v14 to v15)

    main
    The Robots API has moved out of preview. You must rename the client.robotsPreview accessor to client.robots and update any associated imported types. Job endpoints are now located under client.robots.jobs.*.
  7. Connect the MCP Inspector to your server

    main

    Use the MCP Inspector to explore your API and test tools. This server implements a local version of OAuth redirects that accepts an API token and client configuration.

    1. Start the inspector: npx @modelcontextprotocol/inspector.
    2. In the inspector UI (usually http://localhost:5173), set the Transport Type to SSE.
    3. Enter the SSE URL: http://localhost:8787/sse (or your Cloudflare workers.dev/sse URL for remote servers).
    4. Click Connect.
    5. When prompted by the mock login screen, enter any email and password to complete the redirect.
    npx @modelcontextprotocol/inspector
  8. Connect Claude Desktop to a local MCP server

    main

    To use your local MCP server with Claude Desktop, you must configure Claude to use a local proxy (mcp-remote) that communicates with your server over HTTP/SSE.

    1. Open your Claude Desktop configuration file (Settings > Developer > Edit Config).
    2. Replace or update the configuration with the following structure, pointing to your local SSE endpoint:
    {
      "mcpServers": {
        "mux": {
          "command": "npx",
          "args": ["mcp-remote", "http://localhost:8787/sse"]
        }
      }
    }