Artifact References¶
Opt-in artifact references let modules exchange opaque binary or media handles across Core and Surfaces without embedding raw bytes in JSON payloads or letting Core fetch external URLs or local files.
Wire Shape¶
An artifact reference is a closed JSON object (defined by schemas/artifact-ref.schema.json) containing exactly three properties:
| Field | Type | Constraints |
|---|---|---|
id |
string | 1–128 ASCII characters matching ^[A-Za-z0-9_-]{1,128}$. Opaque to Core; scoped to the host's store and carries no authority. |
media_type |
string | Canonical lowercase type/subtype without parameters, whitespace, or wildcards (RFC 6838 §4.2 restricted alphabet, max 127 chars per component). |
size_bytes |
integer | 0 to 9,007,199,254,740,991 inclusive (JSON safe integer range across Python, TypeScript, and Rust). Booleans are rejected. |
Schema Assertions¶
A module opts in on any JSON Schema Draft 2020-12 node using one or more of three extension keywords:
| Keyword | Value | Meaning |
|---|---|---|
x-artifact-ref |
true |
Requires the closed ArtifactRef wire shape at this schema node. |
x-media-types |
non-empty array of unique canonical media types | Implies x-artifact-ref: true and restricts media_type to exact matches in the list. |
x-max-bytes |
nonnegative safe integer | Implies x-artifact-ref: true and enforces size_bytes <= x-max-bytes. |
Example module output schema requiring a PNG thumbnail of at most 64 KiB:
{
"type": "object",
"properties": {
"thumbnail": {
"type": "object",
"x-artifact-ref": true,
"x-media-types": ["image/png"],
"x-max-bytes": 65536
}
},
"required": ["thumbnail"]
}
Validation rules:
- Enforcement runs synchronously during input validation, output validation, and public schema validation — Core never queries an
ArtifactStore, opens a local file, or fetches a URL during validation. - Malformed keyword declarations (including inside unused
$defsof an opted-in schema) fail at schema load time withSCHEMA_PARSE_ERROR. - Validation errors for opted-in schemas never echo raw instance values.
- Exported schemas preserve the raw
x-artifact-ref,x-media-types, andx-max-byteskeywords so Surfaces (MCPresource_linkand A2A hosted artifact links) can project them.
Bounded Host-Owned Reads¶
Storage, writes, signing, expiry, and transport URL projection belong to the host and Surfaces. Core provides the ArtifactStore interface and read_artifact (readArtifact in TypeScript) helper so modules or adapters can read immutable bytes under the caller's trusted Identity and an explicit byte budget (default 8 MiB):
- Rejects invalid references, empty identities, or invalid byte limits before calling the store.
- Returns absent (
None/null) without opening the store whenreference.size_bytes > max_bytes. - Calls
store.read(id, identity, min(max_bytes, size_bytes))and verifies that the store's returnedArtifactRefand exact byte length match the reference; missing, unauthorized, expired, or mismatched responses return absent.
import asyncio
from apcore import ArtifactContent, ArtifactRef, ArtifactStore, Identity, read_artifact
class InMemoryArtifactStore(ArtifactStore):
def __init__(self, items: dict[str, tuple[ArtifactRef, bytes]]) -> None:
self._items = items
async def read(self, artifact_id: str, identity: Identity, max_bytes: int) -> ArtifactContent | None:
entry = self._items.get(artifact_id)
if entry is None:
return None
ref, data = entry
if len(data) > max_bytes:
return None
return ArtifactContent(ref=ref, content=data)
async def main() -> None:
ref = ArtifactRef(id="thumb_01", media_type="image/png", size_bytes=4)
store = InMemoryArtifactStore({"thumb_01": (ref, b"\x89PNG")})
identity = Identity(id="user-1", type="user", roles=("reader",))
loaded = await read_artifact(ref, identity, store, max_bytes=65536)
assert loaded is not None and loaded.content == b"\x89PNG"
asyncio.run(main())
import { Identity, parseArtifactRef, readArtifact } from "apcore-js";
import type { ArtifactContent, ArtifactRef, ArtifactStore } from "apcore-js";
class InMemoryArtifactStore implements ArtifactStore {
private readonly items: Map<string, ArtifactContent>;
constructor(items: Map<string, ArtifactContent>) {
this.items = items;
}
async read(id: string, _identity: Identity, maxBytes: number): Promise<ArtifactContent | null> {
const entry = this.items.get(id);
if (!entry || entry.content.byteLength > maxBytes) {
return null;
}
return entry;
}
}
async function main(): Promise<void> {
const ref: ArtifactRef = parseArtifactRef({
id: "thumb_01",
media_type: "image/png",
size_bytes: 4,
});
const bytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);
const store = new InMemoryArtifactStore(new Map([[ref.id, { ref, content: bytes }]]));
const identity = new Identity("user-1", "user", ["reader"]);
const loaded = await readArtifact(store, ref, identity, 65536);
if (loaded !== null) {
console.log(loaded.ref.media_type, loaded.content.byteLength);
}
}
void main();
use std::collections::HashMap;
use apcore::{read_artifact, ArtifactContent, ArtifactRef, ArtifactStore, Identity, ModuleError};
use async_trait::async_trait;
struct InMemoryArtifactStore {
items: HashMap<String, ArtifactContent>,
}
#[async_trait]
impl ArtifactStore for InMemoryArtifactStore {
async fn read(
&self,
id: &str,
_identity: &Identity,
max_bytes: u64,
) -> Result<Option<ArtifactContent>, ModuleError> {
Ok(self
.items
.get(id)
.filter(|item| item.content.len() as u64 <= max_bytes)
.cloned())
}
}
#[tokio::main]
async fn main() -> Result<(), ModuleError> {
let reference = ArtifactRef::new("thumb_01", "image/png", 4)?;
let content = ArtifactContent {
r#ref: reference.clone(),
content: vec![0x89, 0x50, 0x4e, 0x47],
};
let store = InMemoryArtifactStore {
items: HashMap::from([(reference.id.clone(), content)]),
};
let identity = Identity::new("user-1", "user", vec!["reader".into()], HashMap::new());
let loaded = read_artifact(&store, &reference, &identity, Some(65_536)).await?;
assert!(loaded.is_some());
Ok(())
}
Conformance¶
Cross-language parity for wire shape, schema assertions, local $ref handling, and Draft 2020-12 dialect checks is pinned by conformance/fixtures/artifact_reference.json.