Excessibility.MCP.Resource behaviour (Excessibility v0.14.0)

View Source

Behaviour for MCP resources.

Resources provide read-only data that can be accessed via URIs.

Example

defmodule MyApp.MCP.Resources.Config do
  @behaviour Excessibility.MCP.Resource

  @impl true
  def uri_pattern, do: "config://{name}"

  @impl true
  def name, do: "config"

  @impl true
  def description, do: "Application configuration"

  @impl true
  def mime_type, do: "application/json"

  @impl true
  def list do
    [
      %{
        "uri" => "config://app",
        "name" => "app",
        "description" => "App config",
        "mimeType" => "application/json"
      }
    ]
  end

  @impl true
  def read("config://app") do
    {:ok, Jason.encode!(%{setting: "value"})}
  end

  def read(_uri), do: {:error, "Resource not found"}
end

Callbacks

  • uri_pattern/0 - URI template pattern (e.g., "timeline://latest")
  • name/0 - Human-readable name for this resource type
  • description/0 - Description of what this resource provides
  • mime_type/0 - MIME type of the resource content
  • list/0 - Returns list of available resource instances
  • read/1 - Reads a specific resource by URI

Summary

Functions

Formats a resource read result for MCP response.

Checks if a URI matches a resource module's pattern.

Returns MCP resource definition for a resource module.

Callbacks

description()

@callback description() :: String.t()

list()

@callback list() :: [map()]

mime_type()

@callback mime_type() :: String.t()

name()

@callback name() :: String.t()

read(uri)

@callback read(uri :: String.t()) :: {:ok, String.t()} | {:error, String.t()}

uri_pattern()

@callback uri_pattern() :: String.t()

Functions

format_result(arg, uri, mime_type)

Formats a resource read result for MCP response.

matches_uri?(resource_module, uri)

Checks if a URI matches a resource module's pattern.

Supports simple patterns like:

  • "timeline://latest" (exact match)
  • "snapshot://{name}" (matches any snapshot://...)
  • "config://{type}" (matches any config://...)

to_mcp_definition(resource_module)

Returns MCP resource definition for a resource module.