Noizu.MCP.Server.ResourceTemplate behaviour (Noizu MCP v0.1.1)

Copy Markdown View Source

Define an MCP resource template (RFC 6570 {var} URIs) as a module.

defmodule MyApp.MCP.TableSchema do
  use Noizu.MCP.Server.ResourceTemplate,
    uri_template: "db://{table}/schema",
    name: "Table Schema",
    mime_type: "application/json"

  @impl true
  def read(_uri, %{table: table}, _ctx) do
    {:ok, MyApp.Repo.schema_json(table)}
  end

  @impl true
  def complete(:table, prefix, _ctx) do
    {:ok, Enum.filter(MyApp.Repo.tables(), &String.starts_with?(&1, prefix))}
  end

  @impl true
  def list(_ctx) do
    {:ok, Enum.map(MyApp.Repo.tables(), &%Noizu.MCP.Types.Resource{uri: "db://#{&1}/schema"})}
  end
end

use options

:uri_template (required), plus the same metadata options as Noizu.MCP.Server.Resource (:name, :title, :description, :mime_type, :annotations, :icons, :meta, :subscribable).

Callbacks

  • read/3 (required) — receives the concrete URI and the template variables, atom-keyed. Same return contract as Noizu.MCP.Server.Resource.read/2.
  • complete/3 (optional) — powers completion/complete for template variables. Return {:ok, values} or {:ok, values, has_more: true, total: n}.
  • list/1 (optional) — makes the template's instances enumerable in resources/list.

Summary

Callbacks

The wire definition advertised by resources/templates/list.

Callbacks

complete(variable, value, ctx)

(optional)
@callback complete(variable :: atom(), value :: String.t(), ctx :: Noizu.MCP.Ctx.t()) ::
  {:ok, [String.t()]} | {:ok, [String.t()], keyword()} | {:error, term()}

definition()

@callback definition() :: Noizu.MCP.Types.ResourceTemplate.t()

The wire definition advertised by resources/templates/list.

list(ctx)

(optional)
@callback list(ctx :: Noizu.MCP.Ctx.t()) ::
  {:ok, [Noizu.MCP.Types.Resource.t()]} | {:error, term()}

read(uri, vars, ctx)

@callback read(uri :: String.t(), vars :: map(), ctx :: Noizu.MCP.Ctx.t()) ::
  {:ok, term()} | {:error, term()}