The ecosystem · packages/tools
Tools, made once, for any model
A tool is something a model can do through your app: a name, a description, the JSON Schema of its arguments, and the function that does it. This package holds that definition, what a tool answers, and a JSON value read a field at a time. It is plain Roc on no platform, imported by both the AI package and the MCP package.
Installing it#
Name it in your app's header, beside the packages that use it:
app [program] { ai: "../packages/ai/main.roc", crypto: "../packages/crypto/main.roc", pf: platform "../server/platform/main.roc", tools: "../packages/tools/main.roc" }Then import tools.Tool where you make tools, and import tools.JsonValue where you read JSON no record describes (keys with dashes, a field that is sometimes a number and sometimes text).
One tool, two ways in#
A tool made once works wherever a model asks for it. Given to an agent, Claude uses it from your server; given to an MCP server, an AI client (Claude, an editor) calls it over the protocol:
tools = [
Ai.tool("add_note", "Adds a note of the text given", Notes.add!, Ai.schema([])),
Ai.tool("list_notes", "Lists every note", Notes.list!, Ai.schema([])),
]
# The app's own model
keeper : Agent({})
keeper = { name: "notes_keeper", description: "Keeps the user's notes", system: "…", tools: tools }
# AI clients, over MCP: McpHttp.respond! gives its tools a McpHttp.Caller(c)
server : Mcp.Server(McpHttp.Caller({}))
server = { name: "notes", version: "1.0.0", tools: tools.map(|tool| Tool.with_context(tool, |_caller| {})) }Ai.tool, and the MCP package's McpHttp.tool and McpHttp.endpoint, write the arguments' schema from their Roc type, as the platform writes /openapi.json's. A tool checks its arguments against that type before it runs. Arguments that don't fit go back to the model as a tool error it can act on, as does the tool's own Err.
A tool is given its caller's context: the agent here gives {}. An MCP server behind McpHttp.respond! gives its tools a McpHttp.Caller(c) (the request, who is asking, progress!), so the same tools reach it through Tool.with_context, which makes a tool's context from its caller's (|_caller| {} here; |caller| caller.context passes on what the route's middleware handed on).
The modules#
| Module | What it has |
|---|---|
Tool | Tool.typed(name, description, schema, run!) reads the arguments into a Roc type and writes the answer as JSON; Tool.typed_with_params reads two types from one object (an endpoint's path params and its body); Tool.new takes any JSON and answers with content. Hints for an MCP client: read_only, additive, idempotent, closed_world. Also titled, answers(schema) and reporting; with_context, the tool for a caller whose context is another (an MCP server's); doing, what a person is shown while it runs; with_listener, for a tool that tells its caller what it does and what it cost (an agent: Tool.Listener, Tool.Ran); and Tool.Update, what a page can show meanwhile. |
Content | What a tool answers with: text, an image or a sound (bytes), a link to a resource, or a resource's contents held whole (Content.Contents). Content.to_json writes MCP's content block; the AI package writes the Messages API's from the same value. |
JsonValue | Any JSON, read a field at a time (parse, get, to_text, to_str). decode turns it into a Roc type and encode makes one from a Roc value. A number keeps the text it came as, so an id or an I64 goes back exactly. |
It depends on the crypto package (Base64, for an image's bytes), which it names itself. roc test packages/tools/main.roc runs its expects.
A tool can say what a person sees while it runs: tool.doing("Looking for your info…"). When a call asks the AI package for updates, each tool that starts is a Tool.Update its page can show (Using, with that line), and so is each note the model writes between tool calls (Progress). An agent used as a tool runs with a listener (with_listener), so its own updates reach the page too, and it answers what it cost (Tool.Ran), which its caller counts in its own.
What isn't here#
Tool.typedtakes the schema as text: the writer that makes one from a type is the server platform's, and this package names no platform.Ai.toolandMcpHttp.toolwrite it for you.- Resources and prompts are MCP's alone, so they stay in the MCP package.