Skip to content

Commit 6bd2a02

Browse files
@W-21199544 MCP respect working-directory flag in cartidge and mrt tools
1 parent a09e257 commit 6bd2a02

File tree

8 files changed

+54
-6
lines changed

8 files changed

+54
-6
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,19 @@ export class Services {
313313
return this.b2cInstance.webdav;
314314
}
315315

316+
/**
317+
* Get the project working directory.
318+
* Falls back to process.cwd() if not explicitly set.
319+
*
320+
* This is the directory where the project is located, which may differ from process.cwd()
321+
* when MCP clients spawn servers from a different location (e.g., home directory).
322+
*
323+
* @returns Project working directory path
324+
*/
325+
public getWorkingDirectory(): string {
326+
return this.resolvedConfig.values.startDir ?? process.cwd();
327+
}
328+
316329
/**
317330
* Join path segments.
318331
*

packages/b2c-dx-mcp/src/tools/cartridges/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function createCartridgeDeployTool(services: Services, injections?: CartridgeToo
106106
const instance = context.b2cInstance!;
107107

108108
// Default directory to current directory
109-
const directory = args.directory || '.';
109+
const directory = args.directory || services.getWorkingDirectory();
110110

111111
// Parse options
112112
const options: DeployOptions = {

packages/b2c-dx-mcp/src/tools/mrt/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* @module tools/mrt
1313
*/
1414

15+
import path from 'node:path';
1516
import {z} from 'zod';
1617
import type {McpTool} from '../../utils/index.js';
1718
import type {Services} from '../../services.js';
@@ -96,7 +97,7 @@ function createMrtBundlePushTool(services: Services, injections?: MrtToolInjecti
9697
// Parse comma-separated glob patterns (same as CLI defaults)
9798
const ssrOnly = (args.ssrOnly || 'ssr.js,ssr.mjs,server/**/*').split(',').map((s) => s.trim());
9899
const ssrShared = (args.ssrShared || 'static/**/*,client/**/*').split(',').map((s) => s.trim());
99-
const buildDirectory = args.buildDirectory || './build';
100+
const buildDirectory = args.buildDirectory || path.join(services.getWorkingDirectory(), 'build');
100101

101102
// Log all computed variables before pushing bundle
102103
const logger = getLogger();

packages/b2c-dx-mcp/test/services.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,38 @@ describe('services', () => {
172172
});
173173
});
174174

175+
describe('getWorkingDirectory', () => {
176+
it('should return working directory when provided in config', () => {
177+
const workingDir = '/path/to/project';
178+
const config = createMockResolvedConfig({startDir: workingDir});
179+
const services = new Services({resolvedConfig: config});
180+
181+
expect(services.getWorkingDirectory()).to.equal(workingDir);
182+
});
183+
184+
it('should fall back to process.cwd() when not provided', () => {
185+
const config = createMockResolvedConfig();
186+
const services = new Services({resolvedConfig: config});
187+
188+
expect(services.getWorkingDirectory()).to.equal(process.cwd());
189+
});
190+
191+
it('should return working directory from fromResolvedConfig when provided in config', () => {
192+
const workingDir = '/path/to/project';
193+
const config = createMockResolvedConfig({startDir: workingDir});
194+
const services = Services.fromResolvedConfig(config);
195+
196+
expect(services.getWorkingDirectory()).to.equal(workingDir);
197+
});
198+
199+
it('should fall back to process.cwd() from fromResolvedConfig when not provided in config', () => {
200+
const config = createMockResolvedConfig();
201+
const services = Services.fromResolvedConfig(config);
202+
203+
expect(services.getWorkingDirectory()).to.equal(process.cwd());
204+
});
205+
});
206+
175207
describe('getHomeDir', () => {
176208
it('should return home directory', () => {
177209
const config = createMockResolvedConfig();

packages/b2c-dx-mcp/test/tools/cartridges/index.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ describe('tools/cartridges', () => {
118118

119119
describe('cartridge_deploy execution', () => {
120120
it('should call findAndDeployCartridges with instance and default directory', async () => {
121-
const directory = '.';
122-
123121
const mockResult: DeployResult = {
124122
cartridges: [{name: 'app_storefront_base', src: '/path/to/app_storefront_base', dest: 'app_storefront_base'}],
125123
codeVersion: 'v1',
@@ -143,7 +141,7 @@ describe('tools/cartridges', () => {
143141
DeployOptions,
144142
];
145143
expect(instance).to.equal(mockInstance);
146-
expect(dir).to.equal(directory);
144+
expect(dir).to.equal(services.getWorkingDirectory());
147145
expect(options.include).to.be.undefined;
148146
expect(options.exclude).to.be.undefined;
149147
expect(options.reload).to.be.undefined;

packages/b2c-tooling-sdk/src/cli/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,11 @@ export function loadConfig(
236236
): ResolvedB2CConfig {
237237
const logger = getLogger();
238238

239-
// Preserve instanceName from options.instance if not already in flags
239+
// Preserve instanceName and startDir from options if not already in flags
240240
const effectiveFlags = {
241241
...flags,
242242
instanceName: flags.instanceName ?? options.instance,
243+
startDir: flags.startDir ?? options.startDir,
243244
};
244245

245246
const resolved = resolveConfig(effectiveFlags, {

packages/b2c-tooling-sdk/src/config/mapping.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ export function mergeConfigsWithProtection(
316316
cipHost: overrides.cipHost ?? base.cipHost,
317317
sandboxApiHost: overrides.sandboxApiHost ?? base.sandboxApiHost,
318318
instanceName: overrides.instanceName ?? base.instanceName,
319+
startDir: overrides.startDir ?? base.startDir,
319320
mrtProject: overrides.mrtProject ?? base.mrtProject,
320321
mrtEnvironment: overrides.mrtEnvironment ?? base.mrtEnvironment,
321322
mrtApiKey: overrides.mrtApiKey ?? base.mrtApiKey,

packages/b2c-tooling-sdk/src/config/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export interface NormalizedConfig {
7979
// Metadata
8080
/** Instance name (from multi-config supporting sources) */
8181
instanceName?: string;
82+
/** Starting directory for config file search and project-relative operations */
83+
startDir?: string;
8284

8385
// TLS/mTLS
8486
/** Path to PKCS12 certificate file for client mTLS (two-factor auth) */

0 commit comments

Comments
 (0)