matter.work
Building Apps / The SDK

Calling your tools

Call your app's own tools from its pages, with types for their arguments and results.

Your app’s tools are how its pages, views and agents get data that isn’t in Matter’s tables: the member’s connections, files on the computer they’re using, the web. A page calls one with matter.tools.call and gets what an agent would get, on the member’s computer, as the member.

src/App.tsx
import matter from '@matter.work/app-sdk'

const { structuredContent: inbox } = await matter.tools.call('list_mail', { unread: true })
for (const message of inbox.messages) console.log(message.subject)
  • Any of your tools, whether or not it’s offered anywhere. A page calls only its own app’s tools.
  • It runs the version the page is (a preview’s tools in a preview).
  • It resolves to the tool’s result: content, and structuredContent when the tool returns an object. A result that’s an error (arguments that don’t fit, an unknown tool, a handler that throws) fails with tool_error, its text as the message; tools that don’t start or answer fail with tool_failed, and an app without tools with not_found.
  • The first call after a couple of minutes without one starts your tools, which takes a moment longer than the rest.
  • Only pages call it. Your worker and tools are your own code: import the function instead.

Types#

matter app types types your tools from what matter.json declares, without building or running anything. Each tool’s name and arguments are checked, and its result is typed by its output. A tool without one gives an untyped result, so declare one for every tool a page calls:

matter.json
"tools": {
  "list_mail": {
    "description": "The member’s mail.",
    "input": { "type": "object", "properties": { "unread": { "type": "boolean" } } },
    "output": {
      "type": "object",
      "properties": {
        "messages": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": { "id": { "type": "string" }, "subject": { "type": "string" } },
            "required": ["id", "subject"]
          }
        }
      },
      "required": ["messages"]
    }
  }
}

The same types check your handlers, through defineTools:

src/tools.ts
export default defineTools({
  list_mail: async ({ unread }) => ({ messages: await mailbox.list({ unread }) }),
})

A view’s call is typed the same way: check matter.view.current.key (its tool’s name), and its arguments and result are that tool’s.

src/views/deal.ts
const view = matter.view?.current
if (view?.key === 'show_deal' && view.result && !view.result.isError) {
  const deal = view.result.structuredContent // show_deal's output
}

Declared tools, and calling them from a page, need Matter 0.5.31 or newer.