Skip to content

Tools

Forge tools are server-side scripts your app declares for Kortex agents. An agent calls a tool to read or change data in your app's backend, with the input checked against a schema you provide.


Overview

A tool is a script that:

  1. Receives an input object that matches the JSON Schema you declared
  2. Runs in the same isolated sandbox as a service, with platform API access
  3. Answers with a result the agent reads

Tools are declared in the tools array of ptkl.config.js and built alongside the app bundle. They run with the permissions you give the tool, not with the caller's.

Tools, services and page tools

Forge tool Service Page tool (registerTool)
Declared in tools in ptkl.config.js services in ptkl.config.js App code, at runtime
Runs In the sandbox, on the server In the sandbox, on the server In the user's browser
Called by Kortex agents HTTP clients and your views The Kortex assistant, while the user has the app open
Needs the app open No No Yes
Input Validated against input_schema Any request body Parameters schema, not validated by the platform
Caller needs tool::<name> forge::<app-uuid> service::<name> forge::<app-uuid> (platform access) Nothing extra; the user confirms by policy

Use a tool when an agent should do something in your app without a user looking at it, such as a scheduled or background run. Use a page tool from Kortex when the assistant should act on what the user sees on screen. Services stay HTTP endpoints for people and other systems; declaring a tool never changes your services.


Declaring Tools

// ptkl.config.js
export default {
    name: 'order-desk',
    version: '1.0.0',
    type: 'platform',

    tools: [
        {
            name: 'find_orders',
            description: 'Find orders by customer email. Use it to answer questions about a customer\'s orders.',
            input_schema: {
                type: 'object',
                properties: {
                    email: { type: 'string', description: 'Customer email address' },
                },
                required: ['email'],
            },
            read_only: true,
            permissions: ['read orders'],
            script: path.resolve(__dirname, 'src/tools/findOrders.ts'),
        },
        {
            name: 'cancel_order',
            description: 'Cancel an order that has not shipped yet.',
            input_schema: {
                type: 'object',
                properties: {
                    order_id: { type: 'string' },
                    reason: { type: 'string' },
                },
                required: ['order_id'],
            },
            destructive: true,
            permissions: ['read orders', 'write orders'],
            script: path.resolve(__dirname, 'src/tools/cancelOrder.ts'),
        },
    ],

    runtime_permissions: ['read orders', 'write orders'],
}

Each tool has:

Field Type Required Description
name string Yes Unique among the app's tools. Must match ^[a-z][a-z0-9_]{0,63}$: lowercase letters, digits and underscores, starting with a letter, up to 64 characters.
description string Yes 1 to 1024 characters. Say what the tool does and when to use it. The agent reads this to decide whether to call it.
input_schema object Yes A JSON Schema of type: "object" that describes the input. At most 16 KB when serialized.
read_only boolean No The tool changes nothing. Defaults to false.
destructive boolean No The tool deletes or overwrites data that is hard to get back. Defaults to false.
permissions string[] No What the tool's own identity may do. Must be a subset of the app's runtime_permissions.
script string Yes Path to the tool script source file.

Rules

  • A tool cannot be both read_only and destructive.
  • Tool names must be unique within the app.
  • permissions must be a subset of runtime_permissions. A tool gets exactly the permissions it lists and nothing else.
  • ptkl forge bundle checks all of this before it builds and stops with a message that names the tool and the rule.

Mark tools honestly. read_only and destructive decide how much oversight an agent's run gets (see How agents use tools).

Limits: Maximum 50 tools per app. The input schema is at most 16 KB.


Writing Tool Scripts

A tool script is written like a service script and runs with the same sandbox globals and libraries. These globals matter most for tools:

Global Description
input The input object, already validated against input_schema
context.toolName The name of the tool being run
context.appUuid, context.appName The app's UUID and name
response Builds the answer, as in services
$variables App variables for the environment the tool runs in

