Vancouver.Tool behaviour (Vancouver v0.3.0)
View SourceTools 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:
send_json/2
- sends a JSON responsesend_audio/3
- sends an audio responsesend_image/3
- sends an image responsesend_text/2
- sends a text responsesend_error/2
- sends an error response
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 audio response.
Sends an error response.
Sends image response.
Sends JSON response.
Sends text response.
Callbacks
@callback description() :: String.t()
Human readable description of what the tool does.
@callback input_schema() :: map()
JSON Schema defining the tool's input parameters.
@callback name() :: String.t()
Unique identifier for the tool.
@callback run(conn :: Plug.Conn.t(), params :: map()) :: Plug.Conn.t()
Execute the tool with the given parameters.
Functions
@spec send_audio(Plug.Conn.t(), binary(), binary()) :: Plug.Conn.t()
Sends audio response.
Examples
iex> send_audio(conn, "base64-encoded-audio-data", "audio/wav")
@spec send_error(Plug.Conn.t(), binary()) :: Plug.Conn.t()
Sends an error response.
Examples
iex> send_error(conn, "an error occurred")
@spec send_image(Plug.Conn.t(), binary(), binary()) :: Plug.Conn.t()
Sends image response.
Examples
iex> send_image(conn, "base64-encoded-data", "image/png")
@spec send_json(Plug.Conn.t(), term()) :: Plug.Conn.t()
Sends JSON response.
Examples
iex> send_json(conn, %{id: 123})
@spec send_text(Plug.Conn.t(), binary()) :: Plug.Conn.t()
Sends text response.
Examples
iex> send_text(conn, "hello")