On this page:
tool
schema
shape
resource
text-result
structured-result
error-result
5.1 JSON and validation budgets
5.2 Schema construction checks

5 Declarations and values🔗ℹ

 (require mcp) package: racket-mcp

syntax

(tool (name [argument schema-expression] ...) body ...)

Defines a tool descriptor and registers it with the enclosing language module. Optional clauses before the body, in order, are #:description, #:output, and #:annotations. A required argument has the shown shape. Append #:optional to receive missing when absent, or #:default expression for a default. Defaults execute once, left to right, and can reference preceding arguments. JSON null is 'null; false is #f. Duplicate argument binders are expansion errors.

syntax

(schema name schema-expression)

Defines a reusable JSON Schema. Literal schemas receive expansion checks, and invalid reader literals (including regular expressions, boxes and prefab structures) produce errors at their source location. These checks also apply to argument, output, shape and workflow stage schemas. Dynamic schemas receive construction checks; arbitrary schema expressions are never evaluated by a transformer.

syntax

(shape name [field schema-expression] ...)

Produces the schema binding, a make-name constructor, name-field accessors, name->json/json->name conversions, and a name-codec binding. These generated names use the declared spelling and lexical context. The initial shape form has required fields.

syntax

(resource (name [argument schema-expression] ...) #:uri uri-expression body ...)

Calling the resource binding constructs a reference, with expansion-time arity checks. It does not read the resource. Optional clauses are #:description, #:mime-type, and #:matcher, in that order. A renderer returns a string or bytes. JSON content is rendered as labelled text, never as a new MCP content type. Simple templates receive an inverse matcher; ambiguous templates require an explicit matcher.

procedure

(text-result text) → hash?

  text : string?
Constructs a complete text tool result.

procedure

(structured-result value [#:content content]) → hash?

  value : any/c
  content : list? = '()
Constructs a structured result. Successful declared output schemas are checked before dispatch returns the result.

procedure

(error-result message) → hash?

  message : string?
Constructs an execution-error tool result.

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.

role expands several content items into the required sequence of single-content prompt messages. Prompt arguments remain strings on the wire. A declared string codec converts them to checked JSON-representable domain values:
(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))))
make-string-codec takes its name, version, domain schema, encoder, and decoder. Decoder results must satisfy both the codec and argument schemas. Missing optional arguments remain missing; defaults are domain expressions and can reference earlier decoded arguments. Defaults bypass the wire decoder. Codec expressions run once during descriptor construction. Encoding and decoding procedures should be pure; resumed requests may decode the echoed strings again.

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.