Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(backend): the connector gateway hands over rest_entry and transport, as Grid does

bubu-duduf422bbf88921
diff --git a/backend/README.md b/backend/README.md
index 546db530a..a282bda6f 100644
--- a/backend/README.md
+++ b/backend/README.md
@@ -114,7 +114,10 @@ To turn it on:
`connector_credentials` with their unique indexes. Both are kept as they are, not encrypted: the
database is private.
3. **The apps**, one row per service in `connector_apps` (`code`, `clientId`, `clientSecret`, `authUrl`,
- `tokenUrl`, `scopes`, `extra: {mcp_url, token_field, mcp_auth_header}`, `enabled`…): edited in
+ `tokenUrl`, `scopes`, `extra: {mcp_url, token_field, mcp_auth_header, rest_entry, transport}`, `enabled`…).
+ A service with no MCP server the grant can use (Gmail, Google Drive, Calendar, Figma) has `rest_entry`, its
+ REST tools: the gateway hands it over with the token, untouched, and the computer's bridge serves those
+ tools to agents as MCP, calling the service itself. The rows are edited in
Compass, or written from a file with `npm run connectors:import <config-connector-auth.json>` (the
backend's `DATABASE_URL`). The file is the Grid control plane's shape; only `auth_type: app` entries
are written. Production's are the `app` entries of Grid prod's
diff --git a/backend/src/lib/connectorGateway.ts b/backend/src/lib/connectorGateway.ts
index 7d3aef448..e7be49c78 100644
--- a/backend/src/lib/connectorGateway.ts
+++ b/backend/src/lib/connectorGateway.ts
@@ -21,6 +21,7 @@
* the tokens, it is kept as it is: this database is private.
*/
import { createHash, randomBytes, timingSafeEqual } from 'crypto'
+import type { Prisma } from '@prisma/client'
import { pub } from './bus.js'
import { prisma } from './prisma.js'
import { env } from '../config/env.js'
@@ -59,10 +60,23 @@ export interface ConnectorApp {
mcpUrl: string
/** `header:<Name>` sends the raw token under that name; anything else is `Authorization: Bearer`. */
mcpAuthHeader: string
+ /**
+ * A service with no MCP server the grant can use (Gmail with gmail.send, Drive, Calendar, Figma): its
+ * REST tools, which the computer serves to agents as MCP. Handed over verbatim, the token not filled in.
+ */
+ restEntry: Record<string, unknown> | null
+ /** `mcp`, `rest`, or `''` when linking stores a credential nothing can act on: as Grid derives it. */
+ transport: string
}
const str = (value: unknown): string => (typeof value === 'string' ? value : value == null ? '' : String(value))
+/** rest_entry and transport, as Grid reads them: the transport derived unless the config says it. */
+function wiring(rest: unknown, transport: unknown, mcpUrl: string): { restEntry: Record<string, unknown> | null, transport: string } {
+ const restEntry = rest && typeof rest === 'object' && !Array.isArray(rest) && Object.keys(rest).length ? rest as Record<string, unknown> : null
+ return { restEntry, transport: str(transport).trim().toLowerCase() || (mcpUrl ? 'mcp' : restEntry ? 'rest' : '') }
+}
+
/** The `app` entries of a config-connector-auth.json document; others sign in from the computer. */
export function parseConfig(raw: string): Record<string, ConnectorApp> {
let document: unknown
@@ -78,6 +92,7 @@ export function parseConfig(raw: string): Record<string, ConnectorApp> {
if (!str(entry.client_id) || !str(entry.auth_url) || !str(entry.token_url)) continue
const scopes = Array.isArray(entry.scopes) ? entry.scopes.map(str) : str(entry.scopes).split(/[\s,]+/)
const extra = (entry.extra && typeof entry.extra === 'object' ? entry.extra : {}) as Record<string, unknown>
+ const mcpUrl = str(entry.mcp_url) || str(extra.mcp_url)
apps[code] = {
code, label: str(entry.label) || code, description: str(entry.description), imageUrl: str(entry.image_url),
clientId: str(entry.client_id), clientSecret: str(entry.client_secret),
@@ -85,8 +100,9 @@ export function parseConfig(raw: string): Record<string, ConnectorApp> {
scopes: scopes.filter(Boolean), authStyle: str(entry.auth_style).toLowerCase(), pkce: entry.pkce === true, refresh: entry.refresh === true,
authParams: Object.fromEntries(Object.entries((entry.auth_params ?? {}) as Record<string, unknown>).map(([k, v]) => [k, str(v)])),
// As Grid reads them: these three live in `extra` (copied there from the device firmware's table).
- tokenField: str(entry.token_field) || str(extra.token_field), mcpUrl: str(entry.mcp_url) || str(extra.mcp_url),
+ tokenField: str(entry.token_field) || str(extra.token_field), mcpUrl,
mcpAuthHeader: str(entry.mcp_auth_header) || str(extra.mcp_auth_header),
+ ...wiring(entry.rest_entry ?? extra.rest_entry, entry.transport ?? extra.transport, mcpUrl),
}
}
return apps
@@ -112,6 +128,7 @@ function fromRow(row: AppRow): ConnectorApp {
scopes: row.scopes, authStyle: row.authStyle, pkce: row.pkce, refresh: row.refresh,
authParams: Object.fromEntries(Object.entries(record(row.authParams)).map(([k, v]) => [k, str(v)])),
tokenField: str(extra.token_field), mcpUrl: str(extra.mcp_url), mcpAuthHeader: str(extra.mcp_auth_header),
+ ...wiring(extra.rest_entry, extra.transport, str(extra.mcp_url)),
}
}
@@ -147,7 +164,8 @@ export async function importApps(raw: string): Promise<string[]> {
scopes: config.scopes, authStyle: config.authStyle, pkce: config.pkce, refresh: config.refresh,
authParams: config.authParams,
extra: { ...record(entry.extra), ...(config.tokenField ? { token_field: config.tokenField } : {}),
- ...(config.mcpUrl ? { mcp_url: config.mcpUrl } : {}), ...(config.mcpAuthHeader ? { mcp_auth_header: config.mcpAuthHeader } : {}) },
+ ...(config.mcpUrl ? { mcp_url: config.mcpUrl } : {}), ...(config.mcpAuthHeader ? { mcp_auth_header: config.mcpAuthHeader } : {}),
+ ...(config.restEntry ? { rest_entry: config.restEntry as Prisma.InputJsonObject } : {}), ...(str(entry.transport) ? { transport: config.transport } : {}) },
}
await prisma.connectorApp.upsert({ where: { code }, create: { code, ...fields }, update: fields })
}
@@ -169,7 +187,7 @@ export async function list(userId: string): Promise<Record<string, unknown>[]> {
const row = mine.get(a.code)
return {
code: a.code, label: a.label, description: a.description, image_url: a.imageUrl,
- auth_type: 'app', mcp_url: a.mcpUrl, refresh: a.refresh, scopes: a.scopes,
+ auth_type: 'app', mcp_url: a.mcpUrl, transport: a.transport, refresh: a.refresh, scopes: a.scopes,
status: row ? 'connected' : 'not_connected', account_name: row?.accountName ?? '',
expires_at: row?.expiresAt ?? 0, connected_at: row ? Math.floor(row.connectedAt.getTime() / 1000) : 0,
}
@@ -264,6 +282,10 @@ function tokenFor(config: ConnectorApp, payload: Record<string, unknown>, previo
const headers = custom && custom.toLowerCase() !== 'authorization' ? { [custom]: access } : { Authorization: `Bearer ${access}` }
token.mcp_entry = { url: config.mcpUrl, headers }
}
+ // Verbatim, the token not filled in (Grid's rest_entry_for_client): the computer fills it per call, so a
+ // renewal never leaves it a stale copy. A fresh copy, since the apps are cached across requests.
+ token.rest_entry = config.restEntry ? structuredClone(config.restEntry) : null
+ token.transport = config.transport
return token
}
diff --git a/backend/src/routes/connectors.test.ts b/backend/src/routes/connectors.test.ts
index 9475b1300..0e91fc0e9 100644
--- a/backend/src/routes/connectors.test.ts
+++ b/backend/src/routes/connectors.test.ts
@@ -61,7 +61,9 @@ const CONFIG = JSON.stringify({ connectors: {
token_url: 'https://github.com/login/oauth/access_token', scopes: ['repo', 'read:user'], userinfo_url: 'https://api.github.com/user', mcp_url: 'https://api.githubcopilot.com/mcp/' },
gmail: { auth_type: 'app', label: 'Gmail', client_id: 'g-client', client_secret: 'g-secret', auth_url: 'https://accounts.google.com/o/oauth2/v2/auth',
token_url: 'https://oauth2.googleapis.com/token', scopes: ['https://www.googleapis.com/auth/gmail.readonly'], refresh: true, auth_style: 'header',
- auth_params: { access_type: 'offline', prompt: 'consent' } },
+ auth_params: { access_type: 'offline', prompt: 'consent' }, transport: 'rest',
+ rest_entry: { auth: { in: 'header', name: 'Authorization', format: 'Bearer {access_token}' },
+ tools: [{ name: 'gmail_identity', request: { method: 'GET', url: 'https://www.googleapis.com/oauth2/v3/userinfo' } }] } },
notion: { auth_type: 'app', client_id: 'n', auth_url: 'https://mcp.notion.com/authorize', token_url: 'https://mcp.notion.com/token', pkce: true },
slack: { auth_type: 'app', client_id: 's', client_secret: 's-secret', auth_url: 'https://slack.com/oauth/v2/authorize', token_url: 'https://slack.com/api/oauth.v2.access',
extra: { token_field: 'authed_user.access_token', mcp_url: 'https://mcp.slack.com/mcp' } },
@@ -184,6 +186,25 @@ describe('the connector gateway', () => {
await post('/api/connectors/callback', { code: 'c', state: new URL(started.authorize_url).searchParams.get('state') }, {})
const ready = (await post('/api/connectors/poll', { pickup_code: started.pickup_code })).json().data
expect(ready).toMatchObject({ access_token: 'xoxp-user', mcp_entry: { url: 'https://mcp.slack.com/mcp', headers: { Authorization: 'Bearer xoxp-user' } } })
+ expect(ready).toMatchObject({ transport: 'mcp', rest_entry: null })
+ })
+
+ it('hands a REST-only service\'s tools over verbatim, the token not filled in, on poll and refresh (Grid\'s rest_entry)', async () => {
+ const tools = { auth: { in: 'header', name: 'Authorization', format: 'Bearer {access_token}' },
+ tools: [{ name: 'gmail_identity', request: { method: 'GET', url: 'https://www.googleapis.com/oauth2/v3/userinfo' } }] }
+ expect(mocks.appRows.get('gmail')!.extra).toEqual({ rest_entry: tools, transport: 'rest' })
+ const listed = (await app.inject({ method: 'GET', url: '/api/connectors', headers: owner })).json().data.connectors
+ expect(Object.fromEntries(listed.map((row: { code: string, transport: string }) => [row.code, row.transport]))).toMatchObject({ gmail: 'rest', github: 'mcp', notion: '' })
+ const started = (await post('/api/connectors/start', { connector: 'gmail' })).json().data
+ answer = () => Response.json({ access_token: 'ya29.a', refresh_token: 'r1', expires_in: 3600 })
+ await post('/api/connectors/callback', { code: 'c', state: new URL(started.authorize_url).searchParams.get('state') }, {})
+ const ready = (await post('/api/connectors/poll', { pickup_code: started.pickup_code })).json().data
+ expect(ready).toMatchObject({ transport: 'rest', rest_entry: tools })
+ expect(ready.mcp_entry).toBeUndefined()
+ expect(JSON.stringify(ready.rest_entry)).not.toContain('ya29')
+ answer = () => Response.json({ access_token: 'ya29.b', expires_in: 3600 })
+ const renewed = (await post('/api/connectors/refresh', { connector: 'gmail' })).json().data
+ expect(renewed).toMatchObject({ access_token: 'ya29.b', transport: 'rest', rest_entry: tools })
})
it('keeps the apps in connector_apps; a disabled one is not served', async () => {