barrel_mcp_uri_template (barrel_mcp v2.0.2)

View Source

Minimal RFC 6570 Level-1 URI Template matcher and expander.

Used by the server's resources/read handler to route a concrete URI to a registered resource_template. We only implement Level 1 ({var} simple expansion) — that covers every template the spec's reference examples use, and it's the level the MCP server registry actually accepts.

Examples:

   match(<<"file:///etc/hosts">>, <<"file:///{path}">>).
     %% => {ok, #{<<"path">> => <<"etc/hosts">>}}
  
   match(<<"https://api.x/v1/users/42">>,
         <<"https://api.x/v1/{kind}/{id}">>).
     %% => {ok, #{<<"kind">> => <<"users">>,
     %%          <<"id">>   => <<"42">>}}
  
   expand(<<"file:///{path}">>, #{<<"path">> => <<"etc/hosts">>}).
     %% => {ok, <<"file:///etc/hosts">>}

Summary

Functions

Expand Template by substituting Vars. Returns the concrete URI or {error, {missing_var, Name}} if a variable in the template has no value in Vars.

Match Uri against Template. Returns {ok, Vars} where Vars is a binary-keyed map of substituted values, or nomatch if the URI doesn't fit the template. Variables are matched greedily but stop at literal segments (a value never crosses a literal / that the template requires after the variable).

Functions

expand(Template, Vars)

-spec expand(binary(), map()) -> {ok, binary()} | {error, term()}.

Expand Template by substituting Vars. Returns the concrete URI or {error, {missing_var, Name}} if a variable in the template has no value in Vars.

match(Uri, Template)

-spec match(binary(), binary()) -> {ok, map()} | nomatch.

Match Uri against Template. Returns {ok, Vars} where Vars is a binary-keyed map of substituted values, or nomatch if the URI doesn't fit the template. Variables are matched greedily but stop at literal segments (a value never crosses a literal / that the template requires after the variable).