import { CancellationToken } from "../../../../base/common/cancellation.js";
import { Disposable, IDisposable } from "../../../../base/common/lifecycle.js";
import { IObservable } from "../../../../base/common/observable.js";
import { URI } from "../../../../base/common/uri.js";
import { ICommandService } from "../../../../platform/commands/common/commands.service.js";
import { IInstantiationService } from "../../../../platform/instantiation/common/instantiation.js";
import { ILogger } from "../../../../platform/log/common/log.js";
import { ILoggerService } from "../../../../platform/log/common/log.service.js";
import { INotificationService } from "../../../../platform/notification/common/notification.service.js";
import { IOpenerService } from "../../../../platform/opener/common/opener.service.js";
import { StorageScope } from "../../../../platform/storage/common/storage.js";
import { IStorageService } from "../../../../platform/storage/common/storage.service.js";
import { ITelemetryService } from "../../../../platform/telemetry/common/telemetry.service.js";
import { IWorkspaceContextService } from "../../../../platform/workspace/common/workspace.service.js";
import { IWorkbenchEnvironmentService } from "../../../services/environment/common/environmentService.service.js";
import { IExtensionService } from "../../../services/extensions/common/extensions.service.js";
import { IOutputService } from "../../../services/output/common/output.service.js";
import { ToolProgress } from "../../chat/common/tools/languageModelToolsService.js";
import { StoredMcpIcons } from "./mcpIcons.js";
import { IMcpRegistry } from "./mcpRegistryTypes.service.js";
import { McpServerRequestHandler } from "@codingame/monaco-vscode-mcp-service-override/vscode/vs/workbench/contrib/mcp/common/mcpServerRequestHandler";
import { IMcpIcons, IMcpPrompt, IMcpResource, IMcpResourceTemplate, IMcpServer, IMcpServerConnection, IMcpServerStartOpts, IMcpTool, IMcpToolCallContext, McpCapability, McpCollectionDefinition, McpCollectionReference, McpConnectionState, McpDefinitionReference, McpServerCacheState, McpServerDefinition, McpToolVisibility } from "./mcpTypes.js";
import { IMcpElicitationService } from "./mcpTypes.service.js";
import { IMcpSamplingService } from "./mcpTypes.service.js";
import { MCP } from "./modelContextProtocol.js";
export type McpServerInstallData = {
    serverName: string;
    source: "gallery" | "local";
    scope: string;
    success: boolean;
    error?: string;
    hasInputs: boolean;
};
export type McpServerInstallClassification = {
    owner: "connor4312";
    comment: "MCP server installation event tracking";
    serverName: {
        classification: "SystemMetaData";
        purpose: "FeatureInsight";
        comment: "The name of the MCP server being installed";
    };
    source: {
        classification: "SystemMetaData";
        purpose: "FeatureInsight";
        comment: "Installation source (gallery or local)";
    };
    scope: {
        classification: "SystemMetaData";
        purpose: "FeatureInsight";
        comment: "Installation scope (user, workspace, etc.)";
    };
    success: {
        classification: "SystemMetaData";
        purpose: "FeatureInsight";
        isMeasurement: true;
        comment: "Whether installation succeeded";
    };
    error?: {
        classification: "CallstackOrException";
        purpose: "FeatureInsight";
        comment: "Error message if installation failed";
    };
    hasInputs: {
        classification: "SystemMetaData";
        purpose: "FeatureInsight";
        isMeasurement: true;
        comment: "Whether the server requires input configuration";
    };
};
type StoredMcpPrompt = MCP.Prompt & {
    _icons: StoredMcpIcons;
};
interface IToolCacheEntry {
    readonly serverName: string | undefined;
    readonly serverInstructions: string | undefined;
    readonly serverIcons: StoredMcpIcons;
    readonly trustedAtNonce: string | undefined;
    readonly nonce: string | undefined;
    /** Cached tools so we can show what's available before it's started */
    readonly tools: readonly ValidatedMcpTool[];
    /** Cached prompts */
    readonly prompts: readonly StoredMcpPrompt[] | undefined;
    /** Cached capabilities */
    readonly capabilities: McpCapability | undefined;
}
interface IServerCacheEntry {
    readonly servers: readonly McpServerDefinition.Serialized[];
}
export declare class McpServerMetadataCache extends Disposable {
    private didChange;
    private readonly cache;
    private readonly extensionServers;
    constructor(scope: StorageScope, storageService: IStorageService);
    /** Resets the cache for primitives and extension servers */
    reset(): void;
    /** Gets cached primitives for a server (used before a server is running) */
    get(definitionId: string): IToolCacheEntry | undefined;
    /** Sets cached primitives for a server */
    store(definitionId: string, entry: Partial<IToolCacheEntry>): void;
    /** Gets cached servers for a collection (used for extensions, before the extension activates) */
    getServers(collectionId: string): IServerCacheEntry | undefined;
    /** Sets cached servers for a collection */
    storeServers(collectionId: string, entry: IServerCacheEntry | undefined): void;
}
type ValidatedMcpTool = MCP.Tool & {
    _icons: StoredMcpIcons;
    /**
     * Tool name as published by the MCP server. This may
     * be different than the one in {@link definition} due to name normalization
     * in {@link McpServer._getValidatedTools}.
     */
    serverToolName: string;
    /**
     * Visibility of the tool, parsed from `_meta.ui.visibility`.
     * Defaults to Model | App if not specified.
     */
    visibility: McpToolVisibility;
    /**
     * UI resource URI if this tool has an associated MCP App UI.
     * Parsed from `_meta.ui.resourceUri`.
     */
    uiResourceUri?: string;
};
interface ServerMetadata {
    readonly serverName: string | undefined;
    readonly serverInstructions: string | undefined;
    readonly icons: IMcpIcons;
}
export declare class McpServer extends Disposable implements IMcpServer {
    readonly definition: McpDefinitionReference;
    private readonly _requiresExtensionActivation;
    private readonly _primitiveCache;
    private readonly _mcpRegistry;
    private readonly _extensionService;
    private readonly _loggerService;
    private readonly _outputService;
    private readonly _telemetryService;
    private readonly _commandService;
    private readonly _instantiationService;
    private readonly _notificationService;
    private readonly _openerService;
    private readonly _samplingService;
    private readonly _elicitationService;
    /** Shared task manager that survives reconnections */
    private readonly _taskManager;
    /**
     * Helper function to call the function on the handler once it's online. The
     * connection started if it is not already.
     */
    static callOn<R>(server: IMcpServer, fn: (handler: McpServerRequestHandler) => Promise<R>, token?: CancellationToken): Promise<R>;
    readonly collection: McpCollectionReference;
    private readonly _connectionSequencer;
    private readonly _connection;
    readonly connection: import("../../../../base/common/observable.js").ISettableObservable<IMcpServerConnection | undefined, void> & IDisposable;
    readonly connectionState: IObservable<McpConnectionState>;
    private readonly _capabilities;
    get capabilities(): IObservable<number | undefined>;
    private readonly _tools;
    get tools(): IObservable<readonly IMcpTool[]>;
    private readonly _prompts;
    get prompts(): IObservable<readonly IMcpPrompt[]>;
    private readonly _serverMetadata;
    get serverMetadata(): IObservable<ServerMetadata>;
    get trustedAtNonce(): string | undefined;
    set trustedAtNonce(nonce: string | undefined);
    private readonly _fullDefinitions;
    readonly cacheState: import("../../../../base/common/observable.js").IObservableWithChange<McpServerCacheState, void>;
    get logger(): ILogger;
    private readonly _loggerId;
    private readonly _logger;
    private _lastModeDebugged;
    /** Count of running tool calls, used to detect if sampling is during an LM call */
    runningToolCalls: Set<IMcpToolCallContext>;
    constructor(initialCollection: McpCollectionDefinition, definition: McpDefinitionReference, explicitRoots: URI[] | undefined, _requiresExtensionActivation: boolean | undefined, _primitiveCache: McpServerMetadataCache, toolPrefix: string, _mcpRegistry: IMcpRegistry, workspacesService: IWorkspaceContextService, _extensionService: IExtensionService, _loggerService: ILoggerService, _outputService: IOutputService, _telemetryService: ITelemetryService, _commandService: ICommandService, _instantiationService: IInstantiationService, _notificationService: INotificationService, _openerService: IOpenerService, _samplingService: IMcpSamplingService, _elicitationService: IMcpElicitationService, environmentService: IWorkbenchEnvironmentService);
    readDefinitions(): IObservable<{
        server: McpServerDefinition | undefined;
        collection: McpCollectionDefinition | undefined;
    }>;
    showOutput(preserveFocus?: boolean): Promise<void>;
    resources(token?: CancellationToken): AsyncIterable<IMcpResource[]>;
    resourceTemplates(token?: CancellationToken): Promise<IMcpResourceTemplate[]>;
    start({ interaction, autoTrustChanges, promptType, debug, errorOnUserInteraction }?: IMcpServerStartOpts): Promise<McpConnectionState>;
    private showInteractiveError;
    stop(): Promise<void>;
    /** Waits for any ongoing tools to be refreshed before resolving. */
    awaitToolRefresh(): Promise<void>;
    private resetLiveData;
    private _normalizeTool;
    private _getValidatedTools;
    /**
     * Parses incoming MCP icons and returns the resulting 'stored' record. Note
     * that this requires an active MCP server connection since we validate
     * against some of that connection's data. The icons may however be stored
     * and rehydrated later.
     */
    private _parseIcons;
    private _setServerTools;
    private _setServerPrompts;
    private _toStoredMetadata;
    private _setServerMetadata;
    private _populateLiveData;
}
export declare class McpTool implements IMcpTool {
    private readonly _server;
    private readonly _definition;
    private readonly _elicitationService;
    readonly id: string;
    readonly referenceName: string;
    readonly icons: IMcpIcons;
    readonly visibility: McpToolVisibility;
    get definition(): MCP.Tool;
    get uiResourceUri(): string | undefined;
    constructor(_server: McpServer, idPrefix: string, _definition: ValidatedMcpTool, _elicitationService: IMcpElicitationService);
    call(params: Record<string, unknown>, context?: IMcpToolCallContext, token?: CancellationToken): Promise<MCP.CallToolResult>;
    callWithProgress(params: Record<string, unknown>, progress: ToolProgress, context?: IMcpToolCallContext, token?: CancellationToken): Promise<MCP.CallToolResult>;
    _callWithProgress(params: Record<string, unknown>, progress: ToolProgress | undefined, context?: IMcpToolCallContext, token?: Readonly<CancellationToken>, allowRetry?: boolean): Promise<MCP.CallToolResult>;
    private _handleElicitationErr;
    compare(other: IMcpTool): number;
}
export {};
