MCPKit.Tool behaviour (mcp_kit v0.2.4)

Copy Markdown View Source

Behaviour and schema DSL for MCP tool modules.

A tool module declares its JSON-schema-like input contract with schema/1 and implements execute/2 to return an MCPKit.Response.

Example:

defmodule MyApp.MCP.Tools.Ping do
  use MCPKit.Tool

  alias MCPKit.Response

  schema do
    field :message, :string, required: true
  end

  def execute(arguments, context) do
    {:reply, Response.tool() |> Response.structured(arguments), context}
  end
end

Supported schema nodes today:

  • field/2
  • field/3
  • embeds_many/2
  • embeds_many/3

Summary

Callbacks

Returns the description shown in tools/list.

Executes the tool and returns an MCPKit.Response.

Returns the JSON-schema-like MCP input schema generated from schema/1.

Validates and normalizes incoming tool arguments.

Functions

Declares the MCP input schema for a tool.

Callbacks

description()

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

Returns the description shown in tools/list.

execute(map, map)

@callback execute(map(), map()) :: {:reply, MCPKit.Response.t(), map()}

Executes the tool and returns an MCPKit.Response.

The context map currently includes at least :session.

input_schema()

@callback input_schema() :: map()

Returns the JSON-schema-like MCP input schema generated from schema/1.

validate_arguments(map)

@callback validate_arguments(map()) :: {:ok, map()} | {:error, String.t()}

Validates and normalizes incoming tool arguments.

This callback is generated by schema/1 unless overridden.

Functions

schema(list)

(macro)

Declares the MCP input schema for a tool.

This macro generates input_schema/0 and validate_arguments/1 for the tool module.