Skip to content

Your team of AI agents, on your Mac, Windows or Linux. Agents that talk in channels, write code, review each other and use your tools, on the Claude Code, Codex, OpenCode or Pi you already have. Anything that touches the real world waits for your yes.

Set a connector's token without removing it (#80)

Rayanne B. Limaac8cad87e242
diff --git a/src/core/connectors.ts b/src/core/connectors.ts
index c0c1543..d81737a 100644
--- a/src/core/connectors.ts
+++ b/src/core/connectors.ts
@@ -96,6 +96,17 @@ export class ConnectorManager {
return id
}
+ // Sets or replaces the token of a connector that signs in with one, and connects with it.
+ async setToken(id: string, token: string): Promise<void> {
+ const c = this.deps.db.connector(id)
+ if (!c || c.kind !== 'remote' || c.auth !== 'token') throw new Error(L('Esse conector não usa token.', "This connector doesn't use a token."))
+ const value = token.trim()
+ if (!value) throw new Error(L('Cole o token.', 'Paste the token.'))
+ await this.deps.secrets.set(key(id, 'token'), value)
+ await this.live.get(id)?.connecting // an attempt under way read the old token: this one comes after it
+ await this.connect(id)
+ }
+
async remove(id: string): Promise<void> {
await this.disconnect(id)
this.live.delete(id)
@@ -170,7 +181,12 @@ export class ConnectorManager {
try {
await client.connect(transport, { timeout: CONNECT_TIMEOUT_MS })
} catch (e) {
- if (e instanceof UnauthorizedError || (e instanceof Error && /unauthorized|401/i.test(e.message))) {
+ const refused = e instanceof UnauthorizedError || (e instanceof Error && /unauthorized|401/i.test(e.message))
+ if (refused && c.auth === 'token') {
+ // With a token there is no browser to go to: the server refused it, or there is none yet.
+ await client.close().catch(() => {})
+ this.setStatus(id, 'error', L('O servidor recusou. Salve um token válido neste conector.', 'The server refused. Save a valid token on this connector.'))
+ } else if (refused) {
this.setStatus(id, 'needs_auth', interactive ? L('Conclua a autorização no navegador.', 'Finish the authorization in the browser.') : L('Precisa autorizar.', 'Needs authorization.'))
} else if (e instanceof Error && /dynamic client registration/i.test(e.message)) {
// GitHub's MCP server, for one: its OAuth only serves apps registered ahead of time, so a token is the way in.
diff --git a/src/core/index.ts b/src/core/index.ts
index cd1724f..a2e5ae2 100644
--- a/src/core/index.ts
+++ b/src/core/index.ts
@@ -903,6 +903,7 @@ const api: Api = {
removeConnector: ({ id }) => void connectors.remove(id),
reconnectConnector: ({ id }) => void connectors.connect(id),
+ setConnectorToken: ({ id, token }) => connectors.setToken(id, token),
authorizeConnector: async ({ id }) => {
const url = await connectors.authorize(id)
return serverMode && url ? { url } : {}
diff --git a/src/renderer/src/components/Pages.tsx b/src/renderer/src/components/Pages.tsx
index 7034fc4..d66c5c4 100644
--- a/src/renderer/src/components/Pages.tsx
+++ b/src/renderer/src/components/Pages.tsx
@@ -20,6 +20,7 @@ import {
addConnector,
authorizeConnector,
reconnectConnector,
+ setConnectorToken,
removeConnector,
updateTool,
updateSettings,
@@ -1036,6 +1037,30 @@ const STATUS = (): Record<Connector['status'], { label: string; tone: string }>
const address = (c: Connector): string => (c.kind === 'remote' ? (c.url ?? '') : [c.command, ...(c.args ?? [])].join(' '))
+// The token of a connector, set or replaced without removing it (which would lose the agents' access).
+function ConnectorToken({ id }: { id: string }): ReactNode {
+ const [token, setToken] = useState('')
+ const [busy, setBusy] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const save = (): void => {
+ setBusy(true)
+ setError(null)
+ setConnectorToken(id, token)
+ .then(() => setToken(''))
+ .catch((e: unknown) => setError(e instanceof Error ? e.message : String(e)))
+ .finally(() => setBusy(false))
+ }
+ return (
+ <div className="row">
+ <input className="grow" type="password" value={token} placeholder={L('Token (substitui o atual)', 'Token (replaces the current one)')} onChange={(e) => setToken(e.target.value)} />
+ <button className="btn btn--sm" disabled={busy || !token.trim()} onClick={save}>
+ {L('Salvar token', 'Save token')}
+ </button>
+ {error && <span className="note note--error">{error}</span>}
+ </div>
+ )
+}
+
export function ConnectorsPage(): ReactNode {
const connectors = useStore((s) => s.connectors)
const agents = useStore((s) => s.agents)
@@ -1106,6 +1131,7 @@ export function ConnectorsPage(): ReactNode {
</p>
)}
{c.error && <p className="note note--error">{c.error}</p>}
+ {c.kind === 'remote' && c.auth === 'token' && <ConnectorToken id={c.id} />}
<div className="row connector__actions">
{c.status === 'needs_auth' && (
<button className="btn btn--primary btn--sm" onClick={() => authorizeConnector(c.id)}>
diff --git a/src/renderer/src/store.ts b/src/renderer/src/store.ts
index 4543076..7afcf84 100644
--- a/src/renderer/src/store.ts
+++ b/src/renderer/src/store.ts
@@ -407,6 +407,7 @@ export const updateTool = (connectorId: string, name: string, patch: Partial<Pic
export const addConnector = (input: ConnectorInput): Promise<string> => call('addConnector', { input })
export const removeConnector = (id: string): void => void call('removeConnector', { id })
export const reconnectConnector = (id: string): void => void call('reconnectConnector', { id })
+export const setConnectorToken = (id: string, token: string): Promise<void> => call('setConnectorToken', { id, token })
// On a server the authorization page comes back to be opened here, then pasted back.
export const authorizeConnector = (id: string): void =>
void call('authorizeConnector', { id }).then((r) => r.url && openDialog({ kind: 'oauth', id, url: r.url }))
diff --git a/src/shared/rpc.ts b/src/shared/rpc.ts
index b9a7f57..12095f2 100644
--- a/src/shared/rpc.ts
+++ b/src/shared/rpc.ts
@@ -53,6 +53,7 @@ export interface Api {
addConnector(p: { input: ConnectorInput }): Promise<string>
removeConnector(p: { id: string }): void
reconnectConnector(p: { id: string }): void
+ setConnectorToken(p: { id: string; token: string }): Promise<void>
authorizeConnector(p: { id: string }): Promise<{ url?: string }> // url: on a server, the page to open in the person's browser
finishConnectorAuth(p: { url: string }): Promise<boolean> // on a server: the address the authorization page ended on
harnessMcps(): Promise<HarnessMcp[]> // the MCP servers set up in Claude Code, Codex and OpenCode
diff --git a/test/connectors.test.ts b/test/connectors.test.ts
index f305204..bd5cc07 100644
--- a/test/connectors.test.ts
+++ b/test/connectors.test.ts
@@ -105,3 +105,35 @@ describe('OAuth signed in from a page on another machine (Tucano as a server)',
await expect(connectors.finishOAuthFromUrl('http://127.0.0.1:1/oauth/callback?error=access_denied')).rejects.toThrow(/access_denied/)
})
})
+
+describe('a connector with a token', () => {
+ it('asks for the token, not for a browser, and takes it without being removed', async () => {
+ let seen: string | undefined
+ const srv = createServer((req, res) => {
+ seen = req.headers.authorization
+ res.writeHead(401).end()
+ })
+ await new Promise<void>((resolve) => srv.listen(0, '127.0.0.1', resolve))
+ const id = await connectors.add({ kind: 'remote', name: 'Demo', url: `http://127.0.0.1:${(srv.address() as AddressInfo).port}/mcp`, auth: 'token' })
+ await connectors.connect(id)
+ expect(connectors.get(id)).toMatchObject({ status: 'error' })
+ await connectors.setToken(id, ' abc ')
+ expect(seen).toBe('Bearer abc')
+ await expect(connectors.setToken(id, ' ')).rejects.toThrow()
+ srv.close()
+ })
+
+ it('a token saved during a connection attempt is the one tried next', async () => {
+ const seen: (string | undefined)[] = []
+ const srv = createServer((req, res) => {
+ seen.push(req.headers.authorization)
+ res.writeHead(401).end()
+ })
+ await new Promise<void>((resolve) => srv.listen(0, '127.0.0.1', resolve))
+ const id = await connectors.add({ kind: 'remote', name: 'Demo', url: `http://127.0.0.1:${(srv.address() as AddressInfo).port}/mcp`, auth: 'token' })
+ void connectors.connect(id)
+ await connectors.setToken(id, 'new')
+ expect(seen.at(-1)).toBe('Bearer new')
+ srv.close()
+ })
+})