Skip to content

Commit e3b6195

Browse files
committed
fix(foundry): use bearer auth in native transport
1 parent 72bfc5e commit e3b6195

4 files changed

Lines changed: 88 additions & 1 deletion

File tree

‎src/agents/anthropic-transport-stream.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ function makeAnthropicTransportModel(
143143
maxTokens?: number;
144144
thinkingLevelMap?: AnthropicMessagesModel["thinkingLevelMap"];
145145
headers?: Record<string, string>;
146+
authHeader?: boolean;
146147
requestTransport?: RequestTransportConfig;
147148
} = {},
148149
): AnthropicMessagesModel {
@@ -161,6 +162,7 @@ function makeAnthropicTransportModel(
161162
maxTokens: params.maxTokens ?? 8192,
162163
...(params.thinkingLevelMap ? { thinkingLevelMap: params.thinkingLevelMap } : {}),
163164
...(params.headers ? { headers: params.headers } : {}),
165+
...(params.authHeader !== undefined ? { authHeader: params.authHeader } : {}),
164166
} satisfies AnthropicMessagesModel,
165167
params.requestTransport ?? {
166168
proxy: {
@@ -235,6 +237,35 @@ describe("anthropic transport stream", () => {
235237
);
236238
});
237239

240+
it("uses bearer auth for Microsoft Foundry Anthropic transport requests", async () => {
241+
const model = makeAnthropicTransportModel({
242+
provider: "microsoft-foundry",
243+
baseUrl: "https://example.services.ai.azure.com/anthropic",
244+
authHeader: true,
245+
headers: {
246+
"api-key": "stale-foundry-key",
247+
"x-api-key": "stale-resource-key",
248+
"X-Provider": "foundry",
249+
},
250+
});
251+
252+
await runTransportStream(
253+
model,
254+
{
255+
messages: [{ role: "user", content: "hello" }],
256+
} as AnthropicStreamContext,
257+
{
258+
apiKey: "entra-access-token",
259+
} as AnthropicStreamOptions,
260+
);
261+
262+
const headers = latestAnthropicRequestHeaders();
263+
expect(headers.get("authorization")).toBe("Bearer entra-access-token");
264+
expect(headers.get("api-key")).toBeNull();
265+
expect(headers.get("x-api-key")).toBeNull();
266+
expect(headers.get("X-Provider")).toBe("foundry");
267+
});
268+
238269
it("honors ANTHROPIC_BASE_URL when model base URL is blank", async () => {
239270
vi.stubEnv("ANTHROPIC_BASE_URL", " https://anthropic-proxy.example/v1 ");
240271

‎src/agents/anthropic-transport-stream.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,39 @@ function isAnthropicOAuthToken(apiKey: string): boolean {
239239
return apiKey.includes("sk-ant-oat");
240240
}
241241

242+
function hasBearerAuthorizationHeader(headers?: Record<string, string>): boolean {
243+
if (!headers) {
244+
return false;
245+
}
246+
return Object.entries(headers).some(
247+
([key, value]) => key.toLowerCase() === "authorization" && /^bearer\s+\S+/i.test(value.trim()),
248+
);
249+
}
250+
251+
function usesFoundryBearerAuth(model: AnthropicTransportModel): boolean {
252+
return (
253+
model.provider === "microsoft-foundry" &&
254+
(model.authHeader === true || hasBearerAuthorizationHeader(model.headers))
255+
);
256+
}
257+
258+
function omitFoundryBearerCredentialHeaders(
259+
headers?: Record<string, string>,
260+
): Record<string, string> | undefined {
261+
if (!headers) {
262+
return undefined;
263+
}
264+
const next: Record<string, string> = {};
265+
for (const [key, value] of Object.entries(headers)) {
266+
const lower = key.toLowerCase();
267+
if (lower === "authorization" || lower === "x-api-key" || lower === "api-key") {
268+
continue;
269+
}
270+
next[key] = value;
271+
}
272+
return Object.keys(next).length > 0 ? next : undefined;
273+
}
274+
242275
function isDirectAnthropicModel(model: Pick<AnthropicTransportModel, "provider" | "baseUrl">) {
243276
if (normalizeLowercaseStringOrEmpty(model.provider) !== "anthropic") {
244277
return false;
@@ -791,6 +824,27 @@ function createAnthropicTransportClient(params: {
791824
isOAuthToken: false,
792825
};
793826
}
827+
if (usesFoundryBearerAuth(model)) {
828+
const betaFeatures = needsInterleavedBeta ? ["interleaved-thinking-2025-05-14"] : [];
829+
return {
830+
client: createAnthropicMessagesClient({
831+
apiKey: null,
832+
authToken: apiKey,
833+
baseURL: model.baseUrl,
834+
defaultHeaders: mergeTransportHeaders(
835+
{
836+
accept: "application/json",
837+
"anthropic-dangerous-direct-browser-access": "true",
838+
...(betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}),
839+
},
840+
omitFoundryBearerCredentialHeaders(model.headers),
841+
options?.headers,
842+
),
843+
fetch,
844+
}),
845+
isOAuthToken: false,
846+
};
847+
}
794848
const betaFeatures = ["fine-grained-tool-streaming-2025-05-14"];
795849
if (needsInterleavedBeta) {
796850
betaFeatures.push("interleaved-thinking-2025-05-14");

‎src/llm/providers/anthropic.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ describe("Anthropic provider", () => {
8989
baseUrl: "https://example.services.ai.azure.com/anthropic",
9090
authHeader: true,
9191
headers: {
92+
"api-key": "stale-foundry-key",
9293
"x-api-key": "stale-resource-key",
9394
},
9495
});
@@ -110,6 +111,7 @@ describe("Anthropic provider", () => {
110111
expect(config.apiKey).toBeNull();
111112
expect(config.authToken).toBe("entra-access-token");
112113
expect(config.defaultHeaders?.Authorization).toBeUndefined();
114+
expect(config.defaultHeaders?.["api-key"]).toBeUndefined();
113115
expect(config.defaultHeaders?.["x-api-key"]).toBeUndefined();
114116
});
115117

‎src/llm/providers/anthropic.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ function omitFoundryBearerCredentialHeaders(
282282
const next: Record<string, string> = {};
283283
for (const [key, value] of Object.entries(headers)) {
284284
const lower = key.toLowerCase();
285-
if (lower === "authorization" || lower === "x-api-key") {
285+
if (lower === "authorization" || lower === "x-api-key" || lower === "api-key") {
286286
continue;
287287
}
288288
next[key] = value;

0 commit comments

Comments
 (0)