MCPcopy Create free account
hub / github.com/Noumena-Network/code / buildFirstPartyHeadersFromSession

Function buildFirstPartyHeadersFromSession

src/auth/runtime/headers.ts:11–45  ·  view source on GitHub ↗
({
  apiKey,
  includeApiKeyHeader,
  session,
}: BuildFirstPartyHeadersFromSessionInput)

Source from the content-addressed store, hash-verified

9}
10
11export function buildFirstPartyHeadersFromSession({
12 apiKey,
13 includeApiKeyHeader,
14 session,
15}: BuildFirstPartyHeadersFromSessionInput): Record<string, string> {
16 const headers: Record<string, string> = {}
17
18 if (session.headersKind === 'bearer') {
19 const bearerValue =
20 session.principalSource === 'api_key_helper'
21 ? session.apiKey
22 : session.accessToken
23 if (!bearerValue) {
24 throw new AuthRuntimeError({
25 code: 'service_credential_invalid',
26 message: 'Bearer-backed auth session did not contain a usable credential.',
27 userMessage: session.recoveryMessage ?? 'Check the configured credential and try again.',
28 recoveryAction: session.recoveryAction,
29 })
30 }
31 headers.Authorization = `Bearer ${bearerValue}`
32 headers['anthropic-beta'] = OAUTH_BETA_HEADER
33 }
34
35 if (includeApiKeyHeader) {
36 const resolvedApiKey =
37 apiKey ??
38 (session.rawApiKeySource === 'OPENAI_API_KEY' ? undefined : session.apiKey)
39 if (resolvedApiKey) {
40 headers['x-api-key'] = resolvedApiKey
41 }
42 }
43
44 return headers
45}

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected