> ## Documentation Index
> Fetch the complete documentation index at: https://www.plain.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom Tools

> Build endpoints the Customer Agent calls to look up additional context or carry out actions to resolve customer requests.

> [!NOTE]
>
> Custom tools are in early access. To join the beta, email [support@plain.com](mailto:support@plain.com).

Custom tools let the [Customer Agent](https://www.plain.com/docs/product/agents/ari.md) look things up and take actions in your own systems, such as finding an invoice or issuing a refund. Each tool is an HTTPS endpoint you build and host. Plain calls it with a signed token naming the workspace, thread, and customer, and the Customer Agent uses the JSON you return. Plain never holds your credentials: your endpoint does the work.

## What a custom tool is

Each tool has:

* **Name**: shown on the thread timeline and in the token
* **Endpoint URL**: the HTTPS URL Plain calls
* **Read or write**: a read tool returns information, such as the customer's plan or recent invoices, and changes nothing. A write tool acts for the customer, such as a refund or a cancellation, without a person approving it.
* **When to use**: a condition in plain language, such as "Use when the customer reports a payment error"
* **Enabled**: whether the Customer Agent can call the tool. New tools start enabled.

## How the Customer Agent decides to call a tool

The Customer Agent calls an enabled tool when its **When to use** condition matches the conversation. Describe when to call it, not how it works: "Use when the customer asks for a refund". Keep eligibility rules, such as "only refund invoices from the last 30 days", in your endpoint, where the Customer Agent can't get them wrong.

Use [custom instructions](https://www.plain.com/docs/product/agents/ari/custom-instructions.md) to shape what the Customer Agent does around a tool, such as what to say when a refund isn't possible.

## Configure tools

Configure tools in **Agents → Customer Agent → Configure → Tools**, with the [GraphQL API](https://www.plain.com/docs/graphql/introduction.md), or with the [Model Context Protocol (MCP) server](https://www.plain.com/docs/integrations/mcp-server.md).

Only workspace admins can manage tools by default. A tool can send thread and customer data to any URL, so treat changes to tools like changes to your API keys.

## The request Plain sends

Plain sends a `POST` request with an empty JSON body (`{}`) and a JSON Web Token (JWT) in the `Authorization` header, as `Bearer` followed by the token. Read everything from the claims in the verified token. The Customer Agent fills in no parameters, and you can't add custom headers or claims. Plain waits 5s in total, doesn't follow redirects, and never retries a call.

### Claims in the token

The token is signed with RS256 and expires 60s after Plain issues it:

| Claim | Description |
| - | - |
| `iss` | A fixed value that identifies Plain as the sender. Check it matches the issuer Plain gives you. |
| `aud` | Your workspace ID. |
| `iat` | When Plain issued the token, in seconds since the Unix epoch. |
| `exp` | When the token expires, 60s after `iat`. |
| `jti` | A unique ID for this call, the same as `tool_request_id`. |
| `workspace_id` | Your workspace ID. |
| `thread_id` | The thread the Customer Agent is handling. |
| `channel` | The channel the thread came in on, such as `EMAIL`, `CHAT`, or `SLACK`. |
| `customer_id` | The customer the thread belongs to, which is the customer who started it unless someone on your team changed it. |
| `customer_external_id` | The customer's ID in your systems, or `null`. |
| `customer_email` | The customer's email address. |
| `tenant_id` | The ID of the thread's [account](https://www.plain.com/docs/product/platform/accounts.md), or `null`. |
| `requested_by_customer_id` | The customer who sent the latest message, which started this run of the Customer Agent. It can differ from `customer_id`, such as when a colleague replies in a shared Slack channel. |
| `additional_customer_ids` | Other customers on the thread, or an empty array. |
| `tool_name` | The name of the tool being called. |
| `tool_request_id` | A unique ID for this call. |
| `idempotency_key` | Write tools only. The same for every call of this tool for one customer message. See [write tools](#write-tools). |
| `is_test` | `true` for calls from testing a tool or from the [Playground](https://www.plain.com/docs/product/agents/ari/playground.md). Tests run on a thread you pick, and the Playground creates a test thread, so the IDs resolve. Never act on a test call. |

### IP addresses

Plain sends tool requests from the same fixed IP addresses as webhooks:

<Snippet file="shared/outbound-ip-addresses-uk.mdx" />

Every workspace shares these addresses and Plain's [mutual TLS (mTLS)](https://www.plain.com/docs/mtls.md) certificate, so use an [IP allowlist](https://www.plain.com/docs/ip-addresses.md) or mTLS only as an extra filter. The token is the only thing that authenticates a request.

## Verify every request

Tool requests don't use the hash-based message authentication code (HMAC) [request signing](https://www.plain.com/docs/request-signing.md) that webhooks use. Verify the token before you read the claims or do any work, and return `401` if any check fails:

* **Algorithm**: `alg` is `RS256`. Reject every other algorithm.
* **Signature**: a key from Plain's JSON Web Key Set (JWKS) signed the token
* **Issuer**: `iss` equals Plain's issuer
* **Audience**: `aud` is one of your workspace IDs, so a token Plain signed for another workspace fails
* **Tool**: `tool_name` is the tool this endpoint serves, so a token for another tool in your workspace fails
* **Expiry**: `exp` hasn't passed, allowing at most 30s for clock differences
* **Replay**: you haven't seen this `jti` before

Once the token passes and your own checks allow the call, check `is_test`. If it's `true`, return the response you would return, without changing anything.

Hardcode Plain's JWKS URL and issuer. Never take either from the token, since anyone can write any value into a token:

| Setting | Value |
| - | - |
| JWKS URL | Provided by the Plain team when your workspace joins the beta |
| Issuer | Provided by the Plain team when your workspace joins the beta |

Cache the keys by key ID (the `kid` in the token header), and fetch the JWKS again when a token has a `kid` you haven't seen, so a new signing key works without a change on your side.

Use a JWT library for these checks, such as [`jose`](https://github.com/panva/jose) for Node.js, rather than writing them yourself. Tell a failed check apart from an outage:

* **Failed check**: a bad signature, the wrong issuer, audience, or tool, an expired token, an unknown key ID, or a replayed `jti`. Return `401`.
* **Outage**: the JWKS URL times out, answers with a status other than `200`, or serves a malformed key set, or your store of seen `jti` values is down. Return a `5xx`, since the token didn't fail a check.

## The response your endpoint returns

Return a `2xx` status and a JSON body. The Customer Agent reads the body as data, never as instructions, and can quote any of it to the customer. Return only what everyone who can see the reply may read word for word, which can be more people than the customer, such as anyone copied on an email or everyone in a shared Slack channel. Leave out internal notes, other customers' data, and fields your team uses to make decisions. Keep it specific: the customer's last 5 invoices work better than their full billing history.

Plain calls tools on every channel and doesn't verify who sent a message. If a read tool returns the customer's own account data, such as invoices or usage, return a refusal unless `requested_by_customer_id` equals `customer_id`, and apply your own rules for sensitive data.

Plain counts any of these as an error, and the Customer Agent continues without the body:

* A status code outside the `2xx` range
* No complete response within 5s
* A body over 32 KB, or one that isn't valid JSON
* A redirect

A write tool that times out is handled differently, as described in [write tools](#write-tools). Each call shows on the thread timeline as an event.

## Write tools

The Customer Agent calls a write tool without a person approving the action. Your endpoint decides whether the action is allowed, whatever the token says: whether the customer is owed a refund and hasn't already had one, and whether the customer in `requested_by_customer_id` may act for the customer in `customer_id`.

Write tools follow these rules:

* **Once per message**: it calls each write tool at most once per customer message
* **Idempotency key**: `idempotency_key` stays the same if the Customer Agent handles the same message again. Act at most once per key.
* **Timeouts**: Plain treats a write that takes over 5s as outcome unknown, and the Customer Agent hands off to your team with a note saying the action may have happened
* **Failed replies**: if a write succeeds but the reply fails, the Customer Agent unassigns itself and leaves a note for your team
* **Misuse checks**: if Plain's check flags a message as an attempt to misuse a tool, or doesn't finish, write tools are off for that message
* **Testing**: write tools only run in the [Playground](https://www.plain.com/docs/product/agents/ari/playground.md) when you turn them on, and testing one shows a warning that the call goes to your real endpoint with `is_test` set to `true`

### Act once per idempotency key

Return every outcome, including a refusal, as a `2xx` response with the reason in the body, such as `{ "status": "not_eligible", "reason": "The invoice is over 30 days old." }`, so the Customer Agent can explain it. Handle a write in this order:

1. Verify the token, then check that `requested_by_customer_id` may act for `customer_id`. If not, return a refusal.
2. If `is_test` is `true`, return what you would do, without acting.
3. Claim the `idempotency_key` by inserting it into a table with a unique constraint, such as with `INSERT ... ON CONFLICT DO NOTHING`. If the key is already claimed, return the saved outcome, or `unknown` if there isn't one yet.
4. Act, and pass the key to the system that makes the change as its idempotency key.
5. Save the outcome against the key, and return it.

## Look up more context with the GraphQL API

Tools don't take parameters, so your endpoint fetches anything else from the [GraphQL API](https://www.plain.com/docs/graphql/introduction.md) using the IDs in the token, with an [API key](https://www.plain.com/docs/graphql/authentication.md) that has only the permissions it needs.

For example, to find an invoice ID the customer typed, [get the thread](https://www.plain.com/docs/graphql/threads/get.md) with `thread_id` and read its latest `timelineEntries`, filtered to messages. Each entry's `llmText` is the message as plain text, and its `actor` shows which customer sent it. This needs the `thread:read` and `timeline:read` permissions.

Treat what the customer typed as untrusted: before you use an ID from a message, check that it belongs to the customer in `customer_id`, or to the account in `tenant_id`.

## Limits and what isn't supported yet

During the beta, custom tools have these limits:

* **Requests**: `POST` only, with a 5s total timeout, no redirects, and no retries
* **Responses**: JSON only, up to 32 KB
* **Write tools**: at most once per customer message

These aren't supported yet:

* Parameters filled in by the Customer Agent
* Custom headers or custom claims in the token
* Other HTTP methods or ways to authenticate
* MCP servers as tools
* A list of calls or errors for each tool


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.