Chrome DevTools for coding agents
fix: set empty filePath to undefined instead of empty string (#2836)
Nicholas Roscinoadfc1ffa80fd
diff --git a/src/ToolHandler.ts b/src/ToolHandler.tsindex 2df3a290..19c3d1c3 100644--- a/src/ToolHandler.ts+++ b/src/ToolHandler.ts@@ -74,7 +74,10 @@ function isPageScopedTool(async function validateAndResolvePathOrUrl(filePathOrUrl: string,context: McpContext,-): Promise<string> {+): Promise<string | undefined> {+ if (filePathOrUrl.trim().length === 0) {+ return undefined;+ }try {const url = new URL(filePathOrUrl);if (url.protocol === 'file:') {@@ -130,7 +133,10 @@ async function validateToolFiles(const updated: unknown[] = [];for (const item of val) {if (typeof item === 'string') {- updated.push(await validateAndResolvePathOrUrl(item, context));+ const resolved = await validateAndResolvePathOrUrl(item, context);+ if (resolved !== undefined) {+ updated.push(resolved);+ }} else {throw new Error('Unexpected non-string value as a file path or URL',diff --git a/tests/ToolHandler.test.ts b/tests/ToolHandler.test.tsindex ab02f269..2d35baf1 100644--- a/tests/ToolHandler.test.ts+++ b/tests/ToolHandler.test.ts@@ -26,6 +26,7 @@ import {type ToolDefinition,} from '../src/tools/ToolDefinition.js';import {createTools} from '../src/tools/tools.js';+import {createMockMcpContext} from './mocks.js';import {getMockBrowser} from './utils.js';import {Mutex} from '../src/third_party/index.js';@@ -1120,4 +1121,53 @@ describe('ToolHandler', () => {filePath: canonicalFilePath,});});++ it('skips validation and clears empty or whitespace-only file paths in params', async () => {+ let receivedParams: Record<string, unknown> | undefined;+ const tool: ToolDefinition = {+ name: 'file_tool',+ description: 'A tool with file verification',+ annotations: {+ category: ToolCategory.DEBUGGING,+ readOnlyHint: false,+ },+ schema: {+ filePath: zod.string().optional(),+ filePaths: zod.array(zod.string()).optional(),+ },+ blockedByDialog: false,+ verifyFilesSchema: {+ filePath: true,+ filePaths: true,+ },+ handler: async request => {+ receivedParams = request.params;+ },+ };++ const mockContext = createMockMcpContext();+ mockContext.browser = getMockBrowser();+ const serverArgs = parseArguments('1.0.0', ['node', 'script.js'], {+ CHROME_DEVTOOLS_MCP_NO_USAGE_STATISTICS: 'true',+ });++ const toolHandler = new ToolHandler(+ tool,+ serverArgs,+ async () => mockContext,+ new Mutex(),+ );++ const result = await toolHandler.handle({+ filePath: ' ',+ filePaths: [' ', ''],+ });++ assert.strictEqual(result.isError, undefined);+ sinon.assert.notCalled(mockContext.validatePath);+ assert.deepStrictEqual(receivedParams, {+ filePath: undefined,+ filePaths: [],+ });+ });});