5 Declarations and values
| (require mcp) | package: racket-mcp |
syntax
(tool (name [argument schema-expression] ...) body ...)
syntax
(schema name schema-expression)
syntax
(shape name [field schema-expression] ...)
syntax
(resource (name [argument schema-expression] ...) #:uri uri-expression body ...)
procedure
(text-result text) → hash?
text : string?
procedure
(structured-result value [#:content content]) → hash?
value : any/c content : list? = '()
procedure
(error-result message) → hash?
message : string?
link serializes a reference through the active mounted interface. embed resolves it through the current authorization and dispatch path. Embedding checks cycles and depth. Prompts and resources accept a #:workflow "version" clause after their metadata clauses. Their bodies then use the workflow stage grammar; a prompt ends with a message list and a resource ends with its rendered text. The ordinary constructors accept a #:workflow descriptor with #f as the unused handler. These executions share checkpoint and input persistence with tools, and are bound to the request method. When a store is configured, embedded content in tool/prompt results is captured as a persistent layout. Children resolve in source order, with separate input namespaces, retained completed results, and authorization checked on retry. The parent handler is not reconstructed after an input boundary. Resource bodies can return text-presentation with strings and embedded text resources, json-presentation with a JSON value, or binary-presentation with bytes and an explicit MIME type. These constructors produce persistable representations. Text presentation resolves nested children before concatenation; JSON is labelled application/json; binary content uses the protocol’s base64 blob field. Binary content is never implicitly decoded as text. Arbitrary host string operations are not deferred presentation forms. resource-contents returns an explicit sequence of resource presentations. Each part retains the declaration’s resolved URI and its own presentation MIME type. Empty and nested sequences are supported. Embedding such a resource preserves all parts in order: tool content expands into separate embedded-resource items, and prompt content expands into single-content messages retaining the original role. Text presentations concatenate embedded text parts without inserting a separator; binary parts require explicit conversion. JSON presentations and structured tool values represent a multi-content embedding as an array. Single content embeddings retain their existing object representation. Use binary-presentation for persistable binary parts. resource-content instead wraps an explicit JSON resource content object with a string uri, exactly one string text or blob, and an optional string mimeType. It preserves that URI and metadata, allowing a read to return multiple related resources with distinct identities.
Durable rendering stores all completed child content. Its record version is now render/3; older records fail with a version diagnostic because they may have already discarded content. Application workflow versions remain independently checked. SDK rendering markers carry a private Racket property; identical JSON objects remain ordinary data. SQLite stores marker provenance in separate columns for state, arguments, and journal events. Public JSON decoding cannot supply this provenance. No application JSON key by itself activates an embedded resource read or content-group expansion. SDK storage preserves it across process restarts; an ordinary JSON round trip intentionally does not. Storage requires sidecar columns in the existing table layout and explicit provenance for every stored JSON value. Unsupported older layouts are rejected without an automatic schema upgrade; rows lacking provenance require explicit recovery.
Rendering after a completed workflow uses a stable key bound to that workflow instance, so retrying its continuation retains completed child results.
A resource can attach a named renderer with #:render. make-text-renderer, make-json-renderer, and make-binary-renderer take a name, version, and unary procedure. The resource advertises the renderer’s MIME type; a conflicting explicit resource MIME type fails construction. Text renderers return a string or a list of text and embedded-resource parts. Binary renderers return bytes; JSON renderers return JSON values. make-resource-renderer accepts an explicit MIME type and a procedure returning a matching presentation.
Ordinary resource handlers may return host values, such as generated shape instances, which the renderer converts before persistence. Workflow completion values still require a persistable representation. Renderer expressions run once at declaration instantiation; rendering runs on demand inside the persistent embedding context. Completed parent rendering survives child suspension without reconstructing its handler or renderer. Renderer identity, version, and MIME type contribute to continuation compatibility; changing renderer code requires updating its version until complete executable build fingerprints are available.
(define integer-text (make-string-codec "integer-text" "1" Integer number->string string->number)) (prompt (counting [start Integer #:codec integer-text] [next Integer #:codec integer-text #:default (add1 start)]) (role "user" (text-content (number->string next))))
The ordinary descriptor API uses prompt-argument, which extends argument with a codec field. encode-prompt-arguments produces a wire argument object from supplied domain values without evaluating defaults. Codec identity, version, and schema contribute to durable prompt compatibility. This string-codec API supports JSON domain values; arbitrary host structures use the separate checkpoint/effect value-codec mechanism.
5.1 JSON and validation budgets
read-json counts consumed UTF-8 bytes for #:max-bytes, including multibyte characters, and rejects malformed UTF-8 without substituting replacement characters. It reads one value and preserves following input. string->json requires the remaining input to contain only JSON’s four whitespace characters.
validate and valid? accept #:max-steps (default 1,000,000) in addition to #:max-depth. Registry indexing, construction metaschema evaluation, applicator visits, and property/item traversal share the budget within a validation call. Exhaustion raises an error; it never produces a successful or ordinary invalid result. Later independent calls receive a fresh budget. This bounds counted structural work, not the CPU cost of arbitrary regex matching or large numerical operations. Registry cycles are rejected explicitly.
5.2 Schema construction checks
check-schema! validates declarations against the pinned, offline 2020-12 metaschemas and raises exn:fail:schema with schema/instance diagnostics for malformed keyword values. Its #:max-depth and #:max-nodes keywords bound the schema value graph; cyclic and non-JSON host values are rejected. Standard metaschemas cannot be replaced by application registry entries. Explicit custom dialects use the preloaded registry, and unknown required vocabularies fail. The installed package includes the pinned vendor/metaschemas resources. No schema validation operation performs network access.
Metaschema format keywords retain annotation semantics. This construction gate therefore does not establish complete URI-format or ECMA regular-expression compatibility; those remain separately documented validation limits.