GenMCP.Suite.PromptRepo behaviour (gen_mcp v0.10.0)

Copy Markdown View Source

Defines the behaviour for implementing prompt repositories.

Prompts are reusable templates for LLM interactions. A repository groups related prompts under a common namespace (prefix).

Example

defmodule MyPromptRepo do
  @behaviour GenMCP.Suite.PromptRepo

  @impl true
  def prefix(_arg), do: "my_prompts"

  @impl true
  def list(_cursor, _channel, _arg) do
    prompts = [
      %{name: "greeting", description: "Say hello", arguments: []}
    ]
    {prompts, nil}
  end

  @impl true
  def get("greeting", _args, _channel, _arg) do
    result = %GenMCP.MCP.GetPromptResult{
      description: "Say hello",
      messages: [
        %GenMCP.MCP.PromptMessage{
          role: "user",
          content: %GenMCP.MCP.TextContent{type: "text", text: "Hello!"}
        }
      ]
    }
    {:ok, result}
  end

  def get(_name, _args, _channel, _arg), do: {:error, :not_found}
end

Summary

Callbacks

Retrieves a specific prompt by name with arguments.

Lists available prompts in the repository.

Returns the prefix for prompts in this repository.

Functions

Returns a descriptor for the given module or {module, arg} tuple.

Types

arg()

@type arg() :: term()

prompt_argument()

@type prompt_argument() :: %{
  :name => String.t(),
  optional(:description) => String.t(),
  optional(:required) => boolean()
}

prompt_item()

@type prompt_item() :: %{
  :name => String.t(),
  optional(:title) => String.t(),
  optional(:description) => String.t(),
  optional(:arguments) => [prompt_argument()]
}

prompt_repo()

@type prompt_repo() :: module() | {module(), arg()} | prompt_repo_descriptor()

prompt_repo_descriptor()

@type prompt_repo_descriptor() :: %{mod: module(), arg: arg(), prefix: String.t()}

Callbacks

get(name, arguments, t, arg)

@callback get(
  name :: String.t(),
  arguments :: %{required(binary()) => term()},
  GenMCP.Mux.Channel.t(),
  arg()
) :: {:ok, GenMCP.MCP.GetPromptResult.t()} | {:error, :not_found | String.t()}

Retrieves a specific prompt by name with arguments.

Arguments are passed as a map and are not automatically validated against the prompt's definition.

Examples

def get("greeting", %{"name" => name}, _channel, _arg) do
  result =
    GenMCP.MCP.get_prompt_result(
      description: "Say hello",
      assistant: "Hello #{name}, how can I help you?",
      text: "Hello, ..."
    )

  {:ok, result}
end

def get("unknown", _args, _channel, _arg) do
  {:error, :not_found}
end

list(pagination_token, t, arg)

@callback list(pagination_token :: String.t() | nil, GenMCP.Mux.Channel.t(), arg()) ::
  {[prompt_item()], next_cursor :: term() | nil}

Lists available prompts in the repository.

Supports pagination via a cursor. Returns a tuple {prompts, next_cursor}. If next_cursor is nil, there are no more pages.

Examples

def list(nil, _channel, _arg) do
  {[%{name: "prompt1"}], "page2"}
end

def list("page2", _channel, _arg) do
  {[%{name: "prompt2"}], nil}
end

prefix(arg)

@callback prefix(arg()) :: String.t()

Returns the prefix for prompts in this repository.

The prefix is used to namespace prompts to avoid collisions when multiple repositories are used.

Examples

def prefix(_arg), do: "my_app"

Functions

expand(mod)

@spec expand(prompt_repo()) :: prompt_repo_descriptor()

Returns a descriptor for the given module or {module, arg} tuple.

get_prompt(repo, name, arguments, channel)

list_prompts(repo, cursor, channel)