Skip to main content

Use a person's outside accounts

An agent calls Google, GitHub or Slack for a person and never holds their credentials. LumoAuth keeps the connection and hands out short-lived tokens.

RecipeDraftView source
This page is a draft. The code exists, and not all of it has been run for this page yet. The page says what was checked.
Works with
JavaScript / TypeScriptPythonPlain HTTP

What it shows​

  1. An administrator adds a connection from the template "Google Calendar". The connection holds the OAuth app of your organization at Google.
  2. A person links their Google account in their LumoAuth account, and picks the agent "Calendar assistant" as the one agent that may use it.
  3. The agent asks LumoAuth for a token of that connection, for that person.
  4. The agent calls the Google Calendar API with the token.

The agent never sees the Google password or the refresh token of the person. LumoAuth keeps the refresh token, and hands out access tokens that expire soon.

Before you start​

  • An OAuth app at Google with the Google Calendar API, and its client ID and secret.
  • The agent "Calendar assistant" with the switch May act on behalf of users. The setup script below registers it.
  • Check that your plan includes agent connections in the plans matrix.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_API_KEYFor the setup script only: an API key with the scopes admin:permissions:read, admin:permissions:write, admin:clients:read, admin:clients:write, admin:agents:read and admin:agents:write. The agent does not read it.
AGENT_CLIENT_IDThe client ID of the agent "Calendar assistant". The setup script prints it.
AGENT_CLIENT_SECRETThe client secret of the agent. The setup script prints it.
CONNECTION_IDThe ID of the connection (conn_…), from its page in the portal.
PERSONThe email address or the user ID of the person who linked their Google account, for example jordan.lee@example.com.

The setup of the recipe for acting for a person registers the agent "Calendar assistant", which may act for people. The connection and the link are made in the portal and in the account of the person.

node delegation/act-for-a-user/setup.mjs
../act-for-a-user/setup.mjs
// Creates what the recipe needs: two scopes for a calendar, the application
// through which a person signs in and allows them, and an agent that may act
// for people. Run it again at any time.
//
// node delegation/act-for-a-user/setup.mjs
//
// The API key needs the scopes admin:permissions:read, admin:permissions:write,
// admin:clients:read, admin:clients:write, admin:agents:read and
// admin:agents:write.
import { call, ensure, printEnv } from '../../_lib/admin.mjs';

for (const [name, description] of [
['calendar:read', 'Read the events of your calendar.'],
['calendar:write', 'Add and change events in your calendar.'],
]) {
await ensure('/admin/scopes', (s) => s.name === name, { name, description });
}

// The person signs in here and allows both scopes. An agent can never get
// more than the person allowed.
const redirectUris = [`http://localhost:${process.env.PORT ?? 3000}/auth/callback`];
const scopes = ['openid', 'profile', 'email', 'offline_access', 'calendar:read', 'calendar:write'];
const app = await ensure('/admin/clients', (c) => c.name === 'Acme Calendar', {
name: 'Acme Calendar',
redirectUris,
isConfidential: false,
scopes,
});
// A second run with another port replaces the redirect address.
await call('PATCH', `/admin/clients/${app.clientId}`, { redirectUris, scopes });

const agent = await ensure('/admin/agents', (a) => a.name === 'Calendar assistant', {
name: 'Calendar assistant',
description: 'Reads the calendar of a person and suggests times for meetings.',
identityType: 'client_secret',
// The agent may only read, whatever the person allowed.
capabilities: ['calendar:read'],
// The switch "May act on behalf of users" of the portal.
canActOnBehalfOfUsers: true,
});

// LumoAuth shows a secret once, in the answer that creates it. On a second
// run the script therefore asks for a new secret, and the old one stops.
const secret =
agent.clientSecret ??
(await call('POST', `/admin/agents/${agent.agentId}/rotate-credentials`)).data.clientSecret;

console.log(`The application "${app.name}" and the agent "${agent.name}" are ready.`);
printEnv({ LUMOAUTH_CLIENT_ID: app.clientId, AGENT_CLIENT_ID: agent.agentId, AGENT_CLIENT_SECRET: secret });

