/** * Durable MCP catalog operations and live connection supervision. * @module @anht3889/dsh-mcp-mgmt-bundle/manager/runtime */ import type { Context } from '@deepseek-ai/cordis'; import type { McpConnectionStatus, McpLogEntry, McpRuntime, McpServerId, McpServerRecord, McpToolInfo } from '@anht3889/dsh-mcp-mgmt-mcp'; import { type DiscoveredOAuthConfig, type OAuthCallbackQuery } from '@anht3889/dsh-mcp-mgmt-oauth'; import { type ConnectionHandle, type ConnectionHooks } from './connection.ts'; import { type CredentialsApi } from './secrets.ts'; /** Filesystem locations used by the MCP manager. */ export interface McpManagerRuntimeConfig { /** Non-secret MCP server records. */ catalogPath: string; /** Fallback secret storage when no credential provider is mounted. */ secretsPath: string; } /** Optional test seams for the runtime's process-bound dependencies. */ export interface McpManagerRuntimeOptions extends McpManagerRuntimeConfig { /** Optional credential service, resolved from `ctx.get('credentials')` by the plugin. */ credentials?: CredentialsApi; /** Starts one connection generation. */ startConnection?: (record: McpServerRecord, hooks: ConnectionHooks) => ConnectionHandle; /** Resolves the browser-visible origin serving OAuth callbacks. */ oauthRedirectOrigin?: () => string; } /** * Concrete `ctx.mcp` provider that owns catalog persistence, secrets, OAuth, * and every live MCP connection. */ export declare class McpManagerRuntime implements McpRuntime { private readonly ctx; private readonly config; private readonly records; private readonly statuses; private readonly logs; private readonly connections; /** Tools each server listed most recently, without their selection state. */ private readonly listedTools; private readonly secrets; private readonly oauth; private readonly resolveRedirectOrigin; private readonly catalogListeners; private readonly start; private constructor(); /** * Loads the catalog then starts every enabled server. * @param ctx - the service-owning Cordis context. * @param options - persisted paths plus optional integration seams. * @returns the ready MCP runtime. */ static create(ctx: Context, options: McpManagerRuntimeOptions): Promise; /** @returns configured MCP server records. */ list(): McpServerRecord[]; /** * @param id - server identifier. * @returns the configured record, if present. */ get(id: McpServerId): McpServerRecord | undefined; /** * Persists a record and stops any prior connection generation. * @param record - server configuration to create or replace. * @returns the persisted record. */ upsert(record: McpServerRecord): Promise; /** * Stops the server, removes its record, and wipes its owned secrets. * @param id - server identifier. */ remove(id: McpServerId): Promise; /** * Persists an enabled state and aligns the live connection. * @param id - server identifier. * @param enabled - whether the server should be active. */ setEnabled(id: McpServerId, enabled: boolean): Promise; /** * Starts a fresh connection generation for a configured server. * @param id - server identifier. */ connect(id: McpServerId): Promise; /** * Stops the current generation and unregisters its tools. * @param id - server identifier. */ disconnect(id: McpServerId): Promise; /** * @param id - server identifier. * @returns current in-memory connection state. */ getStatus(id: McpServerId): McpConnectionStatus; /** * @param id - server identifier. * @param after - exclusive log cursor. * @returns retained log entries after the cursor. */ getLogs(id: McpServerId, after?: number): { next: number; entries: McpLogEntry[]; }; /** * @param id - server identifier. * @returns the tools listed by the most recent successful connection, each * carrying the selection state the record holds now. */ getTools(id: McpServerId): McpToolInfo[]; /** * Persists a tool's selection state and re-registers the live connection's * tools, which keeps its transport and so its stdio process or HTTP session. * * @param id - server identifier. * @param toolName - tool name as the server lists it. * @param enabled - whether the harness registry exposes the tool. */ setToolEnabled(id: McpServerId, toolName: string, enabled: boolean): Promise; /** * @param id - OAuth-configured server identifier. * @returns authorization URL. */ startOAuth(id: McpServerId): Promise<{ authorizeUrl: string; }>; /** * Clears OAuth tokens and stops the server until it is authorized again. * @param id - OAuth-configured server identifier. */ clearOAuth(id: McpServerId): Promise; /** * Stores write-only server secrets. * @param id - server identifier. * @param secrets - secret values keyed by their logical names. */ setSecrets(id: McpServerId, secrets: Record): Promise; /** * Returns configured states for the secrets associated with a server. * @param id - server identifier. * @returns value-free configured states keyed by logical secret name. */ describeSecrets(id: McpServerId): Promise>; /** * Discovers OAuth endpoints from an MCP HTTP URL and registers a public * client when the authorization server supports Dynamic Client Registration * and this profile can name a callback origin. * * Endpoint discovery needs no redirect URI, so a profile that serves no * callback still reports the authorize and token endpoints and leaves * `clientId` empty with `registered: false`. * * @param serverUrl - streamable-http MCP server URL. * @returns fields for the Settings editor's OAuth form. */ discoverOAuth(serverUrl: string): Promise; /** * @returns the callback origin without a trailing slash, or `undefined` when * neither `publicOrigin` nor a listening web server can name one. */ oauthRedirectOrigin(): string | undefined; /** * @returns the Dynamic Client Registration redirect URI, or `undefined` when * this profile can name no callback origin. Registration is the only part * of discovery that needs one, and a client registered against an origin * that serves no callback could never complete an authorization. */ private registrationRedirectUri; /** * @returns the distinct loopback callback paths configured by OAuth servers, * which the HTTP API must serve as routes of their own. */ oauthCallbackPaths(): string[]; /** * Observes catalog membership so callback routes follow configuration. * @param listener - called after any record is added, replaced, or removed. * @returns a disposer that removes the listener. */ onCatalogChanged(listener: () => void): () => void; /** * Exchanges a browser OAuth callback and starts an enabled server. * @param query - authorization response fields from the redirect URI. * @returns the authorized server identifier. */ handleOAuthCallback(query: OAuthCallbackQuery): Promise<{ serverId: string; }>; /** Stops all live server generations. */ dispose(): Promise; private save; private notifyCatalogChanged; private bufferFor; private requireRecord; private resolveHeaders; }