← Files Grid CatalogARCHIVED FILE
docs/api/catalog-source-import/README.md
8.82 KB · Nov 30, 2026 · 00:26 UTC
# Equipment catalog source imports
Implementation: draft PR #6879; migration and Edge Function rollout are required. Discover deployed MCP tools before relying on them. This capability saves equipment **source listings**, preserving original product facts and verified image copies. Canonical products, kits, categories, definitions and matches require separate review.
REST and MCP invoke the same shared service and guarded database transaction. PDF crops, website photos and feed images use the same staged-byte contract. Original URLs are provenance; these operations never fetch them.
| Operation | Partner REST POST | MCP tool | OAuth scopes |
| --- | --- | --- | --- |
| Image upload grant | `/api/v1/catalog-source-import/image-uploads` | `grid.request_catalog_source_image_upload` | `grid:read grid:write` |
| Preview | `/api/v1/catalog-source-import/preview` | `grid.preview_catalog_source_import` | `grid:read` |
| Apply | `/api/v1/catalog-source-import/apply` | `grid.apply_catalog_source_import` | `grid:read grid:write` |
All operations require current catalog-company administration, using the existing `can_access_catalog_source_company` boundary and an active company. OAuth scopes alone grant no company access. Partner REST uses an integration bearer token scoped to the API resource. App-session aliases are `catalog-source-preview/api/import/{image-uploads,preview,apply}` and use the existing Supabase JWT middleware; MCP OAuth tokens are not app JWTs. Existing local/offline source review and legacy preview/apply routes remain available.
## Capture and stage images
1. Resolve the catalog owner's existing company ID; keep owner distinct from manufacturer/brand. Retain a source manifest, original documents, checksums, source dates and observed coverage. Review item/image association, image roles, PDF page and crop coordinates before staging. Leave missing images null.
2. For each selected image, calculate SHA-256 over the actual local bytes. Request a grant with `company_id`, original HTTP(S) `source_url` (a PDF URL is valid crop provenance), `sha256`, `mime_type` and `size_bytes`. JPEG, PNG, WebP and GIF are supported; SVG is not. The existing public-media policy requires at least 100 bytes. Each image and the batch must fit 32 MiB.
3. PUT the raw bytes to returned `upload_url` with the declared content type. Do not send base64 bytes through MCP. Upload objects are private, create-only and cannot overwrite another object. Upload expiry and evidence expiry are returned independently. Evidence lasts 24 hours; both preview and apply inspect, validate and hash the staged bytes.
4. Retain `image_evidence` with the original source URL and hash. Proofs bind actor, company, source, MIME and byte count. Generic entity attachment or legacy location evidence cannot be substituted.
## Preview the complete batch
Send the original snake_case `CatalogSourceCandidate` facts using the [OpenAPI schema](openapi.json). The envelope is:
```json
{
"company_id": "11111111-1111-4111-8111-111111111111",
"source_url": "https://manufacturer.example/catalog/",
"expected_candidate_count": 1,
"expected_image_count": 1,
"candidates": ["complete CatalogSourceCandidate objects"],
"image_bindings": [{
"client_url": "capture:photo-001",
"source_url": "https://manufacturer.example/brochure.pdf",
"sha256": "64 lowercase hexadecimal characters",
"image_evidence": "returned staging proof"
}]
}
```
The example illustrates the envelope; replace the placeholder candidate and hash with complete valid data. `client_url` is a literal reference appearing in candidate `image_url`, gallery `images[].url` or `variants[].image_url`; it may be a local capture reference because it is never fetched. All new image references require matching bindings. Bindings for reused source rows or unused images are rejected. Different references with identical hashes share one verified public copy within an attempt.
The catalog root URL normalizes outer whitespace and scheme case, retaining path/query/fragment identity; image-evidence URLs retain their original spelling to match the signed proof. Supply independent intended counts; mismatches fail rather than accepting a partial batch. Limits: 1–50 candidates, 50 bindings, 1,000 distinct attribute keys across the batch after canonical harvesting, 750,000 UTF-8 JSON bytes (apply allows a further 4,096 bytes for confirmation), 32 MiB of staged image bytes. The canonical planner's field bounds also apply: 8,000-character descriptions, 2,000-character bounded text fields, 20 gallery images, 50 variants, 30 spec blocks, 120 raw attributes, 50 warnings and 30 owner-evidence entries per candidate. The external byte cap applies to the original reviewed request; verified storage URLs/cache metadata are added to its validated internal write plan without consuming that external budget again. Input that would truncate facts is rejected; split batches and preserve complete artifacts. Duplicate source identities must be reconciled first.
Preview returns signed `preview_token`, expiry, source revision, intent hash, expected/received counts, create/reuse/image counts, item decisions, existing IDs, warnings and policy. The token expires after 30 minutes and binds actor, company, complete normalized request and relevant source/reference state. Show the actual original candidate facts, image associations, counts, warnings and reused IDs for explicit confirmation. The token itself is not permission to apply.
This initial operation is create-or-reuse only. Existing active source hashes are reused unchanged, including authored edits, curated attributes and canonical matches. A hash belonging to another company is rejected. A capture refresh is not an implicit overwrite operation. Contextual persistence hints (`source_variant`, `inferred_variant`, or a parent source ID) are rejected before preview; keep published variants/options as source facts and use existing catalog review for contextual materialization. New rows use the existing canonical source planner and persistence RPC; normalized attributes may reference existing definitions but do not create definitions.
## Apply and retry
Send the **unchanged original preview envelope**, `preview_token`, a stable UUID `request_id`, and `confirm_preview: true`. A missing/false confirmation is rejected. Changed intent, expired previews, changed staged bytes or relevant state changes require a new preview and review. Unrelated source edits that this operation never overwrites, unrelated attribute definitions, and catalog load timestamps do not invalidate a preview.
Apply copies reviewed bytes to attempt-specific Grid public-media paths and verifies the stored copy's SHA-256. Cleanup obligations are persisted before public writes. Only after all copies verify does a guarded database transaction recheck company authority and source state, create source rows/attributes and save the receipt atomically. Multi-table failure rolls back records and receipt together. Images are public after copying; private staging alone creates no public image.
The receipt returns `status: applied`, action/request/company IDs, created/reused counts, saved source item/catalog IDs, copy paths/hash/verification and `canonical_writes: false`. Original image URLs and capture references remain provenance. No automatic reversal is advertised.
Retry identical apply arguments and the same request ID after a timeout. A committed receipt returns the original IDs before preview/media-expiry checks, after rechecking current company access. Reused IDs with different intent fail. Do not infer rollback from a timeout or delete potentially committed image paths. Uncommitted or losing copies retain durable cleanup obligations in the existing action storage ledger. This operation does not promise automatic reclamation of abandoned failed-attempt copies; operational cleanup must check references and preserve uncertain outcomes. Private staging uses the existing evidence retention policy.
Errors: REST 422 invalid shape/count/field limit; 403 insufficient scopes or company/source authorization; 409 changed/expired preview, image evidence or request identity conflict; 413 oversized HTTP body; 503 upstream/deployment failure. MCP maps validation to invalid params, authorization to `-32001` and review conflicts to `-32009`. Deployment/schema errors are not reported as successful imports or user conflicts. The legacy partner error envelope uses `research_conflict` as its generic REST 409 code; use status/message to identify this operation's conflict.
[OpenAPI](openapi.json). Contract tests cover the shared service, both transports and the real canonical source RPC on disposable PostgreSQL. Live Storage/OAuth rollout verification remains a separate deployment check.
Reviewed selected-image corrections on existing listings use the separate [source media operation](../catalog-source-media/README.md), when deployed. Repeat import remains create-or-reuse only.
SHA-256: 436fc0890ae99559f50e8f18a9cc62fcdd1be4bacdc5a6fddb16ae1b4897f040