Add the connection in the portal:

  1. Open AI Agents, Agents, the tab Connections, and click Add connection.
  2. Enter a Connection name, for example "Google Calendar (Calendar assistant)", and select the Provider template "Google Calendar".
  3. Under Whose account is used, select User-delegated.
  4. Paste the Client ID and the Client secret of your Google app. Leave Scopes empty: the template asks for calendar.readonly.
  5. Under Agent access, keep Only selected agents, and select "Calendar assistant" in Permitted agents. Click Add Connection.
  6. The page of the connection shows the Connection ID (conn_…) and the Callback URL. Add the callback URL to the redirect URIs of your Google app.

The person links their account:

  1. As the person, open https://app.lumoauth.dev/account/connections and sign in. The page is Accounts agents can use.
  2. On the card of the connection, tick "Calendar assistant" under Agents that may use this account once linked, and click Link account.
  3. Google shows its consent screen. After the consent, Google sends the person back to LumoAuth, which stores the link.

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. The first line was seen with 0 linked accounts. Before a link, the token request answers grant_not_found, and for an agent that the connection does not name, agent_not_allowed. The lines after it follow the documented answer and were not seen.

delegation/connected-accounts/js/vault.mjs
// An agent gets a Google Calendar token of one person from the Token Vault,
// and reads the calendars of that person.
//
// node delegation/connected-accounts/js/vault.mjs
import { LumoAgent } from '@lumoauth/agent';

const api = `${process.env.LUMOAUTH_URL ?? 'https://app.lumoauth.dev'}/orgs/${process.env.LUMOAUTH_ORG_ID}/api/v1`;

// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
// The agent never holds the Google credentials of the person.
const agent = new LumoAgent();
const headers = { Authorization: `Bearer ${await agent.getAccessToken()}`, 'Content-Type': 'application/json' };

// The connections that this agent may use.
const { connections } = await (await fetch(`${api}/agents/me/connections`, { headers })).json();
for (const c of connections) console.log(`connection: ${c.name} (${c.provider}), ${c.linked_users} linked`);

// A short-lived token of the connection, for one person. LumoAuth keeps the
// refresh token, and refreshes the access token when needed.
const person = process.env.PERSON;
const answer = await fetch(`${api}/agents/me/connections/${process.env.CONNECTION_ID}/token`, {
method: 'POST',
headers,
body: JSON.stringify({ user_id: person }),
});
const token = await answer.json();
if (!answer.ok) {
console.log(`token for ${person}: refused (${token.error})`);
process.exit(1);
}
console.log(`token for ${person}: expires ${token.expires_at}`);

// The agent calls Google with the token of the person.
const calendars = await fetch('https://www.googleapis.com/calendar/v3/users/me/calendarList', {
headers: { Authorization: `Bearer ${token.access_token}` },
});
console.log(`calendars: ${(await calendars.json()).items.length}`);
node delegation/connected-accounts/js/vault.mjs

Compiles. Not run against an organization. The LumoAuth side ran against an organization, up to the answer that the person has not linked Google. No Google OAuth app was available, so no account was linked and Google was not called.

JavaScript and the curl script call the two endpoints of the agent directly: no agent SDK has a call for them. Python uses LumoVaultClient of the FastMCP package, which also works outside an MCP server.

Check the result​

Output
connection: Google Calendar (Calendar assistant) (google_calendar), 1 linked
token for jordan.lee@example.com: expires 2026-11-28T03:34:00+00:00
calendars: 3
  • Before the person links the account, the token request fails with grant_not_found.
  • An agent that the connection does not name gets agent_not_allowed, and the connection is not in its list.
  • After Unlink on the card, or after the person takes the agent off the account, LumoAuth refuses the next token request.
  • The page of the connection in the portal lists the grant of the person, with the agents that may use it.

Limits​

  • The person decides, agent by agent, who may use the account. A link with no agent ticked is usable by nobody.
  • A token is issued only to an agent with May act on behalf of users, and only for a person who is active and not blocked.
  • The token request needs the full token of the agent. A token that was narrowed with scope gets insufficient_scope.
  • LumoAuth allows 60 token requests in a minute for each agent.
  • The addresses of a custom provider must be public HTTPS addresses.
  • To use an outside account inside an MCP tool, see Call an outside API from a tool.