Skip to content

Commit f8a74c2

Browse files
committed
Refactoring changes plus updated tests
1 parent fbc6dda commit f8a74c2

11 files changed

Lines changed: 1390 additions & 262 deletions

File tree

packages/b2c-cli/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
"bin": "b2c",
7878
"dirname": "b2c",
7979
"commands": "./dist/commands",
80+
"telemetry": {
81+
"connectionString": "InstrumentationKey=6fcc215f-0b11-4864-ad5c-3945ae19e2f3;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=a60f17ec-265a-4dfc-b8df-03a64695697d"
82+
},
8083
"plugins": [
8184
"@oclif/plugin-help",
8285
"@oclif/plugin-plugins",

packages/b2c-dx-mcp/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
"strategy": "single",
3535
"target": "./dist/commands/mcp.js"
3636
},
37-
"topicSeparator": " "
37+
"topicSeparator": " ",
38+
"telemetry": {
39+
"connectionString": "InstrumentationKey=6fcc215f-0b11-4864-ad5c-3945ae19e2f3;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=a60f17ec-265a-4dfc-b8df-03a64695697d"
40+
}
3841
},
3942
"files": [
4043
"bin",

packages/b2c-dx-mcp/src/commands/mcp.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
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';
144146
import type {LoadConfigOptions} from '@salesforce/b2c-tooling-sdk/cli';
145147
import type {ResolvedB2CConfig} from '@salesforce/b2c-tooling-sdk/config';
146-
import {createTelemetry, type Telemetry} from '@salesforce/b2c-tooling-sdk/telemetry';
147148
import {StdioServerTransport} from '@modelcontextprotocol/sdk/server/stdio.js';
148149
import {B2CDxMcpServer} from '../server.js';
149150
import {Services} from '../services.js';
150151
import {registerToolsets} from '../registry.js';
151-
import {APPLICATION_INSIGHTS_CONNECTION_STRING} from '../config.js';
152152
import {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
*/
164165
export 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
}

packages/b2c-dx-mcp/src/config.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,21 @@
33
* SPDX-License-Identifier: Apache-2
44
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
55
*/
6+
67
/**
7-
* Configuration constants.
8+
* Configuration module.
9+
*
10+
* Telemetry configuration has been moved to package.json under oclif.telemetry.connectionString.
11+
* Telemetry is now auto-initialized by BaseCommand from the SDK.
12+
*
13+
* Environment variables for telemetry:
14+
* - SF_DISABLE_TELEMETRY=true - Disable telemetry (sf CLI standard)
15+
* - SFCC_DISABLE_TELEMETRY=true - Disable telemetry
16+
* - SFCC_APP_INSIGHTS_KEY - Override connection string from package.json
817
*
918
* @module config
1019
*/
1120

12-
const DEFAULT_APPLICATION_INSIGHTS_CONNECTION_STRING =
13-
'InstrumentationKey=6fcc215f-0b11-4864-ad5c-3945ae19e2f3;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=a60f17ec-265a-4dfc-b8df-03a64695697d';
14-
15-
/**
16-
* Determines if telemetry should be enabled based on environment.
17-
* Telemetry is disabled in development mode to avoid polluting production data.
18-
*/
19-
function getConnectionString(): string | undefined {
20-
// Allow explicit override via env var
21-
if (process.env.B2C_DX_MCP_APP_INSIGHTS_CONNECTION_STRING) {
22-
return process.env.B2C_DX_MCP_APP_INSIGHTS_CONNECTION_STRING;
23-
}
24-
25-
// Disable telemetry in development mode
26-
if (process.env.NODE_ENV === 'development') {
27-
return undefined;
28-
}
29-
30-
return DEFAULT_APPLICATION_INSIGHTS_CONNECTION_STRING;
31-
}
32-
33-
/**
34-
* Application Insights connection string for telemetry.
35-
*
36-
* Returns undefined in development mode (NODE_ENV=development) to disable telemetry.
37-
* Can be overridden via the `B2C_DX_MCP_APP_INSIGHTS_CONNECTION_STRING` environment variable.
38-
*/
39-
export const APPLICATION_INSIGHTS_CONNECTION_STRING = getConnectionString();
21+
// Export placeholder to satisfy ESLint "no empty files" rule
22+
// Configuration constants can be added here as needed
23+
export const CONFIG_MODULE_VERSION = '1.0.0';

0 commit comments

Comments
 (0)