McpServer.URITemplate (HTTP MCP Server v0.6.0)

View Source

Utility for simple URI templates.

Supports templates with named segments using either :name or {name}. Examples: "/users/:id" "/posts/{post_id}/comments/{id}"

Provides:

  • new/1 to create a parsed template struct
  • interpolate/2 to build a URI from variables
  • match/2 to 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

t()

@type t() :: %McpServer.URITemplate{
  segments: [any()],
  template: String.t(),
  vars: [String.t()]
}

Functions

interpolate(tpl, vars)

@spec interpolate(t(), map()) :: {:ok, String.t()} | {:error, String.t()}

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(tpl, uri)

@spec match(t(), String.t()) :: {:ok, map()} | :nomatch

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

new(template)

@spec new(String.t()) :: t()

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"]