relay/resources

Resources a server lists and reads: static resources and URI templates, for resources/list, resources/templates/list and resources/read.

static(uri, name, read) declares one resource. template(uri_template, name, read) declares a family of resources; its built-in matcher accepts one simple {name} variable per slash segment, including embedded forms such as {id}.png, and it panics on other syntax because templates are written in source code. try_template returns a typed error for templates built at runtime, and template_with_matcher takes an application-owned matcher for other syntax. Both kinds share the with_* setters. A read handler’s error is hidden from the client, which sees the MCP resource-not-found error.

Attach resources with relay/server.with_resources; a client reads the listings as Declaration and TemplateDeclaration values.

import relay/content
import relay/resources

pub fn readme() -> resources.Resource(Nil) {
  resources.static("memo://readme", "Readme", fn(_context, uri) {
    Ok([content.text_resource(uri, "Hello")])
  })
  |> resources.with_mime_type("text/plain")
}

Types

A static resource as resources/list publishes it. Read fields by label.

pub type Declaration {
  Declaration(
    uri: String,
    name: String,
    title: option.Option(String),
    description: option.Option(String),
    mime_type: option.Option(String),
    size: option.Option(Int),
    annotations: option.Option(content.Annotations),
    icons: List(content.Icon),
    meta: List(#(String, value.Value)),
  )
}

Constructors

A static resource or a resource template, bound to its read handler.

pub type Resource(context) =
  @internal Resource(context)

A resource template as resources/templates/list publishes it. Read fields by label.

pub type TemplateDeclaration {
  TemplateDeclaration(
    uri_template: String,
    name: String,
    title: option.Option(String),
    description: option.Option(String),
    mime_type: option.Option(String),
    annotations: option.Option(content.Annotations),
    icons: List(content.Icon),
    meta: List(#(String, value.Value)),
  )
}

Constructors

Why a URI template was refused.

pub type TemplateError {
  InvalidTemplateUri(template: String)
  UnsupportedTemplateSyntax(template: String)
}

Constructors

  • InvalidTemplateUri(template: String)

    The template has no URI scheme, or leading or trailing whitespace.

  • UnsupportedTemplateSyntax(template: String)

    The template uses syntax the built-in matcher does not support; use template_with_matcher.

Values

pub fn describe_template_error(error: TemplateError) -> String

A one-line description of a template error.

pub fn static(
  uri: String,
  name: String,
  read: fn(context, String) -> Result(
    List(content.ResourceContents),
    error,
  ),
) -> Resource(context)

A resource at one URI. read receives the context and the requested URI.

pub fn template(
  uri_template: String,
  name: String,
  read: fn(context, String) -> Result(
    List(content.ResourceContents),
    error,
  ),
) -> Resource(context)

A family of resources matched by a URI template such as "file:///notes/{id}.md". Panics on a template the built-in matcher does not support; use try_template for templates built at runtime.

pub fn template_with_matcher(
  uri_template: String,
  name: String,
  read: fn(context, String) -> Result(
    List(content.ResourceContents),
    error,
  ),
  matches: fn(String) -> Bool,
) -> Resource(context)

A resource template whose URIs an application-owned matches function recognizes, for template syntax beyond the built-in matcher. The template is published as given. Panics when it has no URI scheme.

pub fn try_template(
  uri_template: String,
  name: String,
  read: fn(context, String) -> Result(
    List(content.ResourceContents),
    error,
  ),
) -> Result(Resource(context), TemplateError)

template for templates built from runtime data.

pub fn with_annotations(
  resource: Resource(context),
  annotations: content.Annotations,
) -> Resource(context)

Sets the annotations.

pub fn with_description(
  resource: Resource(context),
  description: String,
) -> Resource(context)

Sets the description.

pub fn with_icons(
  resource: Resource(context),
  icons: List(content.Icon),
) -> Resource(context)

Replaces the icons.

pub fn with_meta(
  resource: Resource(context),
  meta: List(#(String, value.Value)),
) -> Resource(context)

Replaces the _meta members.

pub fn with_mime_type(
  resource: Resource(context),
  mime_type: String,
) -> Resource(context)

Sets the MIME type of the contents.

pub fn with_size(
  resource: Resource(context),
  size: Int,
) -> Resource(context)

Sets the size in bytes of a static resource. Templates publish no size.

pub fn with_title(
  resource: Resource(context),
  title: String,
) -> Resource(context)

Sets the human-readable title.

✨ Search Document