McpServer.URITemplate (HTTP MCP Server v0.6.0)
View SourceUtility for simple URI templates.
Supports templates with named segments using either :name or {name}.
Examples:
"/users/:id"
"/posts/{post_id}/comments/{id}"
Provides:
new/1to create a parsed template structinterpolate/2to build a URI from variablesmatch/2to check and extract variables from a uri string
Examples:
iex> tpl = McpServer.URITemplate.new("/users/:id/posts/{post}") iex> tpl.vars ["id", "post"] iex> {:ok, uri} = McpServer.URITemplate.interpolate(tpl, %{id: 42, post: "hello"}) iex> uri "/users/42/posts/hello" iex> McpServer.URITemplate.match(tpl, "/users/42/posts/hello") {:ok, %{"id" => "42", "post" => "hello"}}
Summary
Functions
Interpolate a template with a map of variables (string or atom keys).
Match a uri string against the template and return {:ok, vars_map} or :nomatch.
Create a parsed URI template from a string.
Types
Functions
Interpolate a template with a map of variables (string or atom keys).
Returns {:ok, uri} or {:error, reason} when variables are missing.
Examples:
iex> tpl = McpServer.URITemplate.new("/users/:id/posts/{post_id}")
iex> McpServer.URITemplate.interpolate(tpl, %{id: 42, post_id: 7})
{:ok, "/users/42/posts/7"}
iex> tpl = McpServer.URITemplate.new("/a/:x/b/{y}")
iex> McpServer.URITemplate.interpolate(tpl, %{"x" => "one"})
{:error, "missing variable: y"}
Match a uri string against the template and return {:ok, vars_map} or :nomatch.
Examples:
iex> tpl = McpServer.URITemplate.new("/users/:id/posts/{post}")
iex> McpServer.URITemplate.match(tpl, "/users/123/posts/abc")
{:ok, %{"id" => "123", "post" => "abc"}}
iex> tpl = McpServer.URITemplate.new("/users/:id")
iex> McpServer.URITemplate.match(tpl, "/accounts/1")
:nomatch
Create a parsed URI template from a string.
Examples:
iex> tpl = McpServer.URITemplate.new("/users/:id/profile/{section}")
iex> tpl.template
"/users/:id/profile/{section}"
iex> tpl.vars
["id", "section"]