Vancouver.Tool behaviour (Vancouver v0.1.1)

View Source

Tools enable LLMs to perform actions.

You can implement a tool as follows:

defmodule MyApp.Tools.CalculateSum do
  use Vancouver.Tool

  def name, do: "calculate_sum"
  def description, do: "Add two numbers together"

  def input_schema do
    %{
      "type" => "object",
      "properties" => %{
        "a" => %{"type" => "number"},
        "b" => %{"type" => "number"}
      },
      "required" => ["a", "b"]
    }
  end

  def run(conn, %{"a" => a, "b" => b}) do
    send_text(conn, "#{a + b}")
  end
end

The input schema must be a valid JSON Schema object.

Sending responses

Tools provide helper functions to send valid MCP responses:

Summary

Callbacks

Human readable description of what the tool does.

JSON Schema defining the tool's input parameters.

Unique identifier for the tool.

Execute the tool with the given parameters.

Functions

Sends an error response.

Sends JSON response.

Sends text response.

Callbacks

description()

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

Human readable description of what the tool does.

input_schema()

@callback input_schema() :: map()

JSON Schema defining the tool's input parameters.

name()

@callback name() :: String.t()

Unique identifier for the tool.

run(conn, params)

@callback run(conn :: Plug.Conn.t(), params :: map()) :: Plug.Conn.t()

Execute the tool with the given parameters.

Functions

send_error(conn, message)

@spec send_error(Plug.Conn.t(), binary()) :: Plug.Conn.t()

Sends an error response.

Examples

iex> send_error(conn, "an error occurred")

send_json(conn, data)

@spec send_json(Plug.Conn.t(), term()) :: Plug.Conn.t()

Sends JSON response.

Examples

iex> send_json(conn, %{id: 123})

send_text(conn, text)

@spec send_text(Plug.Conn.t(), binary()) :: Plug.Conn.t()

Sends text response.

Examples

iex> send_text(conn, "hello")