Behaviour and compile-time convenience DSL for MCP resources.
A resource module declares either one exact :uri or one :uri_template.
The framework matches URIs against templates but does not expand them, and
authorization policy stays application-owned.
A template in the RFC 6570 shapes described in Snodo.Resource.Template
({var}, {+path}, {/seg}, {/seg*}, {?q,lang}) gets a generated
matches?/1, so it needs no matcher at all. A template outside those shapes
is a compile error that names the shape, unless the module implements
matches?/1 itself.
matches?/1 may answer in two ways. true and false route without saying
anything more. {:ok, variables}, a map of string keys to string values,
routes and hands the extracted template variables to read/2, so a
matcher never has to be paired with a second parse of the same URI. The
generated matcher uses this form. Variables may not shadow "uri" or
"_meta", which the request itself owns.
read/2 receives the request params, merged with any variables the matcher
bound, plus the immutable request context. It returns
Snodo.Result.resource_read/2 containing text or blob content maps.
defmodule PackageInfo do
use Snodo.Resource, uri_template: "hex://{name}/info", name: "package_info"
@impl true
def read(%{"name" => name}, _context), do: fetch(name)
endContent maps must contain only JSON values, which means string keys.
Snodo.JSONValue.encodable!/1 converts an atom-keyed domain value into one.
wrap: applies Snodo.Component.Wrap modules to read/2 and, when defined,
complete/2 after router checks.
Summary
Functions
Builds one base64-encoded binary resource-content map.
Returns and validates the protocol-neutral definition for a resource module.
Builds one JSON text resource-content map.
Builds one text resource-content map.
Validates all resource callbacks and static metadata.
Callbacks
@callback complete(Snodo.Completion.t(), Snodo.Context.t()) :: {:ok, Snodo.Result.t()} | {:error, Snodo.Error.t() | term()}
@callback definition() :: Snodo.Resource.Definition.t()
@callback matches?(uri :: String.t()) :: boolean() | {:ok, Snodo.Resource.Template.variables()}
@callback read(params :: map(), Snodo.Context.t()) :: {:ok, Snodo.Result.t()} | {:error, Snodo.Error.t() | term()}
Functions
Builds one base64-encoded binary resource-content map.
@spec definition(module()) :: Snodo.Resource.Definition.t()
Returns and validates the protocol-neutral definition for a resource module.
Builds one JSON text resource-content map.
Builds one text resource-content map.
@spec validate_module!(module()) :: :ok
Validates all resource callbacks and static metadata.