Skip to content

Building a connector

A connector is the connection point to one kind of source (ADR 0016). With @academy/connector-sdk it is a manifest and four functions; the SDK talks to the portal (protocol): channel and reconnects, configuration, schedule, batches, uploads, deletions, reports.

connectors/acme-wiki/
manifest.json what it is, what it needs (settings), what it can do
src/connector.ts defineConnector({ manifest, test, browse, changes, read })
src/main.ts await runFromEnvironment(acmeWiki)
test/… against a stand-in of the source
package.json, tsconfig.json, tsup.config.ts, Dockerfile (copy from connectors/fileshare)
import { ConnectorError } from "@academy/connector-protocol";
import { defineConnector, type Change } from "@academy/connector-sdk";
import manifest from "../manifest.json" with { type: "json" };
export const acmeWiki = defineConnector({
manifest,
// Settings typed in the portal (merged with local ones): reach the source, say what you reached.
async test(settings) {
return { account: `${settings.baseUrl}` };
},
// For path fields with "browse": list what is under `path` (null: the top).
async browse(settings, sourceSettings, field, path) {
return { path, entries: [{ name: "Handbook", path: "HB", kind: "folder", hasChildren: true }] };
},
// Everything in the source, or (manifest: "delta": true) what changed since `cursor`.
// Return the next cursor. Without delta, what isn't listed counts as deleted.
async *changes(ctx, cursor): AsyncGenerator<Change, string | null, void> {
yield { type: "upsert", item: { externalId: "42", path: "/HB/Travel", title: "Travel", etag: "v7", link: "https://wiki/42" } };
yield { type: "delete", externalId: "41" };
return "next-cursor";
},
// One item's content as a file: text files go as Markdown, others to Docling or the portal.
async read(ctx, externalId) {
return { stream, name: "travel.md", mime: "text/markdown" };
},
// Optional: prepare content yourself (code, a transcript …) instead of a file.
async describe(ctx, item) {
return { type: "document", version: 1, markdown: "# Travel\n\n…" };
},
// Optional (capability "notifications"): register the source's change notifications to `url`,
// or renew `current`. Called when there is none or it ends within two days.
async subscribe(ctx, { url, clientState, current }) {
return { id: "sub-1", expiresAt: new Date(Date.now() + 29 * 86_400_000).toISOString() };
},
// Optional (capability "query"): search the source live, with the connector's own access.
async query(ctx, q, limit) {
return [{ externalId: "42", title: "Travel", path: "/HB/Travel", link: "https://wiki/42", snippet: "… per diem …", modifiedAt: null }];
},
});

Permissions (capability permissions): put who may read each item on what changes yields, as access (protocol):

yield {
type: "upsert",
item: {
externalId: "42", path: "/HB/Travel", title: "Travel", etag: "v7", restricted: true,
access: { readers: [{ kind: "group", system: "acme_wiki", id: "hr", name: "HR" }], labels: [{ system: "acme_wiki", id: "confidential", name: "Confidential" }] },
},
};

Name people by address (system: "email") and groups by the directory’s ids (entra, google, ad) wherever the source knows them: those the portal matches with people itself. Groups of the source’s own are given to teams by the organisation’s administrators. Leave access out when nothing changed, and set it to null when the source can’t tell.

  • ctx.settings: the connector’s settings; ctx.sourceSettings: the source’s, checked against the manifest (defaults filled in); ctx.log.
  • Throw ConnectorError(code, message) for what the portal should show: settings_invalid, source_unreachable, source_denied, path_outside_root, ….
  • Read only what the source’s settings point at; refuse paths outside them (path_outside_root).
  • A connector the portal may run (hostable in its catalog) must reach the network through reachFetch(url, init, reach): it checks every address and redirect against private networks.
  • Include/exclude settings: pathFilter(include, exclude) (globs: **, *, ?, {a,b}).
Terminal window
corepack pnpm --filter @academy/connector-acme-wiki build
PORTAL_URL=http://localhost:4000 CONNECTOR_TOKEN=ck_… CONNECTOR_API_TOKEN=… node connectors/acme-wiki/dist/main.js

Test against a stand-in of the source (node:http), as connectors/sharepoint/test does with Microsoft Graph. Add the manifest to the portal’s catalog (apps/api/src/knowledge/catalog.ts) to have it listed before installation, and to the connector host (apps/connector-host) if the portal should run it.