@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
62 lines • 3.12 kB
JavaScript
import { fingerprintSecret } from "../log.js";
import { McpOAuthProvider } from "./oauth-provider.js";
import { McpRefreshManager } from "./oauth-refresh.js";
import { McpTokenStore } from "./token-store.js";
// Placeholder loopback redirect for background (non-interactive) connects; the
// real callback URL is supplied by /mcp auth. Never actually dereferenced.
const BACKGROUND_REDIRECT = "http://127.0.0.1:0/callback";
// OAuth autodetect (#158): headers or an explicit non-oauth `auth` disable it so
// API-key servers never trigger DCR/discovery.
export function resolveAuthMode(config) {
if (config.auth === "oauth")
return "oauth";
if (config.auth === false)
return "none";
if (config.auth === "bearer" || config.bearerTokenEnv !== undefined)
return "bearer";
if (config.headers !== undefined && Object.keys(config.headers).length > 0)
return "none";
if (config.type === "http" && typeof config.url === "string" && config.url.length > 0)
return "oauth";
return "none";
}
export function resolveServerAuth(deps) {
const mode = resolveAuthMode(deps.config);
if (mode !== "oauth")
return { mode };
const serverUrl = deps.config.url ?? "";
const store = new McpTokenStore({ agentDir: deps.agentDir, serverName: deps.serverName, serverUrl });
const provider = new McpOAuthProvider({
serverName: deps.serverName,
serverUrl,
store,
// A redirect URL must be present so the SDK selects the authorization-code
// flow and raises UnauthorizedError (-> needs_auth) on a token-less connect,
// rather than falling through to a client_credentials token request.
redirectUrl: deps.redirectUrl ?? BACKGROUND_REDIRECT,
// Only a caller that names its redirect sends an authorize request; background connects and refresh keep
// the stored registration so an existing sign-in keeps refreshing (senpi#6327).
requireRegisteredRedirect: deps.redirectUrl !== undefined,
scopes: deps.config.oauth?.scopes,
clientId: deps.config.oauth?.clientId,
clientMetadataUrl: deps.config.oauth?.clientMetadataUrl,
logger: deps.logger,
onRedirect: deps.onRedirect,
});
return { mode, provider, refresh: new McpRefreshManager(provider) };
}
const LITERAL_SECRET = /(?:bearer\s+\S|sk-[a-z0-9]|[a-z0-9]{24,})/i;
// Warn (never block) when a plaintext token is embedded in headers instead of a
// ${VAR} reference. Emits fingerprints only, never the raw value.
export function detectLiteralBearerWarnings(serverName, config) {
const warnings = [];
for (const [header, value] of Object.entries(config.headers ?? {})) {
if (typeof value !== "string" || value.includes("${"))
continue;
if (LITERAL_SECRET.test(value)) {
warnings.push(`MCP server ${serverName} header '${header}' appears to contain a literal secret (fp ${fingerprintSecret(value)}); use \${ENV_VAR} so the token stays out of the config file.`);
}
}
return warnings;
}
//# sourceMappingURL=context.js.map