UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

109 lines • 4.92 kB
import { detectLiteralBearerWarnings, resolveServerAuth } from "./auth/context.js"; import { mcpConnectionIdentity, mcpGenerationRenewal } from "./config-generation.js"; import { ServerConnection } from "./connection.js"; import { markMcpConnectionNeedsAuth } from "./health.js"; import { shareable } from "./host-registry.js"; import { configureMcpConnectionLifecycle, disposeMcpConnectionLifecycle } from "./idle.js"; import { createMcpLogger } from "./log.js"; import { getProjectMcpAuthority } from "./project-skill-policy.js"; import { configureMcpReconnect, disposeMcpReconnect } from "./reconnect.js"; import { SharedMcpLease } from "./shared-lease.js"; import { connectAndRefreshMcpCatalog } from "./startup-race.js"; export function createMcpSessionConnection(options) { const { session, name, key, registry } = options; const config = options.generation.config; // The project authority is keyed by the declared config object; the generation snapshot is a new object. const projectPolicy = getProjectMcpAuthority(options.declared); const logger = createMcpLogger(name, { logDir: session.logDir }); const authPlan = resolveServerAuth({ agentDir: session.agentDir, config, env: session.env, logger, serverName: name, }); for (const warning of detectLiteralBearerWarnings(name, options.declared)) logger.warn(warning); const rekey = () => { options .onCredentialsChanged?.() .catch((error) => logger.warn("Failed to re-key MCP connection", { error })); }; const renewal = mcpGenerationRenewal({ serverName: name, declared: options.declared, env: session.env, agentDir: session.agentDir }, options.generation.configHash, rekey); const connectionOptions = { projectPolicy, credentialIdentity: options.credentialIdentity, credentialsCurrent: options.credentialsCurrent, authProvider: authPlan.provider, config, env: projectPolicy?.initial.env ?? session.env, elicitationUiProvider: options.ui, elicitationEnabled: options.elicitationEnabled, logger, serverName: name, ...(renewal === undefined ? {} : { resolveConfig: renewal.resolveConfig, onGenerationChanged: rekey }), }; const connection = options.share && session.agentDir !== undefined && shareable(config, options.cwd) ? registry.attachShared(key, options.owner, { ...connectionOptions, agentDir: session.agentDir, configHash: mcpConnectionIdentity(options.configHash, options.generation.configHash), }) : registry.attach(key, options.owner, () => new ServerConnection(connectionOptions)); const entry = { agentDir: session.agentDir, env: session.env, artifacts: options.artifacts, authPlan, cacheRefreshedAfterConnect: false, isCurrent: () => options.shouldReconnect(entry), credentialIdentity: options.credentialIdentity, credentialsCurrent: options.credentialsCurrent, onCredentialsChanged: options.onCredentialsChanged, key, name, configHash: options.configHash, generationHash: mcpConnectionIdentity(options.configHash, options.generation.configHash), ...(renewal === undefined ? {} : { generationChanged: renewal.changed }), connection, logger, createdAtMs: Date.now(), counters: { callCount: 0, errorCount: 0, totalLatencyMs: 0, reconnectCount: 0 }, }; if (connection instanceof SharedMcpLease) return entry; configureMcpConnectionLifecycle(connection, config, logger); configureMcpReconnect({ connection, logger, reconnect: async () => { entry.counters.reconnectCount += 1; entry.cacheRefreshedAfterConnect = false; try { await entry.authPlan?.refresh?.ensureFresh(); } catch (error) { if (entry.connection.state === "disabled") return; const authError = markMcpConnectionNeedsAuth(entry.connection, error); if (authError !== undefined) { entry.logger.warn(authError.message); throw authError; } throw error; } await entry.connection.renew(); await connectAndRefreshMcpCatalog(entry, config); }, shouldReconnect: () => options.shouldReconnect(entry), }); return entry; } export async function disposeEntryConnection(entry, registry, owner) { entry.disposeListChanged?.(); entry.disposeWireStatus?.(); disposeMcpReconnect(entry.connection); disposeMcpConnectionLifecycle(entry.connection); await registry.detach(entry.key, owner); } //# sourceMappingURL=service-connection.js.map