Skip to content

Commit 714c540

Browse files
fix(server): recognize a Claude proxy set in Claude's settings.json
The claude CLI applies the env block in its settings files over the process environment, so a proxy configured there (ANTHROPIC_BASE_URL plus ANTHROPIC_AUTH_TOKEN, the usual CLIProxyAPI setup) was invisible to the provider probe and the instance showed as "Claude API Key". Read ANTHROPIC_BASE_URL from the same settings files the CLI loads and prefer the custom-endpoint label over the token-source label.
1 parent 6d450a4 commit 714c540

3 files changed

Lines changed: 95 additions & 7 deletions

File tree

‎apps/server/src/provider/ClaudeProvider.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
} from "@t3tools/provider-core/server/snapshotProbe";
3838
import { resolveClaudeSdkExecutablePath } from "./Drivers/ClaudeExecutable.ts";
3939
import { hasClaudeManagedMcpConfig, makeClaudeEnvironment } from "./Drivers/ClaudeHome.ts";
40-
import { discoverClaudeSkills } from "./Drivers/ClaudeSkills.ts";
40+
import { discoverClaudeSkills, readClaudeSettingsBaseUrl } from "./Drivers/ClaudeSkills.ts";
4141
import type { ProviderWorkspaceSnapshot } from "@t3tools/provider-core/server/driver";
4242
import { makeUnavailableUsageLimits } from "@t3tools/provider-core/server/usageLimits";
4343
import {
@@ -602,7 +602,10 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
602602
}
603603

604604
// First-party account fields cannot verify custom endpoint credentials.
605-
const endpoint = URL.parse(resolvedEnvironment.ANTHROPIC_BASE_URL?.trim() ?? "");
605+
const baseUrl =
606+
(yield* readClaudeSettingsBaseUrl(claudeSettings, cwd, resolvedEnvironment)) ??
607+
resolvedEnvironment.ANTHROPIC_BASE_URL;
608+
const endpoint = URL.parse(baseUrl?.trim() ?? "");
606609
const customApiEndpoint =
607610
endpoint !== null &&
608611
(endpoint.protocol === "http:" || endpoint.protocol === "https:") &&
@@ -626,13 +629,14 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
626629
});
627630
}
628631

632+
// A proxy's ANTHROPIC_AUTH_TOKEN reads as an API key; the endpoint is what the token is for.
629633
const authMetadata =
634+
apiProviderAuthMetadata(capabilities.apiProvider) ??
635+
(customApiEndpoint ? { type: "customEndpoint", label: "Custom API endpoint" } : undefined) ??
630636
claudeAuthMetadata({
631637
subscriptionType: capabilities.subscriptionType,
632638
authMethod: capabilities.tokenSource,
633-
}) ??
634-
apiProviderAuthMetadata(capabilities.apiProvider) ??
635-
(customApiEndpoint ? { type: "customEndpoint", label: "Custom API endpoint" } : undefined);
639+
});
636640
const usageCheckedAt = capabilities.usageCheckedAt ?? checkedAt;
637641
const usageLimits = !capabilities.usage
638642
? makeUnavailableUsageLimits({ checkedAt, reason: "probeFailed" })

‎apps/server/src/provider/Drivers/ClaudeSkills.ts‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ function claudeManagedSettingsPath(
119119
return "/etc/claude-code/managed-settings.json";
120120
}
121121