Answer with response.json(...). The agent receives that body as the tool's result. A non-2xx response.status(...) is an error: its body is the error message the agent sees.

// src/tools/cancelOrder.ts
import '@ptkl/components/forge-service'
import { Platform } from '@ptkl/sdk'

const platform = new Platform()

const order = await platform.component('orders').get(input.order_id)

if (!order?.data) {
    response.status(404).json({ error: `No order ${input.order_id}` })
} else if (order.data.status === 'shipped') {
    response.status(409).json({ error: 'The order has already shipped' })
} else {
    await platform.component('orders').update(input.order_id, {
        status: 'cancelled',
        cancel_reason: input.reason ?? null,
    })
    response.json({ order_id: input.order_id, status: 'cancelled' })
}

The platform rejects input that does not match input_schema with a 400 before your script runs, so you can rely on required fields being present. Still check values the schema cannot express. Treat all input as untrusted, as described in Security.

Write errors for the agent to read. Say what was wrong and, where you can, what to do instead.


Bundle and Inspect

ptkl forge bundle builds each tool the same way it builds a service and writes the result to dist/server/tools/<name>.js. The bundle's manifest lists each tool with its name, description, schema, marks, permissions and built script.

ptkl forge bundle --path ./order-desk --upload

List the tools of a deployed app with:

ptkl forge tools order-desk --env live

--env is dev or live. The list shows what is actually approved to run in that environment. See ptkl forge tools.

The app's page in the platform UI has a Tools tab. It lists each tool of the version you are viewing with its marks, description, permissions and input, and the permission needed to call it.

Not yet: ptkl forge dev

ptkl forge dev does not run tools. Deploy a version to your dev environment to try a tool.


Approval

Tools are part of what a revision declares. The install and update screen lists each tool with its description, its read-only or destructive mark and its permissions, next to the app's other permissions. Tool permissions count toward the set you approve, exactly like service permissions. See Approval and the app's identity.

An app that declares tools needs an Owner or a Maintainer to approve it. The app can run tools only in an environment where a revision is approved. Adding, removing or changing a tool makes a new revision that must be approved again.


Who Can Call a Tool

Each approved tool adds one permission to the project's permission catalogue:

tool::<name> forge::<app-uuid>

Add it to a role to let its members call that tool. A Maintainer holds all of them. Because an agent calls tools as the person or identity it runs as, an agent can call a tool only if that identity holds the permission. When you grant a tool to an agent, you can only choose tools you can call yourself.

A tool then runs with the app's own identity, narrowed to the tool's permissions. The caller's permission lets them call the tool. It does not widen what the tool's script can do.


How Agents Use Tools

Kortex agents use app tools like this:

  1. An admin opens the agent's Workforce settings, grants Forge app tools, and picks the app tools the agent may use. An agent sees only the tools picked for it.
  2. When the agent runs, each picked tool is offered to it with your description and input schema. Tools the agent's identity can no longer call are left out.
  3. The agent calls a tool with an input object. The platform validates it, runs your script and returns the answer.

How much oversight a call gets starts from your marks:

Tool Default
read_only: true Runs without asking.
Neither mark Asks for approval before it runs.
destructive: true Always asks for approval. An admin cannot relax this.

An admin can make any tool ask more often, or refuse it. An admin can also let a tool that is not marked destructive run without asking. The agent's settings then record who relaxed it and when, and show a warning. If a tool must never run without a person approving it, mark it destructive.


Execution Limits

Parameter Value
Tools per app 50
Input schema Max 16 KB serialized
Timeout 60 seconds
Memory 128 MB per execution
Token lifetime 2 minutes, never refreshed

A tool that runs past the timeout is stopped and the agent receives an error.

Each call runs with a token that holds only the tool's permissions and lasts two minutes, enough for the longest run. While it is valid, it appears in the app's Live sessions: under the person who called the tool, or under Agent: when an agent's run called it. You can revoke it there.


See Also