Phantom.ResourceTemplate (phantom_mcp v0.3.2)

View Source

The Model Context Protocol (MCP) provides a standardized way for servers to expose resources to clients. Resources allow servers to share data that provides context to language models, such as files, database schemas, or application-specific information. Each resource is uniquely identified by a URI.

https://modelcontextprotocol.io/specification/2025-03-26/server/resources

Summary

Functions

Build a resource_template spec

Represent a ResourceTemplate spec as json when listing the available resources to clients.

Types

json()

@type json() :: %{
  :uri => String.t(),
  :name => String.t(),
  optional(:description) => String.t(),
  optional(:mimeType) => String.t(),
  optional(:size) => pos_integer()
}

t()

@type t() :: %Phantom.ResourceTemplate{
  completion_function: atom(),
  description: String.t(),
  function: atom(),
  handler: module(),
  meta: map(),
  mime_type: String.t(),
  name: String.t(),
  path: String.t(),
  router: module(),
  scheme: String.t(),
  size: pos_integer(),
  uri: URI.t(),
  uri_template: String.t()
}

Functions

build(attrs)

@spec build(map() | Keyword.t()) :: t()

Build a resource_template spec

The Phantom.Router.resource/3 macro will build these specs.

  • :name - The name of the resource template.
  • :uri - The URI template of the resource in the style of Plug.Router, including the scheme. For example, you can define a path like "myapp:///some/path/:project_id/:id which will be parsed to include path params %{"project_id" => _, "id" => _}. The scheme can be "https", "git", "file", or custom, eg "myapp".
  • :description - The description of the resource and when to use it.
  • :handler - The module to call.
  • :function - The function to call on the handler module.
  • :completion_function - The function to call on the handler module that will provide possible completion results.
  • :mime_type - the MIME type of the results.
  • :router - The Router module that will capture the URIs and route resources by URI to functions. This is constructed by the Phantom.Router.resource/3 macro automatically as MyApp.MyMCPRouter.ResourceRouter.{Scheme}. The module does not need to exist at the time of building it-- it will be generated when added by Phantom.Cache.add_resource_template/2 or by the Phantom.Router.resource/3 macro.

to_json(resource)

@spec to_json(t()) :: json()

Represent a ResourceTemplate spec as json when listing the available resources to clients.