Skip to content

feat(protocol): add media/formats for mdxp 0.9.0 - #5

Open
zkaeshjm wants to merge 1 commit into
motrixapp:mainfrom
zkaeshjm:feat/media_formats_20261018
Open

zkaeshjm wants to merge 1 commit into
motrixapp:mainfrom
zkaeshjm:feat/media_formats_20261018

Conversation

@zkaeshjm

Copy link
Copy Markdown

Problem and contract

agalwood/Motrix#2365 adds a yt-dlp quality picker to the browser extension.
The picker needs to ask the desktop which qualities a page offers, and the
review of that PR asked for exactly one thing before it can merge:

Use the canonical published MDXP contract for media/formats: define and
review it in the protocol repository, including parameter validation,
data-sharing requirements, and exact transport exposure, then consume the
published version in both applications.

Today @motrix/mdxp@0.8.1 has no format-listing method, so #2365 carries a
fork-local declare module '@motrix/mdxp' augmentation plus a hand-mirrored
schema in the extension. This PR defines the method instead, so both apps can
delete their local copies and import the published contract.

This is the same additive shape as download/directories (0.7.0) and the
in-flight downloadHandoff contract (#4): schemas + typed RPC entry + tool
registry entry + a bilingual normative contract. It deliberately does not
implement an extractor, a media-site policy, a picker or a cache.

What is added

  • MediaFormatsParamsSchema — strict { url, cookies? }.
  • MediaFormatsResultSchema — non-strict { available, reason?, title?, durationSec?, entries[] }.
  • Methods.MediaFormats = 'media/formats', an MdxpRequestMap entry, the
    inferred types in the types barrel, and a Tools entry.
  • capabilities.mediaFormats — optional, absent means unsupported.
  • HttpUrlSchema exported from resource.ts, so every method that makes the
    host fetch a caller-supplied URL shares one policy.
  • docs/media-formats.md + docs/media-formats.zh-CN.md, shipped in the
    package (docs/media-formats*.md), plus a README section and usage example.

Parameter validation and bounds

url is restricted to http:/https: (reusing the existing HttpUrlSchema),
because the listing is a host-side fetch that spawns the host's extractor:
file:, data: and javascript: are rejected at the contract boundary. This
is tighter than the app-local draft, which accepted any non-empty string.

Cookies are { name, value } only — the pair needed to build a Cookie: header
— bounded to 256 entries, with a bounded name and value. Params are strict, so
unknown keys (e.g. a raw format selector) are rejected rather than ignored.

Quality tokens

An entry's key is the token the client echoes back as
download/submit.meta.qualityLabel, validated by ^(\d{3,4}p|mp3)$. It is
never a raw extractor selector, so a client cannot smuggle arbitrary format
syntax into a download through this method, and media/formats never
authorizes a download: the following download/submit is validated on its own.

Data sharing

Cookies are disclosed to the extractor for that single URL only and are never
persisted, echoed back or logged. The result carries a title, duration and
option list — no extractor stderr, no filesystem paths. The contract also
states that a host must apply the same media-site, permission and rate policy
it applies to a submitted download, so a URL the submit path refuses is refused
here too and the picker can never promise a quality the submit would decline.

Transport exposure

media/formats is extension-only: registered in Tools for schema discovery
with agentFacing: false, absent from toAgentToolCatalog(), and must not be
reachable over the unary POST /mdxp transport. The contract states explicitly
that agentFacing: false is not authorization and that the transport must
authenticate the principal as a paired extension session before dispatch.

Capability negotiation and failure semantics

A client calls the method only when initialize returns
capabilities.mediaFormats === true; absence means unsupported. An unproducible
listing is a successful available: false with a machine-readable reason
(disabled, not-allowed, binary-missing, unsupported, failed, …) and
empty entries — never a protocol error, and reason is documented as opaque
with a non-exhaustive value set. Only transport/protocol errors (MethodNotFound
from an older host) are failures, and the compatibility matrix covers every
old/new combination in both directions.

Version

0.9.0 — additive new method, no breaking change to protocolVersion 1.0,
to download/submit, or to any existing initialize/task/pairing shape.

Validation

  • 169 tests pass (pnpm test), including 13 new cases: strict params,
    http(s)-only enforcement, cookie bounds, raw-selector rejection,
    forward-compatible result stripping, non-agent-facing registration, and
    capability absence.
  • pnpm lint (Biome) and pnpm build (tsc, exactOptionalPropertyTypes) pass.
  • npm pack verified: the contract docs ship in the tarball.

This repository has no CI workflow today; #4 adds one ("MDXP contract checks").
I ran the equivalent steps locally and am happy to rebase on #4 so this branch
picks up that workflow rather than adding a second, conflicting copy.

Follow-ups once this is published

  • agalwood/Motrix#2365: depend on the published version, advertise
    capabilities.mediaFormats, validate params with the exported schema, and
    delete src/core/bridge/mdxp-local-methods.d.ts +
    src/shared/schemas/media-formats.ts.
  • motrixapp/motrix-extension#56: import the param/result types from
    @motrix/mdxp and drop src/shared/mediaFormats.ts's mirrored wire shapes +
    src/shared/mdxp-media-formats.d.ts, keeping only the picker-local helpers.

I am not asking for a release before review; the intent is a reviewed contract
that both apps can then consume.

Adds the media/formats method: a client asks the host which downloadable
video/audio qualities a page offers, so a browser-extension quality picker
can show real options before the user submits.

- MediaFormatsParamsSchema: strict { url, cookies? }; the host fetches the
  URL itself, so it is restricted to http(s) and the cookie list is bounded
  to 256 { name, value } pairs.
- MediaFormatsResultSchema: non-strict { available, reason?, title?,
  durationSec?, entries[] } where an entry key is the quality token echoed
  back as download/submit.meta.qualityLabel - never a raw extractor
  selector. An unproducible listing is a successful available:false with a
  machine-readable reason, not a protocol error.
- Registered in Methods/Tools as a non-agent-facing client to server method
  (extension WS only; not exposed on unary HTTP), and gated on the new
  capabilities.mediaFormats server flag.
- Exports HttpUrlSchema from resource.ts so every method that makes the host
  fetch a caller-supplied URL shares one http(s)-only policy.
- README (EN + zh-CN): methods table row, usage example, and the
  data-sharing/transport-exposure contract.
Copilot AI balanced review requested due to automatic review settings October 18, 2026 05:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants