# Use CMDOP from your product

Your product's backend can run tools on your machines over HTTPS, the same calls an MCP agent
makes, under the same grant and ceiling.

**Preview.** The `/api/v1` routes with agent tokens ship with the next server release. The SDKs are not published yet.

**In short:** One agent per product and environment, its token on your server only;
`POST https://<contour>.cmdop.ai/api/v1/call` per tool call, `/api/v1/call/stream` for long output.

## Before you start

A first call working from your machine: [quick start for a developer](https://docs.cmdop.ai/start/quickstart-developer.md).

## Steps

1. **Add one agent per product and environment** on the Agents tab (`shop-staging`,
   `shop-prod`). Each call is recorded under that name, and you can revoke one without touching the
   others.
2. **Grant only what the product needs.** A product that reads logs needs `read`; one that deploys
   needs `exec` too. Cap each machine with its [ceiling](https://docs.cmdop.ai/guides/access/ceiling.md) as well.
3. **Keep the token on your server.** Read it from the environment or your secret store. Never send
   it to a browser or a mobile app, never put it in a URL, and never log it.
4. **Call tools from the backend.** Find the machine with
   [`GET /api/v1/machines`](https://docs.cmdop.ai/reference/http/get-machines.md), the tools and their schemas with
   [`GET /api/v1/tools`](https://docs.cmdop.ai/reference/http/get-tools.md), then call with
   [`POST /api/v1/call`](https://docs.cmdop.ai/reference/http/post-call.md):

   ```ts
   async function callTool(machine: string, tool: string, args: object) {
     const res = await fetch(`https://${CONTOUR}.cmdop.ai/api/v1/call`, {
       method: "POST",
       headers: { Authorization: `Bearer ${process.env.CMDOP_TOKEN}`, "Content-Type": "application/json" },
       body: JSON.stringify({ machine, tool, args }),
     });
     const answer = (await res.json()) as { ok?: unknown; error?: { code: string; message: string } };
     if (answer.error) throw new CmdopError(answer.error.code, answer.error.message);
     return answer.ok;
   }
   ```

5. **Stream long output** with [`POST /api/v1/call/stream`](https://docs.cmdop.ai/reference/http/post-call-stream.md):
   newline-delimited JSON, one `{"chunk": {"stream", "data"}, "seq": n}` line per piece of output,
   then one last line shaped like the `/api/v1/call` answer. The status is 200 once the call is
   sent; how it ended is in the last line. For work longer than ten minutes, start a
   [task](https://docs.cmdop.ai/guides/run.md#start-a-long-task) and poll its output instead.

## Act on errors by code

| Code | Your backend should |
|---|---|
| [`busy`](https://docs.cmdop.ai/reference/errors/busy.md), [`rate_limited`](https://docs.cmdop.ai/reference/errors/rate_limited.md) | wait and retry, with backoff |
| [`timed_out`](https://docs.cmdop.ai/reference/errors/timed_out.md) | retry only if the tool is safe to repeat; for long work use a task |
| [`denied`](https://docs.cmdop.ai/reference/errors/denied.md) | stop; the owner must change the grant or the ceiling |
| [`unauthorized`](https://docs.cmdop.ai/reference/errors/unauthorized.md) | stop; the token was revoked or is wrong |
| [`paused`](https://docs.cmdop.ai/reference/errors/paused.md), [`needs_presence`](https://docs.cmdop.ai/reference/errors/needs_presence.md) | stop; a person must act at the machine |
| [`bad_args`](https://docs.cmdop.ai/reference/errors/bad_args.md) | fix the call against the tool's schema |

Every code: [Errors](https://docs.cmdop.ai/reference/errors.md).

## Check

Your product's calls appear under its agent name in the record on your page, and only with the
risk classes you granted it.

## Related

[HTTP API](https://docs.cmdop.ai/reference/http.md) · [Tokens](https://docs.cmdop.ai/concepts/tokens.md) · [Security model](https://docs.cmdop.ai/concepts/security-model.md)
