UNPKG

@code-yeongyu/senpi

Version:

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

84 lines • 4.63 kB
import { cachedToolsToCatalogEntries } from "./catalog.js"; import { computeMcpExposurePolicy } from "./expose/policy.js"; import { registerDirectMcpTools } from "./expose/session.js"; import { ensureMcpToolCallConnection } from "./health.js"; import { createMcpInvocationResolver } from "./invocation.js"; import { connectAndRefreshMcpCatalog, prepareMcpAuthentication } from "./startup-race.js"; export async function registerMcpServiceDirectTools(pi, config, entries, toolSearchService, options = {}) { return await registerDirectMcpTools(pi, config, [...entries].map((entry) => { const serverConfig = config.servers[entry.name]?.config; const claim = entry.startupCatalogClaim; const startupCatalogPending = claim?.ownsRegistration() === true; const invocation = createMcpInvocationResolver({ owner: entry, sink: { logger: entry.logger }, configuration: entry.configHash, contextRequired: options.contextRequired === true, sessionManager: options.sessionManager, servesSession: options.servesSession, isCurrent: () => entry.isCurrent?.() !== false && options.isCurrent?.(entry) !== false, ready: async () => { if (!(await prepareMcpAuthentication(entry))) return; if (entry.isCurrent?.() === false || options.isCurrent?.(entry) === false) return; if (entry.credentialsCurrent?.() === false) { await entry.onCredentialsChanged?.(); return; } await ensureMcpToolCallConnection(entry.connection); if (entry.isCurrent?.() === false || options.isCurrent?.(entry) === false) return; if (entry.credentialsCurrent?.() === false) { await entry.onCredentialsChanged?.(); return; } await connectAndRefreshMcpCatalog(entry, serverConfig); await entry.startupCatalogClaim?.settled(); if (entry.isCurrent?.() === false || options.isCurrent?.(entry) === false) return; await options.publishCurrent?.(); }, current: (tool) => { if (serverConfig === undefined || entry.credentialsCurrent?.() === false || entry.cachedCatalog === undefined || !entry.cacheRefreshedAfterConnect || entry.catalogGeneration !== entry.connection.generation || entry.connection.state !== "connected") { return undefined; } const current = cachedToolsToCatalogEntries(entry.name, entry.cachedCatalog, entry.connection, serverConfig.requestTimeoutMs, () => connectAndRefreshMcpCatalog(entry, serverConfig), { agentDir: entry.agentDir, artifacts: entry.artifacts, ensureFresh: () => entry.authPlan?.refresh?.ensureFresh().then(() => undefined) ?? Promise.resolve(), outputGuard: config.settings.outputGuard, invocation, requestOrigin: (caller) => options.requestOrigin?.(entry, caller), }); return computeMcpExposurePolicy(current, serverConfig, config.settings).filteredEntries.find((candidate) => candidate.tool === tool); }, }); return { agentDir: entry.agentDir, artifacts: entry.artifacts, cachedCatalog: entry.credentialsCurrent?.() === false ? undefined : startupCatalogPending ? claim?.cachedCatalog : entry.cachedCatalog, startupCatalogPending, localCatalogOnly: true, invocation, requestOrigin: (caller) => options.requestOrigin?.(entry, caller), bindPresentation: options.bindPresentation, onRegistered: (identity) => options.onRegistered?.(entry, identity), connection: entry.connection, ensureFresh: () => entry.authPlan?.refresh?.ensureFresh().then(() => undefined) ?? Promise.resolve(), ensureCachedToolConnected: () => connectAndRefreshMcpCatalog(entry, serverConfig), logger: entry.logger, name: entry.name, }; }), toolSearchService, { refreshActiveSetWhenEmpty: options.refreshActiveSetWhenEmpty }); } //# sourceMappingURL=service-register.js.map