GenMCP.Suite.Extension behaviour (gen_mcp v0.9.0)

Copy Markdown View Source

A behaviour describing extensions to the GenMCP.Suite server.

Extensions allow modular addition of tools, resources, and prompts to the suite. They are configured via the :extensions option when plugging GenMCP.Transport.StreamableHTTP.

Lifecycle

Extensions are invoked during server initialization to gather the initial list of capabilities.

The order of invocation follows the order in the :extensions list. However, tools, resources, and prompts defined directly on the configuration (via :tools, :resources, :prompts options) are always treated as the first extension, taking precedence in the listing order.

Channel Access

Extension callbacks receive the GenMCP.Mux.Channel from the initialization HTTP request.

This allows extensions to filter or dynamically generate capabilities based on request context (e.g., user authorization).

Future implementations to support listChanged notifications will pass the channel from the current GET HTTP request streaming server notifications.

Example

defmodule MyExtension do
  @behaviour GenMCP.Suite.Extension

  @impl true
  def tools(channel, _arg) do
    if channel.assigns.admin do
      [AdminTool]
    else
      []
    end
  end

  @impl true
  def resources(_channel, _arg), do: [MyResourceRepo]

  @impl true
  def prompts(_channel, _arg), do: []
end

# In Router
plug GenMCP.Transport.StreamableHTTP,
  extensions: [{MyExtension, []}]

Summary

Callbacks

Returns a list of prompt repositories to be added to the suite.

Returns a list of resource repositories to be added to the suite.

Returns a list of tools to be added to the suite.

Functions

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

Types

arg()

@type arg() :: term()

extension()

@type extension() :: module() | {module(), arg()} | extension_descriptor()

extension_descriptor()

@type extension_descriptor() :: %{mod: module(), arg: arg()}

Callbacks

prompts(t, arg)

Returns a list of prompt repositories to be added to the suite.

Examples

def prompts(_channel, _arg), do: [MyPromptRepo]

resources(t, arg)

Returns a list of resource repositories to be added to the suite.

Examples

def resources(_channel, _arg), do: [MyResourceRepo]

tools(t, arg)

@callback tools(GenMCP.Mux.Channel.t(), arg()) :: [GenMCP.Suite.Tool.tool()]

Returns a list of tools to be added to the suite.

Examples

def tools(_channel, _arg), do: [MyTool, {AnotherTool, [opt: :val]}]

Functions

expand(extension)

@spec expand(extension()) :: extension_descriptor()

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

prompts(map, channel)

resources(map, channel)

tools(map, channel)