122-
function skillOverrideSettingsPaths(
122+
/** Claude Code settings files, lowest precedence first. */
123+
function claudeSettingsPaths(
123124
path: Path.Path,
124125
configDirPath: string,
125126
cwd: string | undefined,
@@ -247,7 +248,7 @@ const readSkillSettings = Effect.fn("readSkillSettings")(function* (
247248
const enabledPlugins = new Map<string, boolean>();
248249
const repositoryRoot = cwd === undefined ? undefined : yield* findRepositoryRoot(cwd);
249250

250-
for (const settingsPath of skillOverrideSettingsPaths(
251+
for (const settingsPath of claudeSettingsPaths(
251252
path,
252253
configDirPath,
253254
cwd,
@@ -427,6 +428,45 @@ const resolveClaudeConfigDirPath = Effect.fn("resolveClaudeConfigDirPath")(funct
427428
return path.join(home, ".claude");
428429
});
429430

431+
const SettingsBaseUrl = fromLenientJson(
432+
Schema.Struct({
433+
env: Schema.optional(Schema.Struct({ ANTHROPIC_BASE_URL: Schema.optional(Schema.String) })),
434+
}),
435+
);
436+
const decodeSettingsBaseUrl = Schema.decodeUnknownEffect(SettingsBaseUrl);
437+
438+
/** ANTHROPIC_BASE_URL from Claude's settings `env`, which Claude Code applies over the process env. */
439+
export const readClaudeSettingsBaseUrl = Effect.fn("readClaudeSettingsBaseUrl")(function* (
440+
config: Pick<ClaudeSettings, "homePath">,
441+
cwd: string | undefined,
442+
environment: NodeJS.ProcessEnv,
443+
): Effect.fn.Return<string | undefined, never, FileSystem.FileSystem | Path.Path> {
444+
const fileSystem = yield* FileSystem.FileSystem;
445+
const path = yield* Path.Path;
446+
const platform = yield* HostProcess.Platform;
447+
const configDirPath = yield* resolveClaudeConfigDirPath(config, environment, cwd);
448+
const repositoryRoot = cwd === undefined ? undefined : yield* findRepositoryRoot(cwd);
449+
let baseUrl: string | undefined;
450+
for (const settingsPath of claudeSettingsPaths(
451+
path,
452+
configDirPath,
453+
cwd,
454+
platform,
455+
environment,
456+
repositoryRoot,
457+
)) {
458+
const contents = yield* fileSystem
459+
.readFileString(settingsPath)
460+
.pipe(Effect.orElseSucceed(() => undefined));
461+
if (contents === undefined) continue;
462+
const parsed = yield* decodeSettingsBaseUrl(contents).pipe(
463+
Effect.orElseSucceed(() => undefined),
464+
);
465+
baseUrl = parsed?.env?.ANTHROPIC_BASE_URL ?? baseUrl;
466+
}
467+
return baseUrl;
468+
});
469+
430470
/** First name wins, matching Claude Code's precedence of user skills over project skills. */
431471
export const discoverClaudeSkills = Effect.fn("discoverClaudeSkills")(function* (
432472
config: Pick<ClaudeSettings, "homePath"> & Partial<Pick<ClaudeSettings, "launchArgs">>,

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import * as Deferred from "effect/Deferred";
1010
import * as Effect from "effect/Effect";
1111
import * as Exit from "effect/Exit";
1212
import * as Fiber from "effect/Fiber";
13+
import * as FileSystem from "effect/FileSystem";
1314
import * as Layer from "effect/Layer";
15+
import * as Path from "effect/Path";
1416
import * as PubSub from "effect/PubSub";
1517
import * as Ref from "effect/Ref";
1618
import * as Schema from "effect/Schema";
@@ -34,6 +36,7 @@ import {
3436
} from "@t3tools/contracts";
3537
import { HttpClient, HttpClientResponse } from "effect/http";
3638
import { ChildProcessSpawner } from "effect/process";
39+
import * as HostProcess from "@t3tools/shared/HostProcess";
3740
import { deepMerge } from "@t3tools/shared/Struct";
3841
import { createModelCapabilities } from "@t3tools/shared/model";
3942
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
@@ -316,6 +319,13 @@ const layerTestNodeServices = Layer.mergeAll(
316319
Layer.succeed(ServerEnvironment.ServerEnvironmentIdentity, {
317320
getEnvironmentId: Effect.succeed(EnvironmentId.make("00000000-0000-4000-8000-000000000001")),
318321
}),
322+
// An empty home, so the developer's own Claude settings never reach provider probes.
323+
Layer.effect(
324+
HostProcess.HomeDirectory,
325+
Effect.gen(function* () {
326+
return yield* (yield* FileSystem.FileSystem).makeTempDirectoryScoped();
327+
}),
328+
).pipe(Layer.provide(NodeServices.layer)),
319329
);
320330

321331
it.layer(
@@ -2332,6 +2342,40 @@ it.layer(
23322342
),
23332343
);
23342344

2345+
it.effect("labels a proxy set in Claude's settings env, which beats the process env", () =>
2346+
Effect.gen(function* () {
2347+
const fileSystem = yield* FileSystem.FileSystem;
2348+
const path = yield* Path.Path;
2349+
const configDir = yield* fileSystem.makeTempDirectoryScoped({
2350+
prefix: "t3-claude-settings-",
2351+
});
2352+
yield* fileSystem.writeFileString(
2353+
path.join(configDir, "settings.json"),
2354+
JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://hub.example.ts.net:8317" } }),
2355+
);
2356+
const status = yield* checkClaudeProviderStatus(
2357+
{ ...defaultClaudeSettings, homePath: configDir },
2358+
claudeCapabilities({
2359+
tokenSource: "ANTHROPIC_AUTH_TOKEN",
2360+
apiKeySource: "none",
2361+
apiProvider: "firstParty",
2362+
}),
2363+
{ ANTHROPIC_BASE_URL: "https://api.anthropic.com" },
2364+
);
2365+
assert.strictEqual(status.status, "ready");
2366+
assert.strictEqual(status.auth.type, "customEndpoint");
2367+
assert.strictEqual(status.auth.label, "Custom API endpoint");
2368+
}).pipe(
2369+
Effect.provide(
2370+
layerMockSpawner((args) => {
2371+
const joined = args.join(" ");
2372+
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
2373+
throw new Error(`Unexpected args: ${joined}`);
2374+
}),
2375+
),
2376+
),
2377+
);
2378+
23352379
it.effect.each([
23362380
"https://api.anthropic.com",
23372381
"https://api.anthropic.com:443",

0 commit comments

Comments
 (0)