/** * Services module providing dependency injection for MCP tools. * * The {@link Services} class is the central dependency container for tools, * providing: * - Pre-resolved B2CInstance for WebDAV/OCAPI operations * - Pre-resolved MRT authentication for Managed Runtime operations * - MRT project/environment configuration * - File system utilities for local operations * * ## Creating Services * * Use {@link Services.fromResolvedConfig} with an already-resolved configuration: * * ```typescript * // In a command that extends BaseCommand * const services = Services.fromResolvedConfig(this.resolvedConfig); * ``` * * ## Resolution Pattern * * Both B2CInstance and MRT auth are resolved once at server startup (not on each tool call). * This provides fail-fast behavior and consistent performance. * * **B2C Instance** (for WebDAV/OCAPI tools): * - Flags (highest priority) merged with dw.json (auto-discovered or via --config) * * **MRT Auth** (for Managed Runtime tools): * 1. `--api-key` flag (oclif also checks `MRT_API_KEY` env var; `SFCC_MRT_API_KEY` also supported) * 2. `~/.mobify` config file (or `~/.mobify--[hostname]` if `--cloud-origin` is set) * * **MRT Origin** (for Managed Runtime API URL): * 1. `--cloud-origin` flag (oclif also checks `MRT_CLOUD_ORIGIN` env var; `SFCC_MRT_CLOUD_ORIGIN` also supported) * 2. `mrtOrigin` field in dw.json * 3. Default: `https://cloud.mobify.com` * * @module services */ import fs from 'node:fs'; import type { B2CInstance } from '@salesforce/b2c-tooling-sdk'; import type { AuthStrategy } from '@salesforce/b2c-tooling-sdk/auth'; import type { ResolvedB2CConfig } from '@salesforce/b2c-tooling-sdk/config'; import { WebDavClient, type CustomApisClient, type MetricsClient, type ScapiSchemasClient } from '@salesforce/b2c-tooling-sdk/clients'; /** * MRT (Managed Runtime) configuration. * Groups auth, project, environment, and origin settings. */ export interface MrtConfig { /** Pre-resolved auth strategy for MRT API operations */ auth?: AuthStrategy; /** MRT project slug from --project flag or MRT_PROJECT env var */ project?: string; /** MRT environment from --environment flag or MRT_ENVIRONMENT env var */ environment?: string; /** MRT API origin URL from --cloud-origin flag, MRT_CLOUD_ORIGIN env var, or mrtOrigin in dw.json */ origin?: string; } /** * Options for Services constructor (internal). */ export interface ServicesOptions { /** Pre-resolved B2C instance (if configured) */ b2cInstance?: B2CInstance; /** Pre-resolved MRT configuration (auth, project, environment) */ mrtConfig?: MrtConfig; /** Resolved configuration for access to SCAPI settings */ resolvedConfig: ResolvedB2CConfig; } /** * Services class that provides utilities for MCP tools. * * Use the static `Services.fromResolvedConfig()` factory method to create * an instance from an already-resolved configuration. * * @example * ```typescript * // In a command that extends BaseCommand * const services = Services.fromResolvedConfig(this.resolvedConfig); * * // Access resolved config * services.b2cInstance; // B2CInstance | undefined * services.mrtConfig.auth; // AuthStrategy | undefined * services.mrtConfig.project; // string | undefined * ``` */ export declare class Services { /** * Pre-resolved B2C instance for WebDAV/OCAPI operations. * Resolved once at server startup from InstanceCommand flags and dw.json. * Undefined if no B2C instance configuration was available. */ readonly b2cInstance?: B2CInstance; /** * Pre-resolved MRT configuration (auth, project, environment, origin). * Resolved once at server startup from MrtCommand flags and ~/.mobify. */ readonly mrtConfig: MrtConfig; /** * Resolved configuration for accessing SCAPI settings. * Provides access to shortCode, tenantId, and OAuth credentials. * @private */ private readonly resolvedConfig; constructor(opts: ServicesOptions); /** * Creates a Services instance from an already-resolved configuration. * * @param config - Already-resolved configuration from BaseCommand.resolvedConfig * @returns Services instance with resolved config * * @example * ```typescript * // In a command that extends BaseCommand * const services = Services.fromResolvedConfig(this.resolvedConfig); * ``` */ static fromResolvedConfig(config: ResolvedB2CConfig): Services; /** * Check if a file or directory exists. * * @param targetPath - Path to check * @returns True if exists, false otherwise */ exists(targetPath: string): boolean; /** * Get Basic auth credentials for SDAPI operations (script debugger). * Returns undefined if credentials are not configured. */ getBasicAuthCredentials(): undefined | { hostname: string; username: string; password: string; }; /** * Get Custom APIs client for managing custom SCAPI endpoints. * Requires shortCode, tenantId, and OAuth credentials to be configured. * * @throws Error if shortCode, tenantId, or OAuth credentials are missing * @returns Typed Custom APIs client */ getCustomApisClient(): CustomApisClient; /** * Get the current working directory. */ getCwd(): string; /** * Get the user's home directory. */ getHomeDir(): string; /** * Get Metrics client for accessing SCAPI observability metrics. * Requires shortCode, tenantId, and OAuth credentials to be configured. * * @throws Error if shortCode, tenantId, or OAuth credentials are missing * @returns Typed Metrics client */ getMetricsClient(): MetricsClient; /** * Get organization ID for SCAPI API calls. * Ensures the tenant ID has the required f_ecom_ prefix. * * @throws Error if tenantId is not configured * @returns Organization ID with f_ecom_ prefix */ getOrganizationId(): string; /** * Get OS platform information. */ getPlatform(): NodeJS.Platform; /** * Get SCAPI Schemas client for discovering available SCAPI APIs. * Requires shortCode, tenantId, and OAuth credentials to be configured. * * @throws Error if shortCode, tenantId, or OAuth credentials are missing * @returns Typed SCAPI Schemas client */ getScapiSchemasClient(): ScapiSchemasClient; /** * Get SCAPI shortCode from configuration. * Returns undefined if not configured. * * @returns shortCode or undefined */ getShortCode(): string | undefined; /** * Get tenant ID from configuration. * Returns undefined if not configured. * * @returns tenantId or undefined */ getTenantId(): string | undefined; /** * Get system temporary directory. */ getTmpDir(): string; /** * Get WebDAV client for file operations on B2C instances. * Requires hostname and WebDAV credentials to be configured. * * @throws Error if hostname or B2C instance is missing * @returns WebDAV client instance */ getWebDavClient(): WebDavClient; /** * Join path segments. * * @param segments - Path segments to join * @returns Joined path */ joinPath(...segments: string[]): string; /** * List directory contents. * * @param dirPath - Directory path to list * @returns Array of directory entries */ listDirectory(dirPath: string): fs.Dirent[]; /** * Read a file from the filesystem. * * @param filePath - Path to the file * @param encoding - File encoding (default: utf8) * @returns File contents as a string */ readFile(filePath: string, encoding?: 'ascii' | 'base64' | 'hex' | 'latin1' | 'utf8'): string; /** * Resolve a path relative to the current working directory. * * @param segments - Path segments to join and resolve * @returns Absolute path */ resolvePath(...segments: string[]): string; /** * Resolve a path relative to the project directory. * If path is not supplied, returns the project directory. * If path is absolute, returns it as-is. * If path is relative, resolves it relative to the project directory. * * @param pathArg - Optional path to resolve * @returns Resolved absolute path */ resolveWithProjectDirectory(pathArg?: string): string; /** * Get file or directory stats. * * @param targetPath - Path to get stats for * @returns File stats object */ stat(targetPath: string): fs.Stats; /** * Get OAuth strategy from resolved configuration. * Mirrors the pattern from OAuthCommand.getOAuthStrategy(). * * @throws Error if OAuth credentials are not configured * @returns OAuth auth strategy * @private */ private getOAuthStrategy; }