Cosmo Docs
Server

Connectors Module

MCP connector instances, credentials, and provider registry

The Connectors module manages per-org MCP integration instances and the global connector provider catalog. Source: packages/server/src/modules/connectors/ and packages/server/src/modules/connector-providers/.

Connectors REST Endpoints

All endpoints are prefixed with /api/v1/connectors and require ClerkAuthGuard.

MethodPathDescription
GET/connectorsList org's connector instances
POST/connectorsCreate a connector instance
GET/connectors/:instanceId/credentialsGet credentials for an instance
POST/connectors/:instanceId/credentialsUpdate credentials
DELETE/connectors/:instanceIdDelete a connector instance

Connector Providers REST Endpoints

MethodPathGuardsDescription
GET/connector-providersNoneList enabled providers (public)
PUT/connector-providersClerkAuthGuardCreate or update a provider (admin)
DELETE/connector-providers/:serverIdClerkAuthGuardDelete a provider (admin)

Connectors Service (services/connectors.service.ts)

list(orgId, userId?)

Returns the org's connector instances. The userId param applies for per-user auth connectors (e.g. Microsoft 365):

  • For perUserAuth: true providers: returns only instances belonging to the requesting user.
  • For org-shared providers: returns all org-level instances.

Hosting override logic:

  • REMOTE_OAUTH and NONE connectors: hosting is set to LOCAL in the response (the desktop manages these locally).
  • All others: hosting is CLOUD (server manages credentials).

create(orgId, userId, dto)

Creates a new MCPConfig record. Input:

FieldRequiredDescription
serverIdYesProvider ID (e.g. "notion")
instanceIdYesClient-generated unique ID
labelYesHuman-readable name
hostingNoCLOUD / LOCAL
transportNoSTDIO / SSE / HTTP
configNoNon-sensitive config JSON
credentialsNoInitial credentials JSON

If the provider has perUserAuth: true, userId is stored on the record (per-user scoping). Otherwise userId is null (org-shared).

Uniqueness constraint: (orgId, userId, instanceId).

getCredentials(orgId, instanceId, userId?)

Fetches the credentials JSON for a connector. For per-user connectors, userId must match the record's user.

updateCredentials(orgId, instanceId, credentials, userId)

Replaces the credentials JSON and updates status to CONNECTED and lastUsedAt to now.

Connector Providers Service (services/connector-providers.service.ts)

listPublic()

Returns all providers where enabled: true, with sensitive fields stripped:

  • clientSecret is not included.
  • authParams with secrets are excluded.

Returned fields: serverId, displayName, description, iconUrl, category, authMode, perUserAuth, requiresAdminConsent, mcpTransport.

getOAuthConfig(serverId)

Internal method (not exposed via REST). Returns the full provider record including clientId, clientSecret, authUrl, tokenUrl, scopes. Used by MCPOAuthService.

getMCPConfig(serverId)

Returns the connection config for starting an MCP session:

{
  command?: string;    // For STDIO transport
  args?: string[];
  url?: string;        // For HTTP/SSE transport
  transport: 'stdio' | 'sse' | 'http';
}

MCPConfig Schema

FieldTypeDescription
idstringPrimary key
orgIdstringOwning organisation
userIdstring?Owning user (null = org-shared)
serverIdstringProvider slug
instanceIdstringUnique instance ID
labelstringHuman-readable name
hostingCLOUD | LOCALWhere the MCP process runs
transportSTDIO | SSE | HTTPConnection transport
configJSON?Non-sensitive config
credentialsJSON?Encrypted OAuth tokens or API key
statusstringCONNECTED | DISCONNECTED | CONNECTING | ERROR
lastErrorstring?Last error message
lastUsedAtDateTime?Last usage

MCP OAuth Module

Server-mediated OAuth for integrations like Notion, Google Drive, and ClickUp. Source: packages/server/src/modules/mcp-oauth/.

OAuth Endpoints

MethodPathGuardsDescription
POST/mcp-oauth/startClerkAuthGuardStart OAuth flow → get authUrl
GET/mcp-oauth/callbackNoneOAuth provider callback
POST/mcp-oauth/refreshNoneRefresh an expired token
GET/mcp-oauth/pollNonePoll for credentials after callback

OAuth Flow

Client          Server           OAuth Provider
  │                │                  │
  ├─POST /start───▶│                  │
  │◀─── authUrl ───┤                  │
  │                │                  │
  │    (user opens browser)           │
  │                │◀─ GET /callback ─┤
  │                ├─ exchange code ──▶│
  │                │◀─── tokens ───────┤
  │                │ (store credentials)
  │                │                  │
  ├─GET /poll ────▶│                  │
  │◀─ credentials ─┤                  │
  1. POST /mcp-oauth/start: Client sends { serverId, instanceId, state }. Server looks up provider's authUrl + clientId + scopes, constructs the full URL, and returns it.
  2. Client opens URL in browser. User grants access. Provider redirects to GET /mcp-oauth/callback?code=...&state=....
  3. Server exchanges the code for tokens using the provider's tokenUrl.
  4. Credentials are stored temporarily (keyed by state) pending the client's poll.
  5. GET /mcp-oauth/poll?state=...: Client polls until { ready: true, credentials: {...} }.
  6. Client calls POST /connectors/:id/credentials to persist the credentials.

MCPOAuthService

MethodDescription
startFlow({ serverId, instanceId, state, orgId })Build and return auth URL
handleCallback(state, code)Exchange code, store credentials temporarily
refreshToken(serverId, refreshToken)Refresh using stored client secret
pollCredentials(state)Return { ready, credentials? }