Skip to content

An open-source, faster Devin alternative

TypeScript

docs: restructure connected services around the order you actually do it — connecting an account was the fifth section though nothing before it works without one, so a reader following the first example got a 404; adds the 409 for an ambiguous label, a failure table, the missing OC_API_URL, a warning that the CLI command the platform's own error suggests does not exist, and puts the multi-account example inside a tool where it compiles

Brian Reardon83af80a39132
diff --git a/docs/agents/services.mdx b/docs/agents/services.mdx
index a5ad79c9..19605924 100644
--- a/docs/agents/services.mdx
+++ b/docs/agents/services.mdx
@@ -3,45 +3,60 @@ title: "Connected services"
description: "Call Gmail, Calendar, Drive, Sheets and GitHub with a credential the platform holds and refreshes"
---
-Some integrations cannot be a secret you set. An OAuth credential is
-short-lived, belongs to a person rather than to your project, and has to be
-refreshed. So the platform holds it: someone connects their account once, and
-your agent asks for a service by name instead of a URL and a header.
+Some credentials cannot be a secret you set. An OAuth token is short-lived,
+belongs to a person rather than to your project, and has to be refreshed. So
+the platform holds it: someone connects their account once, and your agent asks
+for a service by name instead of a URL and a header.
-```tsx
-import { callService, defineTool, useService } from "@opencomputer/agent";
-
-export const unread = defineTool({
- name: "unread_reports",
- description: "Count unread messages labelled triage",
- async run({ signal }) {
- const response = await callService({
- service: "gmail",
- path: "/gmail/v1/users/me/messages?q=label:triage+is:unread",
- signal,
- });
- const { messages = [] } = (await response.json()) as {
- messages?: unknown[];
- };
- return { waiting: messages.length };
- },
-});
-```
-
-The access token is never in your runtime. There is no token to log, leak, or
-hand to a model — which matters most for agents that read documents written by
-other people.
+The access token never enters your runtime — there is nothing to log, leak, or
+hand to a model.
<Note>
This is the other half of [declared connections](/agents/secrets). Use
-`defineConnection` when **you** hold the credential and it is a static secret
-the egress proxy attaches. Use `callService` when the **platform** holds it and
-has to refresh it.
+`defineConnection` when **you** hold the credential and it is a static secret.
+Use `callService` when the **platform** holds it and has to refresh it.
</Note>
-## Declare what you use
+## 1. Connect an account
+
+Do this first — the code below returns `404` until an account exists.
+
+Each account is connected under a **label**, which is how your agent addresses
+it. Mint a link and give it to the person whose account it is:
+
+```bash
+export OC_API_URL=https://app.opencomputer.dev
+
+curl -X POST "$OC_API_URL/api/managed-agents/connections/google/link" \
+ -H "x-api-key: $OPENCOMPUTER_API_KEY" \
+ -H "content-type: application/json" \
+ -d '{"service":"gmail","label":"support"}'
+```
+
+The response carries an `authorizationUrl`. Whoever opens it consents with
+their own account and it is connected under that label — they never sign in to
+OpenComputer. Omit `label` for `default`.
+
+```bash
+# What is connected
+curl "$OC_API_URL/api/managed-agents/connections" -H "x-api-key: $OPENCOMPUTER_API_KEY"
+
+# Disconnect one, by the id from the list above
+curl -X DELETE "$OC_API_URL/api/managed-agents/connections/google?service=gmail&connectionId=$ID" \
+ -H "x-api-key: $OPENCOMPUTER_API_KEY"
+```
+
+<Warning>
+There is no `opencomputer connection` CLI command. If an error message tells
+you to run one, it is wrong — use the API above.
+</Warning>
+
+## 2. Declare the service
```tsx
+import { useService, useTool } from "@opencomputer/agent";
+import { unread } from "./tools/mail.js";
+
export default function Agent() {
useService("gmail");
useTool(unread);
@@ -49,17 +64,10 @@ export default function Agent() {
}
```
-`useService` takes a literal string, read out of your source at build time — so
-it goes in the agent function, while `callService` and `listServices` go in a
-tool. An agent renders its instructions synchronously and cannot wait for a
-request, so there is nothing to await it with at render time.
-
-The declaration does two things:
-
-- It puts the service in the capability manifest, so what your agent can reach
- is reviewable without running it.
-- It asks the deployment for that grant. Without it, `listServices()` returns
- nothing, because the platform only reports accounts your deployment declared.
+`useService` takes a literal string, read out of your source at build time. It
+puts the service in the capability manifest, so what your agent can reach is
+reviewable without running it, and it asks the deployment for that grant —
+without it, `listServices()` returns nothing.
| Service | Reaches | Grant |
| --- | --- | --- |
@@ -69,19 +77,48 @@ The declaration does two things:
| `sheets` | `www.googleapis.com` | Google |
| `github` | GitHub's API | GitHub |
-The four Google services are one consent. GitHub is its own. A name outside
-that list fails the build rather than the request, so a typo is caught before
-it is deployed.
+The four Google services are one consent; GitHub is its own. A name outside
+that list fails the build rather than the request.
+
+## 3. Call it from a tool
+
+`useService` goes in the agent; `callService` goes in a tool. An agent renders
+synchronously and cannot wait for a request.
+
+```tsx
+import { callService, defineTool } from "@opencomputer/agent";
+
+export const unread = defineTool({
+ name: "unread_reports",
+ description: "Count unread messages labelled triage",
+ async run({ signal }) {
+ const response = await callService({
+ service: "gmail",
+ path: "/gmail/v1/users/me/messages?q=label:triage+is:unread",
+ signal,
+ });
+ if (!response.ok) {
+ throw new Error(`gmail: ${response.status} ${await response.text()}`);
+ }
+ const { messages = [] } = (await response.json()) as {
+ messages?: unknown[];
+ };
+ return { waiting: messages.length };
+ },
+});
+```
-## Making a request
+It returns the service's own response — status, headers and body — as `fetch`
+would, so a `403` for a missing scope stays distinguishable from a `404` for a
+deleted message. Check `response.ok`; a model told only "it failed" will retry
+forever.
-`callService` takes a service and an absolute path on it, and returns the
-service's own response — status, headers and body — as `fetch` would.
+Writes take the same shape:
```tsx
-const response = await callService({
+await callService({
service: "gmail",
- label: "support", // which connected account, when there are several
+ label: "support",
method: "POST",
path: "/gmail/v1/users/me/messages/abc/modify",
headers: { "content-type": "application/json" },
@@ -90,113 +127,91 @@ const response = await callService({
});
```
-A failure from the service arrives as a status, not an exception, so a `403`
-for a missing scope stays distinguishable from a `404` for a deleted message.
-Check `response.ok` and read the body — both are worth surfacing, because a
-scope problem is for an operator to fix and a model told only "it failed" will
-retry forever.
-
-It throws only for things that are wrong before a request is made: a path that
-is not absolute, a missing service name, or running somewhere the platform
-provides no connection layer at all.
-
-### Limits
+## 4. Several accounts
-A proxied request is not an unrestricted `fetch`, and the constraints are
-enforced by the platform rather than by your code.
-
-| | |
-| --- | --- |
-| Methods | `GET`, `POST`, `PUT`, `PATCH`, `DELETE` |
-| Request headers | Only `accept`, `content-type`, `if-match`, `if-none-match` |
-| Request body | 4 MB |
-| Response body | 4 MB |
-| Time | 30 seconds |
-
-**Other request headers are dropped, not rejected.** Anything outside that list
-is removed on the way out and the request is still sent, so an API that changes
-behaviour on a header you set will simply behave as though you had not set it.
-There is no error to catch — if a call is not doing what the service's
-documentation says it should, this is the first thing to check.
-
-Authentication headers are dropped for the same reason they are on a
-[declared connection](/agents/secrets): the credential is the platform's to
-attach, and a request that could overwrite it could also send it elsewhere.
-
-The two size limits raise an error rather than truncating, so a response you
-did not expect to be large fails loudly instead of arriving half-parsed. Page
-through large collections rather than asking for them in one call.
-
-## Several accounts
-
-An agent that sweeps more than one mailbox cannot hold their names in its
-source: accounts are connected and disconnected long after the artifact is
-built. Ask for them instead.
+Accounts are connected and disconnected long after the artifact is built, so
+ask for them rather than naming them in source.
```tsx
-const mailboxes = await listServices({ provider: "google" });
-
-for (const mailbox of mailboxes) {
- const response = await callService({
- service: "gmail",
- label: mailbox.label,
- path: "/gmail/v1/users/me/messages?q=is:unread",
- });
- // mailbox.displayName is the account's own address.
-}
+export const sweep = defineTool({
+ name: "sweep_mailboxes",
+ description: "Count unread mail in every connected mailbox",
+ async run({ signal }) {
+ const mailboxes = await listServices({ provider: "google", signal });
+ const counts: Record<string, number> = {};
+
+ for (const mailbox of mailboxes) {
+ const response = await callService({
+ service: "gmail",
+ label: mailbox.label,
+ path: "/gmail/v1/users/me/messages?q=is:unread",
+ signal,
+ });
+ const { messages = [] } = (await response.json()) as {
+ messages?: unknown[];
+ };
+ counts[mailbox.displayName ?? mailbox.label] = messages.length;
+ }
+ return counts;
+ },
+});
```
| Field | What it is |
| --- | --- |
-| `id` | The connection's id, for the disconnect route below |
+| `id` | The connection's id, for the disconnect route |
| `label` | The alias it was connected under. Pass this as `label`. |
| `displayName` | Who the account belongs to, e.g. the mailbox address |
| `provider` | `google` or `github` |
| `scopes` | What was granted at consent |
| `status` | `connected` accounts are usable |
-By default only connected accounts are returned; pass `connectedOnly: false`
-to see pending ones too. Connecting a new account needs no redeploy — the next
-run sees it, and a consent finished seconds ago is reconciled before the list
-is answered.
+Only connected accounts are returned by default; pass `connectedOnly: false`
+for pending ones too. Connecting an account needs no redeploy — the next run
+sees it, and a consent finished seconds ago is reconciled before the list is
+answered.
-## Connecting an account
+## Limits
-Each account is connected under a **label**, which is how your agent addresses
-it. Mint a link and give it to the person whose account it is:
+A proxied request is not an unrestricted `fetch`.
-```bash
-curl -X POST "$OC_API_URL/api/managed-agents/connections/google/link" \
- -H "x-api-key: $OPENCOMPUTER_API_KEY" \
- -H "content-type: application/json" \
- -d '{"service":"gmail","label":"support"}'
-```
+| | |
+| --- | --- |
+| Methods | `GET`, `POST`, `PUT`, `PATCH`, `DELETE` |
+| Request headers | Only `accept`, `content-type`, `if-match`, `if-none-match` |
+| Request body | 4 MB |
+| Response body | 4 MB |
+| Time | 30 seconds |
-The response carries an `authorizationUrl`. Whoever opens it consents with
-their own account, and it is connected under that label — they never sign in
-to OpenComputer. Omit `label` for `default`.
+**Other request headers are dropped, not rejected.** The request is still sent
+without them, so an API that changes behaviour on a header you set will act as
+though you never set it. There is no error to catch.
-```bash
-# What is connected
-curl "$OC_API_URL/api/managed-agents/connections" -H "x-api-key: $OPENCOMPUTER_API_KEY"
+Authentication headers are dropped for the reason they are on a declared
+connection: the credential is the platform's to attach, and a request that
+could overwrite it could also send it elsewhere.
-# Disconnect one
-curl -X DELETE "$OC_API_URL/api/managed-agents/connections/google?service=gmail&connectionId=$ID" \
- -H "x-api-key: $OPENCOMPUTER_API_KEY"
-```
+Both size limits raise an error rather than truncating, so an unexpectedly
+large response fails loudly instead of arriving half-parsed. Page through large
+collections rather than asking for them in one call.
+
+## When something fails
-An agent asked for a service with nothing connected gets a `404` naming the
-service and label, so the failure says what to do about it rather than looking
-like a bug in your tool.
+| You see | It means | Do this |
+| --- | --- | --- |
+| `404 connection_not_found` | Nothing connected under that label | Connect one (step 1) |
+| `409 connection_alias_required` | Several accounts connected, no `label` given | Pass `label`; the message lists them |
+| `403` from the service | The consent is narrower than the call needs | Reconnect the account with wider access |
+| `listServices()` returns `[]` | The service is not declared | Add `useService("gmail")` |
+| A header you set is ignored | It is not in the allowlist above | Nothing — it cannot be sent |
+| Throws *managed connections are unavailable* | No connection layer | You are running outside the platform |
-## What you can and cannot do
+A `403` cannot be fixed from the agent. What a request may do is fixed by the
+consent that created the connection; read `scopes` from `listServices()` to see
+what was actually granted.
-What a request is allowed to do is fixed by the consent that created the
-connection, not by your code. If a call fails with `403`, the account was
-connected with narrower access than the call needs, and the fix is to
-reconnect it — nothing in the agent can widen it. Read `scopes` from
-`listServices()` to see what was actually granted.
+## What this does not do
The platform does not proxy arbitrary hosts. A service reaches its own API and
-nothing else; for anything outside that list, declare an
+nothing else — for anything outside the table above, declare an
[HTTP connection](/agents/secrets) instead.