Noizu.MCP.Server.Resource behaviour (Noizu MCP v0.1.2)

Copy Markdown View Source

Define an MCP resource as a module.

defmodule MyApp.MCP.Config do
  use Noizu.MCP.Server.Resource,
    uri: "config://app",
    name: "App Config",
    mime_type: "application/json",
    subscribable: true

  @impl true
  def read(_uri, _ctx), do: {:ok, Jason.encode!(MyApp.config())}
end

use options

  • :uri (required) — the resource URI
  • :name, :title, :description, :mime_type, :size, :annotations, :icons, :meta — advertised in resources/list
  • :subscribable — when true, clients may resources/subscribe to this URI and the server's resources.subscribe capability is enabled. Publish changes with MyServer.notify_resource_updated(uri).

Return values from read/2

  • {:ok, String.t()} — text contents (with the declared mime type)
  • {:ok, {:blob, binary()}} — binary contents (base64-encoded on the wire)
  • {:ok, ResourceContents.t() | [ResourceContents.t()]} — full control

  • {:error, Noizu.MCP.Error.t()} — protocol error (Noizu.MCP.Error.resource_not_found/1 for missing resources)

Summary

Callbacks

The wire definition advertised by resources/list.

Read the resource. See the module docs for return values.

Callbacks

definition()

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

The wire definition advertised by resources/list.

read(uri, ctx)

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

Read the resource. See the module docs for return values.