2020 * | `--tools` | `SFCC_TOOLS` | Comma-separated individual tools to enable (case-insensitive) |
2121 * | `--allow-non-ga-tools` | `SFCC_ALLOW_NON_GA_TOOLS` | Enable experimental/non-GA tools |
2222 *
23- * ### Environment Variables (no flag equivalent)
23+ * ### Environment Variables for Telemetry
2424 * | Env Variable | Description |
2525 * |--------------|-------------|
26- * | `SFCC_TELEMETRY` | Set to `false` to disable telemetry collection |
26+ * | `SF_DISABLE_TELEMETRY` | Set to `true` to disable telemetry (sf CLI standard) |
27+ * | `SFCC_DISABLE_TELEMETRY` | Set to `true` to disable telemetry |
28+ * | `SFCC_APP_INSIGHTS_KEY` | Override connection string from package.json |
2729 *
2830 * ### MRT Flags (from MrtCommand.baseFlags)
2931 * | Flag | Env Variable | Description |
@@ -143,12 +145,10 @@ import {
143145} from '@salesforce/b2c-tooling-sdk/cli' ;
144146import type { LoadConfigOptions } from '@salesforce/b2c-tooling-sdk/cli' ;
145147import type { ResolvedB2CConfig } from '@salesforce/b2c-tooling-sdk/config' ;
146- import { createTelemetry , type Telemetry } from '@salesforce/b2c-tooling-sdk/telemetry' ;
147148import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js' ;
148149import { B2CDxMcpServer } from '../server.js' ;
149150import { Services } from '../services.js' ;
150151import { registerToolsets } from '../registry.js' ;
151- import { APPLICATION_INSIGHTS_CONNECTION_STRING } from '../config.js' ;
152152import { TOOLSETS , type StartupFlags } from '../utils/index.js' ;
153153
154154/**
@@ -159,6 +159,7 @@ import {TOOLSETS, type StartupFlags} from '../utils/index.js';
159159 * - Global flags for config, logging, and debugging
160160 * - Structured pino logging via `this.logger`
161161 * - Automatic dw.json loading via `this.resolvedConfig`
162+ * - Automatic telemetry initialization via `this.telemetry`
162163 * - `this.config` - package.json metadata and standard config paths
163164 */
164165export default class McpServerCommand extends BaseCommand < typeof McpServerCommand > {
@@ -254,9 +255,9 @@ export default class McpServerCommand extends BaseCommand<typeof McpServerComman
254255 * Main entry point - starts the MCP server.
255256 *
256257 * Execution flow:
257- * 1. BaseCommand.init() parses flags and loads config
258+ * 1. BaseCommand.init() parses flags, loads config, and initializes telemetry
258259 * 2. Filter and validate toolsets (invalid ones are skipped with warning)
259- * 3. Create B2CDxMcpServer instance
260+ * 3. Create B2CDxMcpServer instance with telemetry from BaseCommand
260261 * 4. Create Services via Services.fromResolvedConfig() using already-resolved config
261262 * 5. Register tools based on --toolsets and --tools flags
262263 * 6. Connect to stdio transport (JSON-RPC over stdin/stdout)
@@ -268,6 +269,7 @@ export default class McpServerCommand extends BaseCommand<typeof McpServerComman
268269 * - `this.flags` - Parsed flags including global flags (config, debug, log-level, etc.)
269270 * - `this.resolvedConfig` - Loaded dw.json configuration
270271 * - `this.logger` - Structured pino logger
272+ * - `this.telemetry` - Telemetry instance (auto-initialized from package.json config)
271273 *
272274 * oclif provides standard config paths via `this.config`:
273275 * - `this.config.configDir` - User config (~/.config/b2c-dx-mcp)
@@ -288,25 +290,12 @@ export default class McpServerCommand extends BaseCommand<typeof McpServerComman
288290 workingDirectory : this . flags [ 'working-directory' ] ,
289291 } ;
290292
291- // Initialize telemetry unless disabled via SFCC_TELEMETRY=false
292- let telemetry : Telemetry | undefined ;
293- if ( process . env . SFCC_TELEMETRY !== 'false' ) {
294- telemetry = createTelemetry ( {
295- project : 'b2c-dx-mcp' ,
296- appInsightsKey : APPLICATION_INSIGHTS_CONNECTION_STRING ,
297- version : this . config . version ,
298- initialAttributes : {
299- toolsets : ( startupFlags . toolsets ?? [ ] ) . join ( ', ' ) ,
300- } ,
301- } ) ;
302- await telemetry . start ( ) ;
303- process . stdin . on ( 'close' , ( err ) => {
304- telemetry ?. sendEvent ( err ? 'SERVER_STOPPED_ERROR' : 'SERVER_STOPPED_SUCCESS' ) ;
305- telemetry ?. stop ( ) ;
306- } ) ;
293+ // Add toolsets to telemetry attributes
294+ if ( this . telemetry && startupFlags . toolsets ) {
295+ this . telemetry . addAttributes ( { toolsets : startupFlags . toolsets . join ( ', ' ) } ) ;
307296 }
308297
309- // Create MCP server with telemetry
298+ // Create MCP server with telemetry from BaseCommand
310299 const server = new B2CDxMcpServer (
311300 {
312301 name : this . config . name ,
@@ -317,7 +306,7 @@ export default class McpServerCommand extends BaseCommand<typeof McpServerComman
317306 resources : { } ,
318307 tools : { } ,
319308 } ,
320- telemetry,
309+ telemetry : this . telemetry ,
321310 } ,
322311 ) ;
323312
@@ -331,6 +320,13 @@ export default class McpServerCommand extends BaseCommand<typeof McpServerComman
331320 const transport = new StdioServerTransport ( ) ;
332321 await server . connect ( transport ) ;
333322
323+ // Track server stop when stdin closes (MCP client disconnects)
324+ // Note: The 'close' event has no arguments - it's just a signal that the stream closed
325+ process . stdin . on ( 'close' , ( ) => {
326+ this . telemetry ?. sendEvent ( 'SERVER_STOPPED' ) ;
327+ // Don't call stop() here - let finally() handle telemetry cleanup
328+ } ) ;
329+
334330 // Log startup message using the structured logger
335331 this . logger . info ( { version : this . config . version } , 'MCP Server running on stdio' ) ;
336332 }
0 commit comments