import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { ClientCapabilities, Tool, ReadResourceResult, CallToolResult } from '@modelcontextprotocol/sdk/types.js'; import { D as DiscoveredMcp } from './mcp-discovery-CLihUe93.js'; import { McpOAuthConfig, McpOAuthSecretIO } from './mcp-oauth.js'; import '@modelcontextprotocol/sdk/client/auth.js'; import '@modelcontextprotocol/sdk/shared/auth.js'; /** * mcp-client-pool — the daemon as an MCP *client* to any resolved server * config, not just an `imported-mcps.json` entry. * * `McpProxyRegistry` (mcp-proxy.ts) keys its clients on import ids; the MCP * Apps host (mcp-apps-host.ts) instead resolves a harness's server alias * per session (session config → project → user → imports), and two sessions * in different projects may resolve the same alias to different servers. * So this pool keys clients on a stable hash of the resolved connection * config: sessions that resolve to the same config share one connection. * * Both paths open transports through `openMcpClient` below, so stdio / http * / sse behave the same whichever registry asked. */ /** The connection-relevant slice of a server config — what a transport * needs, nothing about where it was found. `DiscoveredMcp` (and so an * import's `snapshot`) is structurally one. */ interface McpConnectionConfig { type: DiscoveredMcp["type"]; command?: string; args?: string[]; env?: Record; url?: string; headers?: Record; /** http/sse only: authenticate with the OAuth state stored for this import. */ oauth?: McpOAuthConfig; } /** Run `fn` with `headers` added to every request a forwarding connection sends inside it. */ declare function withCallHeaders(headers: Record | undefined, fn: () => T): T; /** The per-call headers of the call in flight, if any. */ declare function currentCallHeaders(): Record | undefined; /** * A `fetch` that adds the in-flight call's headers (the caller session and * node, `caller-scope.ts`) to each request, replacing any header of the same * name the connection config carries so a configured value cannot forge them. */ declare function callerForwardingFetch(base?: typeof fetch): typeof fetch; /** * Whether a connection receives the caller headers: an http/sse server on this * machine, or one that opted in (`forwardCaller` on its import entry). A * third-party server is never told which node a session runs in by default. */ declare function shouldForwardCallerHeaders(config: McpConnectionConfig, optIn?: boolean): boolean; interface OpenMcpClientOptions { /** Prefix for config errors, e.g. `import "chrome-devtools"`. */ label: string; /** Advertised client capabilities. Default `{}`. */ capabilities?: ClientCapabilities; /** Expand `${VAR}` / `${VAR:-default}` in http/sse header values, the * way claude-code expands them in `.mcp.json`. Env values are always * expanded. Default false. */ expandHeaders?: boolean; /** http/sse + `config.oauth` only: explicit secret IO, preferred over the * `getMcpCredentialDeps()` ambient fallback (see `createDaemonOAuthProvider` * for why the fallback is bundling-topology-dependent and should not be * relied on by a caller in a different tsup entry than the daemon). */ oauthSecrets?: McpOAuthSecretIO; /** http/sse only: send the in-flight call's caller headers (see `withCallHeaders`). Default false. */ forwardCallHeaders?: boolean; } /** * Replace `${VAR}` and `${VAR:-default}` placeholders in env values * with the corresponding `process.env` entry (or default), the same * way claude-code / cursor expand mcp-server env. When the variable * is unset and no default is given, the placeholder is dropped — the * upstream then sees the env var as absent rather than as the * literal `${MISSING_VAR}`, which it usually treats as "use default * config" rather than "explicit empty token". */ declare function expandEnvPlaceholders(raw: Record): Record; declare function safeClose(client: Client | null): Promise; /** * Open a transport for a server config. stdio spawns the command; * http/sse open over the network. Returns a connected `Client` on * success; throws with a useful message on failure. */ declare function openMcpClient(config: McpConnectionConfig, opts: OpenMcpClientOptions): Promise; /** Stable identity of a connection config: sha256 over a key-sorted JSON * rendering, so `{a,b}` and `{b,a}` (or a re-read of the same file) hash * alike. Undefined fields are dropped by JSON itself. */ declare function mcpConfigKey(config: McpConnectionConfig): string; /** One UI-bearing tool of a server, per the MCP Apps spec. */ interface McpUiToolEntry { name: string; /** `ui://…`, from ext-apps `getToolUiResourceUri(tool)`. */ resourceUri: string; /** ext-apps `isToolVisibilityAppOnly(tool)`. */ appOnly: boolean; } /** Per-client UI index, built from the cached `tools/list`. */ interface McpUiToolIndex { tools: McpUiToolEntry[]; /** Every app-only tool, UI or not. */ appOnlyTools: string[]; /** Every tool name the server declared in `tools/list`, UI or not. */ allToolNames: string[]; /** Tools whose `_meta.ui.visibility` is an array that omits `"app"` — per * the MCP Apps spec (ext-apps `_meta.ui.visibility`), the default * `["model", "app"]` applies whenever the array is absent, so only an * explicit array without `"app"` lands here. */ notAppVisibleTools: string[]; } declare function buildUiToolIndex(tools: readonly Tool[]): McpUiToolIndex; /** The capability a host advertises so servers that gate their UI tools on * client support (ext-apps `getUiCapability`) expose them to us. */ declare const MCP_APPS_CLIENT_CAPABILITIES: ClientCapabilities; /** Opens a client for a config — injectable so tests can count connects. */ type McpClientOpener = (config: McpConnectionConfig, label: string) => Promise; /** Message shapes that mean the connection itself died mid-call, or that * the upstream rejected our credentials (401/403: a rotated token) — the * same set `McpProxyRegistry.callTool` treats as "reconnect next time". */ declare const DEAD_CONNECTION_RE: RegExp; /** How long a network (`http`/`sse`) `tools/list` stays fresh. A stateless * streamable-HTTP server keeps no session, so a restart is never observed as * a disconnect; without a TTL its cached list would live forever. */ declare const DEFAULT_TOOL_CACHE_TTL_MS = 45000; declare function isUnknownToolError(message: string): boolean; /** Whether a `tools/call` result is the SDK's unknown-tool error shape. */ declare function resultIsUnknownTool(result: unknown): boolean; interface PooledMcpClientOptions { /** Network transports only: how long a cached `tools/list` stays fresh. */ toolCacheTtlMs?: number; /** Clock seam for tests. */ now?: () => number; } /** * A lazily-connected client for one config. Caches `tools/list` and the UI * index derived from it; both are dropped whenever the connection is, when * the server sends `notifications/tools/list_changed`, when a network cache * outlives its TTL, or when a call names a tool the cache does not know. */ declare class PooledMcpClient { readonly key: string; readonly config: McpConnectionConfig; private readonly label; private readonly opener; private client; private connecting; private tools; private index; private toolsFetchedAt; private refreshing; private lastRefreshError; private readonly ttlMs; private readonly now; constructor(key: string, config: McpConnectionConfig, label: string, opener: McpClientOpener, options?: PooledMcpClientOptions); private get isNetwork(); /** Connect (once; concurrent callers share the attempt) and list tools. * Throws the connect/list error — callers classify it. */ private connected; /** Re-list `tools/list`, coalescing concurrent callers into one request. * A failed refresh keeps the existing cache unless the connection died. */ refreshTools(): Promise; listTools(options?: { refresh?: boolean; }): Promise; uiIndex(): Promise; readResource(uri: string): Promise; callTool(name: string, args: Record): Promise; /** Forget the connection (and with it the tool list + index). */ reset(): Promise; private invalidateTools; private toolsStale; private dropIfDead; } declare class McpClientPool { private readonly opener; private readonly options; private clients; constructor(opener?: McpClientOpener, options?: PooledMcpClientOptions); /** The pooled client for `config`, created on first use. `label` only * names the server in config errors. */ get(config: McpConnectionConfig, label: string): PooledMcpClient; get size(): number; closeAll(): Promise; } export { DEAD_CONNECTION_RE, DEFAULT_TOOL_CACHE_TTL_MS, MCP_APPS_CLIENT_CAPABILITIES, type McpClientOpener, McpClientPool, type McpConnectionConfig, type McpUiToolEntry, type McpUiToolIndex, type OpenMcpClientOptions, PooledMcpClient, type PooledMcpClientOptions, buildUiToolIndex, callerForwardingFetch, currentCallHeaders, expandEnvPlaceholders, isUnknownToolError, mcpConfigKey, openMcpClient, resultIsUnknownTool, safeClose, shouldForwardCallerHeaders, withCallHeaders };