NexusMCP.Server.Tool (NexusMCP v0.3.0)

Copy Markdown View Source

Provides the deftool macro for declaring MCP tools alongside their handlers.

Summary

Functions

Defines a tool with its schema and handler in one place.

Formats Ecto changeset errors into a human-readable error tuple.

Functions

deftool(name, description, opts_or_params \\ [], do_block \\ [])

(macro)

Defines a tool with its schema and handler in one place.

Example

deftool "get_page", "Get a page by ID",
  params: [id: {:string!, "Page ID"}] do
  page = CMS.get_page!(params["id"])
  {:ok, Map.take(page, [:id, :title, :slug])}
end

Inside the do block, params and session are bound.

Annotations

Pass annotations to provide hints about the tool's behavior to MCP clients:

deftool "delete_item", "Delete an item",
  params: [id: {:string!, "Item ID"}],
  annotations: %{readOnlyHint: false, destructiveHint: true, idempotentHint: true} do
  Items.delete!(params["id"])
  {:ok, %{deleted: true}}
end

Supported keys: readOnlyHint, destructiveHint, idempotentHint, openWorldHint, title.

format_changeset_errors(map)

Formats Ecto changeset errors into a human-readable error tuple.