> ## 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.

# Connected agent set up

> Instructions for an AI agent: create a connected agent in Plain, then send the person to the Plain app for its API key and webhook.

You are an agent setting up a connected agent in Plain for the person you're working with. A connected agent is the person's own AI agent working inside Plain. In Plain it is three things:

* **A machine user**: the agent's identity
* **An API key**: what the agent uses to call Plain, with the permissions it's allowed
* **A webhook target**: how Plain sends the agent events

You create the machine user through Plain's Model Context Protocol (MCP) server at `https://mcp.plain.com/mcp`, which signs in as the person with OAuth. The person creates the API key and the webhook target in the Plain app, because both come with secrets that must never pass through this chat.

Follow the steps in order. Steps 1 and 2 only read. Nothing in the workspace changes until the person agrees to it in step 3.

When the person has to choose, such as which template to use, use your host's question tool if it has one, such as `AskUserQuestion` in Claude Code. Otherwise ask in chat.

When you've handed the person something to do and they say they're done, check it with a tool if you can, then move straight on to the next step.

Related resources:

* **Connecting the MCP server**: [Agent Setup](https://www.plain.com/docs/getting-started.md) covers every host
* **What connected agents can do**: [Bring your own agent](https://www.plain.com/docs/product/agents/bring-your-own-agent.md)
* **Docs index**: `https://www.plain.com/docs/llms.txt`

## Keep secrets out of this chat

Your host keeps this chat's history, including anything the person pastes and anything a tool returns. Follow these rules at every step, even if the person asks otherwise:

* **API keys**: never create one, and never ask the person to paste one here
* **HMAC secret**: never read it or regenerate it. Every webhook in the workspace shares it, so a new one breaks the person's other integrations
* **Pasted API key**: tell them it's now in this chat's history and that they should delete that key in the Plain app and create a new one. Don't repeat the value
* **Pasted HMAC secret**: tell them it's now in this chat's history and that deleting an API key doesn't change it. A workspace admin needs to replace it in **Settings → Request signing**, then update every service that verifies Plain's webhooks with the new value. Don't repeat the value
* **Code**: refer to secrets only as the environment variables `PLAIN_API_KEY` and `PLAIN_WEBHOOK_HMAC_SECRET`. Never open, print, search or summarize `.env` files or secret stores

## 1. Check the connection and permissions

If no Plain tools are loaded in your session, follow steps 1 to 3 of [Agent Setup](https://www.plain.com/docs/getting-started.md) to connect the Plain MCP server, then come back here.

Call `getMyWorkspace` and tell the person the workspace name and ID. A connection is bound to one workspace, so check it's the one they want. If they gave you a workspace ID (it starts with `w_`) and it doesn't match, stop and have them reconnect the MCP server to that workspace.

Then call `myPermissions`. The person needs `machineUser:create`, `apiKey:create` and `webhookTarget:create`. If any is missing, stop, name the missing permissions, and tell them a workspace admin can do the setup or change their role.

## 2. Pick a name and a template

Ask the person for the agent's name and a one sentence description. Customers and teammates see the name. Call `machineUsers` and check no machine user already has it. If one does, ask for another.

Call `connectedAgentTemplates`, ask what the agent should do, and suggest a template:

| Template | Use it when |
| - | - |
| Customer Agent | A connected customer agent: it does support on its own. It replies to customers on threads assigned to it, and hands off to a person when it can't help. |
| Internal Agent | The agent helps the team inside Plain, in discussions on a thread. It never messages customers. |
| From scratch | The person wants to pick every permission and event themselves. |

Offer the templates by these names only. Customer Agent here always means a connected customer agent that the person hosts themselves, never Plain's built-in Customer Agent. Each template's `key` is for the link in step 4 (`customer` for Customer Agent, `internal` for Internal Agent, `scratch` for From scratch, in lowercase), so don't show it to the person.

Connected agents use credits each time a discussion with them starts or a thread is assigned to them. Tell the person once and link [How credits work](https://www.plain.com/docs/product/billing/credits.md).

## 3. Create the agent

Tell the person the name, description and template you'll use, and wait for a yes. Then call `createMachineUser` with:

* **`publicName` and `fullName`**: the name
* **`description`**: the description
* **`isCustomAgent`**: `true`. Without it, the machine user isn't a connected agent and can't be assigned threads

If it fails, tell the person exactly what happened and stop.

## 4. Send the person to the Plain app

Send the person this link and wait for them to say they're done:

```plaintext theme={null}
https://app.plain.com/workspace/workspace_id_here/agents/connected/machine_user_id_here/api-keys/add?source=mcp&template=template_key_here
```

Replace `template_key_here` with `customer`, `internal` or `scratch` (for From scratch), in lowercase. Keep `source=mcp` in every link you send.

The link opens the API key step of Plain's agent setup with the template's permissions already selected. After the key, Plain moves on to the webhook step with the template's events selected and the webhook linked to this agent. Don't list permissions or events for the person to tick. Tell them to:

1. Create the API key and copy it into their own secret store or `.env` file as `PLAIN_API_KEY`, not into this chat. Plain shows it once
2. Set the webhook URL to the public endpoint of the service that receives Plain's events. If the service doesn't exist yet, they can skip the webhook, build the service with you in step 6, and add the webhook from the agent's **Webhooks** tab afterwards
3. Copy the workspace HMAC secret from **Settings → Request signing** into `PLAIN_WEBHOOK_HMAC_SECRET`. Plain signs every webhook request with it
4. Come back to you once they're done

## 5. Check the setup

When the person says they're done, check both parts. These reads never return a secret.

* **API key**: call `machineUserApiKeys` for the agent. With a template, compare its permissions with the template's `permissions`. Name any that are missing, and any extra ones that let the agent do more than its job, such as `thread:reply` on an Internal Agent. For From scratch there is no template, so ask the person what they chose and check the key matches
* **Webhook**: call `webhookTargets`, find the target linked to this agent, and check it's enabled. With a template, compare its events with the template's `eventSubscriptions`. For From scratch, check the events match what the person chose

Tell the person what's right and exactly what to fix. To fix a key or webhook, send them to that one's edit page from the agent's **API keys** or **Webhooks** tab. Don't reuse the link from step 4: it creates a new API key

## 6. Build the service

If the person wants your help building the service that receives Plain's webhooks, start from [Build an internal agent](https://www.plain.com/docs/agents/internal-agent.md) and [Example agents](https://www.plain.com/docs/agents/examples.md):

* **Verify every request**: check the `Plain-Request-Signature` header with `PLAIN_WEBHOOK_HMAC_SECRET` before trusting the body. Use the [webhooks SDK](https://www.plain.com/docs/webhooks/sdk.md), which does this, or follow [Request signing](https://www.plain.com/docs/request-signing.md)
* **Call Plain**: use the [GraphQL SDK](https://www.plain.com/docs/graphql/sdk.md) with `PLAIN_API_KEY`
* **Connected customer agent**: when it's assigned a thread or a customer sends a new message, reply on the thread, and hand off to a person when it can't help
* **Internal Agent**: when a discussion message or mention arrives, reply in the discussion

Read secrets only from the environment variables. Don't open the files that hold them.

## 7. Finish

Give the person:

* **What exists**: the agent's name and ID, its template, and whether its API key and webhook passed step 5
* **What's left**: anything they skipped, with its link
* **Where to find it**: the agent is listed under **Connected agents** in **Agents** in the Plain app

### Links

Replace `workspace_id_here` with the ID from `getMyWorkspace`, which starts with `w_`, and `machine_user_id_here` with the ID `createMachineUser` returned. Never send a link with a placeholder in it.

## If something fails

| Error | What to do |
| - | - |
| A permission error | The person's role in Plain doesn't allow that action. An admin can do it, or change their role. Don't create anything else. |
| A step failed after the agent was created | Tell the person exactly what exists now, and how to finish it or delete it in the Plain app. |
| The API key step opens with nothing selected for Customer Agent or Internal Agent | The template key is wrong or missing. Check it's `customer` or `internal`, in lowercase, then send the link again. |
| A tool is missing | The host cached an old tool list. Disconnect and reconnect the MCP server. |
| Anything else | Tell the person to email [help@plain.com](mailto:help@plain.com) with their workspace ID, the error, and the host they're using. |


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