16 Binding queries for extension authors
mcp/extension supplies expansion-time binding queries. Import it with (require (for-syntax mcp/extension)) alongside the transformer libraries used by an extension. The functions must be called during syntax expansion; ordinary runtime calls fail. At higher phases, shift this import with the transformer’s other dependencies.
(lookup-mcp-binding identifier #:space ’value) returns an immutable mcp-binding-info value or #f for an unknown or ordinary host binding. The optional space is ’value or ’effect; effect queries do not confuse an adapter name with a same-spelled host variable. Queries use binding predicates and lexical metadata, without evaluating application expressions or compiling a reference to discover its value.
kind: schema, operation, resource, prompt, workflow-value, or effect. Tools and workflows share operation.
name: the original static resource/prompt name, otherwise #f.
arguments: a resource/prompt’s argument-name symbols in declaration order, otherwise #f. An empty list means a known zero-argument declaration; false means the information is unavailable.
(require-mcp-binding identifier kinds #:space ’value #:context syntax) returns the same information when its kind occurs in the nonempty kinds list. Otherwise it raises a syntax error attached to the identifier within the supplied context, which defaults to that identifier. Extension authors should pass the complete source form as context. These information values do not create bindings and contain no compiler representations. Emit the documented declaration and workflow forms to introduce bindings and retain their normal compiler checks.
The separate examples/extensions/resource-link-tool.rkt extension demonstrates this API: it requires a resource binding, derives a tool description from the original resource name, and emits an ordinary tool containing a resource link. Its generated reference still receives core arity checks and interface mapping. This API does not prove the presence of dynamically composed targets or infer references hidden in arbitrary Racket computation.
During workflow-stage extension expansion, binding queries also recognize the workflow arguments and values introduced by preceding checkpoints, input stages, effects and workflow let bindings. Visibility follows the current branch and lexical block: future values, sibling-branch values and values from a finished block are unavailable. A pending workflow value shadows a same-named outer schema or operation for these queries. Ordinary host bindings retain their own behavior. The compiler exposes this context only while expanding the stage; it does not register identifiers globally or evaluate value expressions to discover metadata.
Effect-space queries follow lexical stage order as well. A perform introduces its adapter identifier in the effect space for its input expression and subsequent stages of the same block. Earlier stages, sibling branches and code after a completed inner block cannot see an adapter introduced only there. Adapters inherited from an enclosing sequence remain visible across joins. These rules apply both to workflow extension queries and to expression macros inside handlers; adapters are not hoisted over the whole workflow. Value-space bindings with the same spelling remain separate in generated stage environments. An ordinary host let retains Racket lexical shadowing, including for binding queries in its body. Extension-generated adapters preserve their scopes, and an unresolved forward query is a source error.
examples/extensions/checkpoint-copy.rkt demonstrates the query in a separately implemented workflow extension. (checkpoint-copy [saved String] name) requires name to be an available workflow value and emits a normal checked checkpoint. Nested extensions and renamed imports retain their scopes. Caller-supplied output binders are normalized as binding positions so later stages can refer to them; macro-introduced private identifiers keep their hygiene.