Source read mapping
A read mapping is a JSON document that tells the source worker's mapped reader which tools of a protocol server list a source's documents, fetch one and show…
A read mapping is a JSON document that tells the source worker’s mapped reader which tools of a protocol server list a source’s documents, fetch one and show that one was removed. This page states the format, the three rules that set it apart from the live read’s tool mapping, what the reader answers in each case, and how a server’s results are recorded as test fixtures.
The reader is a library of the worker package. No route of the source worker calls it: a live read uses the tool mapping of the outbound MCP source client, which this format does not replace.
Source entry points
source_worker/loom_source_worker/mapping.py: theMappingmodel,load,digest(the mapping’s identity),definition_digest(the digest a tool is pinned by) andfill(an argument template with its values).source_worker/loom_source_worker/readers/mcp_mapped.py(MappedReader): lists, fetches and checks removals through aToolCaller.source_worker/loom_source_worker/sync_types.py:SourceReader,ToolCaller, the values they exchange andReaderErrorwith its codes.source_worker/qualification/record_mapped.py: the recorder, andReplay, which answers a reader from a recording.
Invariants
- The reader holds no credential and no organization. The
ToolCallerit is given is its whole reach. - A mapped tool that is missing from the server’s tool list, lacks
readOnlyHint: trueor has another digest than the pinned one refuses withmapping_changedbefore any call. Nothing is listed or fetched. - A document is never returned short. A result over a size bound, or one that holds the
mapping’s truncation marker, refuses with
too_large. - A result the mapping cannot read refuses with
protocol. No value is guessed. - Only values the mapping names reach a call: the document id, the cursor, the time and the scope, each at its placeholder.
- The reader reports no absence. A page’s
gonelist is empty, andcompleteis true only on the last page of an unfiltered listing that the mapping declares complete. removedanswersgoneonly for the mapped not-found answer, and only when the scope answered the same reader before that answer and answers it again after.
The format
load parses version 1 and refuses a key the format does not name. digest is the
SHA-256 of the mapping’s canonical JSON, so key order, whitespace and a key written as
null do not change it.
| Key | Value |
|---|---|
version |
1 |
link_origins |
The https://host[:port] origins a returned link may have |
limits |
max_result_bytes, max_text_bytes (at most 4,000,000), max_pages_per_turn |
fetch |
The tool that reads one document |
list |
The tool that lists the scope; without it the source is its selected items |
removed |
mode (listing, fetch_not_found or none) and, for fetch_not_found, not_found |
denied |
The answer that means the connection may not read the document |
fetch and list both carry tool, definition_sha256, arguments and result.
fetch key |
Meaning |
|---|---|
id_path, text_path |
Required for a JSON result; the id must equal the requested one |
title_path, link_path, revision_path |
Optional paths into a JSON result |
text_frame (start, end) |
In a text result, the document is the text between the two markers |
title_regex |
In a text result, a pattern with one group for the title |
link_template |
A link with {id} once, used when no other link is known |
truncated_marker |
Text that marks a result the server cut short |
truncated_marker is searched in the result’s text block as the server wrote it. In a
structured result it is searched in the canonical JSON of the structured content:
sorted keys, no space after : or ,, and each string JSON-escaped. A flag is written
"truncated":true, and a quote inside a string value \".
list key |
Meaning |
|---|---|
items_path |
The list of items in the result; empty when the result is the list |
id_path |
The id inside one item |
revision_path, modified_path, title_path, link_path |
Optional paths inside one item |
next_cursor_path |
The cursor of the next page; absent, null or empty on the last |
complete |
Whether a listing read to its end names every document |
changed_since |
Whether the tool can list only what changed since a time |
A result that is itself the list has no place for a cursor, so load refuses
next_cursor_path beside an empty items_path.
A path is a list of object keys. not_found and denied are matchers: is_error and
text_prefix. A result matches when its error flag equals is_error and its one text
block starts with text_prefix. load refuses a mapping whose two matchers could match
the same result, so a denial never reads as a removal.
The three rules
A tool is pinned by digest. definition_sha256 is the SHA-256 of the canonical JSON
of {name, inputSchema, outputSchema, readOnlyHint} as the server’s tool list advertises
them. The reader never interprets the schema, so a tool may use any JSON Schema keyword.
The description is left out: it changes without changing the call.
Arguments are a template. arguments is a JSON object in which a member
{"$value": "document_id"}, "cursor", "since" or "scope" is replaced by that
value, at any depth. A placeholder with no value drops its key. A fetch names
document_id and may name scope; a listing may name cursor, since and scope.
Nothing else in a call comes from the upstream or from the registration.
A result may be plain text. result is structured (the result’s structured
content), json_text (exactly one text block that parses as JSON) or text (exactly
one text block, which is the document). A listing is never text.
Listing
list_since(checkpoint, full) yields pages until the cursor runs out or
max_pages_per_turn pages were read. The scope is the registration’s one item, sent
through the scope placeholder, so one mapping serves every container of a server.
Without list, the reader yields the registration’s items as one page that is not
complete.
Each page carries a checkpoint: JSON of at most 4,096 bytes that holds the pending
cursor and the listing’s times, and no document text. A later call with that checkpoint
resumes the listing at its cursor. A cursor too long for the checkpoint refuses with
protocol.
When a listing ends and the mapping sets changed_since, the checkpoint’s since is
the newest modified time the listing saw, less 60 seconds: an upstream may list a change
a little after the time it stamps on it. since is never later than the time the
listing started, less 60 seconds. The reader reads the wall clock when a listing starts
without a cursor and carries that time in the checkpoint while a cursor is pending. The
cap covers two cases:
- A document that is edited after its page was listed, while the listing still runs or waits for its next turn. Its new time is after the listing’s start, so the next listing by time names it.
- An item stamped in the future. It would otherwise move
sincepast every later edit.
A newest modified time with no 60 seconds before it, such as a zero time sent for a time
that is not set, refuses with protocol.
A call with full false sends since through the since placeholder. A listing
filtered by time is never complete. A call with full true lists without the filter
and restarts a filtered listing that was pending.
Fetch
In a JSON result the id is read at id_path and must equal the requested id. In a
text result the id is the requested one. The other values fall back in order:
| Value | Order |
|---|---|
| Title | title_path, or title_regex on the first 4,096 bytes; the listing; the id |
| Link | link_path; the listing; link_template |
| Revision | revision_path; the listing; the SHA-256 of the text |
| Modified | The listing |
A link that is not https, or whose origin is not in link_origins, is dropped: the
document has an empty link.
Removal
removed answers gone, present or unknown and raises nothing. With
fetch_not_found it fetches the id: the not_found answer is gone, a readable
document is present. A denial, a timeout, any other error and an unreadable result are
unknown, and so is every id under the modes listing and none, which make no call.
gone also needs evidence that the scope still reads, taken after the not-found answer.
A server may answer for what a connection can no longer see as for what is not there,
and a connection may lose the scope in the middle of a turn. The reader reads again what
the scope already gave it:
- When its listing named an item, it calls the listing again, without a cursor or a time, and the first page must name an item.
- Otherwise, or when that fails, it fetches the document it read last, through
fetchor apresentanswer, and that document must still read. When that is the document asked about, the answer isunknown: the reader tries no earlier document.
A reader that has neither listed an item nor read a document answers unknown, and so
does one whose evidence no longer reads. A gone answer takes at least one call after
the fetch.
Failure codes
ReaderError carries one code and no upstream text.
| Code | Raised by the reader when |
|---|---|
mapping_changed |
A mapped tool is missing, not read-only or differs from its pinned digest |
too_large |
A result or text is over its bound, or holds the truncation marker |
denied |
A result matches denied |
protocol |
Any other error result, a not-found answer to fetch, or an unreadable result |
timeout |
A call did not answer by its deadline |
A ToolCaller raises the same error for its own failures, with one of the remaining
codes: reconnect_required, access_revoked, tool_not_allowed, server_off,
rate_limited or upstream_unavailable.
Recording fixtures
source_worker/qualification/record_mapped.py records a server through a ToolCaller,
so a person runs it where the connection lives and no credential leaves that place. CI
never runs it.
python source_worker/qualification/record_mapped.py \
--caller package.module:factory --mapping mapping.json \
--out source_worker/tests/fixtures/mapped/<server id> \
--scope <id> --empty-scope <id> --document <id> --missing <id> --truncated <id> \
--denied <id> --name "<a workspace or person name>"
factory() returns the ToolCaller. The scope must list in exactly two pages, and the
listing of what changed since must fit one page: the recorder stops otherwise, because
a recording holds one call of that listing. The recorder writes the mapped tools’
definitions (tools.json), one file a call (list_first, list_second,
list_changed, list_empty, fetch, fetch_missing, fetch_truncated,
fetch_denied), each with the call’s name, arguments and result, and expected.json:
per document its id, title, link, revision, modified time and, for the fetched one, the
SHA-256 of its text.
Before anything is written, every upstream id, every cursor and every name given with
--name is replaced by a neutral value such as doc-0001, the same wherever it recurs.
A UUID is found in either spelling; another id is replaced as written. The recorder
stops when a call still holds one of them. Tool definitions are written as advertised,
without their descriptions, because their digest is what a mapping pins. Run
make format on the written files before committing them.
source_worker/tests/test_mapping_proof.py replays the recording under
source_worker/tests/fixtures/mapped/example/, which is built by hand for a made-up
server, through Replay, which refuses a call the recording does not hold.
source_worker/tests/test_reader_contract.py holds the rules every reader keeps.