MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / organizationFromDatabase

Function organizationFromDatabase

apps/cloud/src/mcp/session-meta.ts:138–181  ·  view source on GitHub ↗
(
  organizationId: string,
)

Source from the content-addressed store, hash-verified

136 * defect.
137 */
138const organizationFromDatabase = (
139 organizationId: string,
140): Effect.Effect<
141 { readonly id: string; readonly name: string; readonly slug?: string },
142 McpSessionMetaUnavailableError | OrganizationNotFoundError,
143 UserStoreService | WorkOSClient
144> =>
145 Effect.gen(function* () {
146 let attempts = 0;
147 // `Effect.retry` retries every failure it sees, so definitive failures are
148 // lifted OUT of the error channel before the schedule ever runs and only
149 // the retryable ones are left in it.
150 const attempt = Effect.suspend(() => {
151 attempts += 1;
152 return resolveOrganization(organizationId).pipe(
153 Effect.result,
154 Effect.flatMap((outcome) =>
155 Result.isFailure(outcome) && isRetryableOrganizationLookupFailure(outcome.failure)
156 ? Effect.fail(outcome.failure)
157 : Effect.succeed(outcome),
158 ),
159 );
160 });
161
162 // Two nested Results: the outer one is "the retries ran out", the inner one
163 // is "the lookup failed definitively on the first look". Both mean the same
164 // thing to the caller.
165 const retried = yield* attempt.pipe(Effect.retry(RETRY_SCHEDULE), Effect.result);
166 const outcome = Result.isFailure(retried) ? Result.fail(retried.failure) : retried.success;
167
168 if (Result.isFailure(outcome)) {
169 return yield* new McpSessionMetaUnavailableError({
170 reason: failureReason(outcome.failure),
171 attempts,
172 });
173 }
174 const organization = outcome.success;
175 if (!organization) return yield* new OrganizationNotFoundError({ organizationId });
176 return organization;
177 }).pipe(
178 Effect.withSpan("mcp.session.resolve_organization", {
179 attributes: { "mcp.auth.organization_id": organizationId },
180 }),
181 );
182
183/**
184 * Build the session meta for an init, preferring the org identity the request

Callers 1

Calls 4

resolveOrganizationFunction · 0.90
failureReasonFunction · 0.85
retryMethod · 0.80

Tested by

no test coverage detected