This is the other half of declared connections. Use
defineConnection when you hold the credential and it is a static secret.
Use callService when the platform holds it and has to refresh it.1. Connect an account
Do this first — the code below returns404 until an account exists.
Each account is connected under a label, which is how your agent addresses
it. Use the CLI, or the API directly if you are scripting.
Connected once the account is authorized, so
you know it worked. It does not open a browser: the account often belongs to
somebody else, and this runs on servers as readily as on a laptop. Send them
the link and leave it waiting, or press Ctrl-C — the connection is unaffected.
For minting several links at once, --no-wait prints the link and exits.
--json does the same and is the one to script against.
Omitting the label connects it as default, which is what the examples below
assume. default is a literal name, not a fallback: if you connect an
account as support, a call that passes no label looks for one called
default and gets a 404 even though support is the only account you have.
The provider segment in the URL is the grant, not the service — google for
gmail, calendar, drive and sheets; github for github.
Listing what is connected:
A connection that is not yet
connected is re-checked with the provider each
time it is listed, so pending means what it says. A connected account
is not re-checked: if someone revokes access at the provider, it keeps reading
connected until the next call to it fails. Treat connected as “was working”,
not “is working”.--service <name> picks
one.
--json if you want to parse its output.
2. Declare the service
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.
The Google services share one grant; GitHub is its own. A name outside that
list fails the build rather than the request.
There are two ways to reach GitHub, and they are for different jobs. Use
callService({ service: "github" }) to call the REST API on an account someone
connected. Use a GitHub App connection when the agent needs
to run git and gh itself — that one injects a token into the sandbox rather
than proxying requests.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.
messages.length is the size of the first page, not the total — real counting
means following nextPageToken. Most collection APIs behave this way.
callService 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.
Writes take the same shape:
4. Several accounts
Accounts are connected and disconnected long after the artifact is built, so ask for them rather than naming them in source.
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.
Limits
A proxied request is not an unrestrictedfetch.
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.
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.
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
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.