n8n-nodes-smart-browser-automation 1.1.11 → 1.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -75,8 +75,10 @@ class BrowserSessionManager {
75
75
  throw new Error('MCP client not initialized. Please configure credentials first.');
76
76
  }
77
77
  try {
78
- // Log exact payload to debug truncation issues reported by users
79
- console.log(`[MCP] Calling tool "${toolName}" with args:`, JSON.stringify(toolArgs || {}));
78
+ // Only log in development/debug mode
79
+ if (process.env.NODE_ENV !== 'production' && process.env.DEBUG) {
80
+ console.log(`[MCP] Calling tool "${toolName}" with args:`, JSON.stringify(toolArgs || {}));
81
+ }
80
82
  const result = await this.mcpClient.callTool({
81
83
  name: toolName,
82
84
  arguments: toolArgs || {}
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.SmartBrowserAutomation = void 0;
7
7
  const n8n_workflow_1 = require("n8n-workflow");
8
- const DynamicBrowserTools_1 = require("./tools/DynamicBrowserTools");
9
8
  const BrowserSessionManager_1 = __importDefault(require("./BrowserSessionManager"));
10
9
  class SmartBrowserAutomation {
11
10
  description = {
@@ -194,32 +193,99 @@ class SmartBrowserAutomation {
194
193
  };
195
194
  // Expose tools to AI Agent nodes
196
195
  async getTools() {
197
- const credentials = await this.getCredentials('smartBrowserAutomationApi');
198
- const options = this.getNodeParameter('options', 0, {});
199
- const enabledTools = options.enabledTools || [];
200
- let tools = await (0, DynamicBrowserTools_1.createDynamicBrowserTools)(credentials);
201
- // Filter tools if a selection was made
202
- if (enabledTools.length > 0) {
203
- tools = tools.filter(tool => enabledTools.includes(tool.name));
204
- }
205
- // Sort tools to prioritize core actions
206
- const priorityTools = ['browser_navigate', 'browser_click', 'browser_type', 'browser_press_key', 'browser_scroll_to', 'browser_get_text'];
207
- tools.sort((a, b) => {
208
- const indexA = priorityTools.indexOf(a.name);
209
- const indexB = priorityTools.indexOf(b.name);
210
- if (indexA !== -1 && indexB !== -1)
211
- return indexA - indexB;
212
- if (indexA !== -1)
213
- return -1;
214
- if (indexB !== -1)
215
- return 1;
216
- return a.name.localeCompare(b.name);
217
- });
218
- // Add a custom tool for the AI to connect to a specific CDP URL
196
+ // To save tokens and prevent context overflow, we expose a single "Router Tool"
197
+ // instead of 54 individual tool definitions.
198
+ const routerTool = {
199
+ name: 'browser_tool',
200
+ displayName: 'Browser Action',
201
+ description: `Perform browser automation actions.
202
+ USAGE EXAMPLES:
203
+ - Navigate: action='navigate', params={ "url": "https://..." }
204
+ - Click: action='click', params={ "selector": "button.submit" }
205
+ - Type: action='type', params={ "selector": "#input", "text": "hello" }
206
+ - Scroll: action='scroll_to', params={ "selector": "footer" }
207
+ - Get Text: action='get_text', params={ "selector": ".content" }
208
+ - Screenshot: action='take_screenshot', params={}
209
+
210
+ Available actions: navigate, click, type, press_key, scroll_to, get_text, take_screenshot, evaluate, etc.
211
+ Supported Params depend on the action.`,
212
+ properties: [
213
+ {
214
+ displayName: 'Action Name',
215
+ name: 'action',
216
+ type: 'string',
217
+ required: true,
218
+ default: '',
219
+ },
220
+ {
221
+ displayName: 'Parameters',
222
+ name: 'params',
223
+ type: 'json',
224
+ default: '{}',
225
+ description: 'JSON parameters for the action (e.g., { "URL": "..." })',
226
+ },
227
+ ],
228
+ async execute(input) {
229
+ const sessionManager = BrowserSessionManager_1.default.getInstance();
230
+ const credentials = await this.getCredentials('smartBrowserAutomationApi');
231
+ // Ensure session is initialized
232
+ if (!sessionManager.isReady()) {
233
+ // Check for connection tool usage specifically
234
+ if (input.action === 'connect_cdp' || input.action === 'browser_connect_cdp') {
235
+ const endpoint = input.params?.endpoint;
236
+ if (endpoint) {
237
+ await sessionManager.initialize(credentials.mcpEndpoint, true, endpoint);
238
+ return {
239
+ content: [{ type: 'text', text: `Connected to browser at ${endpoint}.` }],
240
+ isError: false,
241
+ toolName: 'browser_connect_cdp',
242
+ requestedAction: input.action
243
+ };
244
+ }
245
+ }
246
+ // Auto-initialize if possible (blindly)
247
+ await sessionManager.initialize(credentials.mcpEndpoint, credentials.browserMode === 'cdp', credentials.cdpEndpoint);
248
+ }
249
+ // Normalize action name
250
+ let toolName = input.action;
251
+ if (!toolName.startsWith('browser_') && toolName !== 'connect_cdp') {
252
+ toolName = `browser_${toolName}`;
253
+ }
254
+ // Handle params
255
+ let toolArgs = input.params || {};
256
+ if (typeof toolArgs === 'string') {
257
+ try {
258
+ toolArgs = JSON.parse(toolArgs);
259
+ }
260
+ catch (e) {
261
+ // ignore
262
+ }
263
+ }
264
+ console.log(`[Router] Routing '${input.action}' to '${toolName}' with args:`, toolArgs);
265
+ try {
266
+ const result = await sessionManager.callTool(toolName, toolArgs);
267
+ // Add tool name to result for visibility
268
+ if (result && typeof result === 'object') {
269
+ result.toolName = toolName;
270
+ result.requestedAction = input.action;
271
+ }
272
+ return result;
273
+ }
274
+ catch (error) {
275
+ return {
276
+ content: [{ type: 'text', text: `Error executing ${toolName}: ${error.message}` }],
277
+ isError: true,
278
+ toolName: toolName,
279
+ requestedAction: input.action
280
+ };
281
+ }
282
+ }
283
+ };
284
+ // Add a custom tool for specific CDP connection (legacy support)
219
285
  const connectTool = {
220
286
  name: 'browser_connect_cdp',
221
287
  displayName: 'Connect to Browser (CDP)',
222
- description: 'Connect to a specific browser instance using a CDP URL (wss://...)',
288
+ description: 'Connect to a specific browser instance using a CDP URL. Params: { "endpoint": "wss://..." }',
223
289
  properties: [
224
290
  {
225
291
  displayName: 'Endpoint URL',
@@ -227,7 +293,7 @@ class SmartBrowserAutomation {
227
293
  type: 'string',
228
294
  default: '',
229
295
  required: true,
230
- description: 'The wss:// or http:// endpoint for the browser CDP connection',
296
+ description: 'The wss:// or http:// endpoint',
231
297
  },
232
298
  ],
233
299
  async execute(input) {
@@ -235,12 +301,13 @@ class SmartBrowserAutomation {
235
301
  const credentials = await this.getCredentials('smartBrowserAutomationApi');
236
302
  await sessionManager.initialize(credentials.mcpEndpoint, true, input.endpoint);
237
303
  return {
238
- content: [{ type: 'text', text: `Connected to browser at ${input.endpoint}. You can now use tools like browser_navigate, browser_click, browser_type, etc.` }],
239
- isError: false
304
+ content: [{ type: 'text', text: `Connected to browser at ${input.endpoint}.` }],
305
+ isError: false,
306
+ toolName: 'browser_connect_cdp'
240
307
  };
241
308
  }
242
309
  };
243
- return [connectTool, ...tools];
310
+ return [routerTool, connectTool];
244
311
  }
245
312
  async execute() {
246
313
  const items = this.getInputData();
@@ -301,41 +368,40 @@ class SmartBrowserAutomation {
301
368
  const operation = this.getNodeParameter('operation', i);
302
369
  if (operation === 'initialize') {
303
370
  const cdpUrl = credentials.cdpEndpoint;
304
- const tools = await sessionManager.initialize(credentials.mcpEndpoint, true, cdpUrl);
305
- let connectionStatus = 'Skipped (No CDP URL provided)';
306
- let connectionResult = null;
371
+ await sessionManager.initialize(credentials.mcpEndpoint, true, cdpUrl);
307
372
  // New logic: Auto-call browser_connect_cdp tool if a URL is provided
308
373
  if (options.cdpUrl) {
309
374
  if (verbose) {
310
375
  console.log(`Auto-calling 'browser_connect_cdp' with endpoint: ${options.cdpUrl}`);
311
376
  }
312
377
  try {
313
- connectionResult = await sessionManager.callTool('browser_connect_cdp', { endpoint: options.cdpUrl });
314
- connectionStatus = 'Successfully connected';
378
+ const connectionResult = await sessionManager.callTool('browser_connect_cdp', { endpoint: options.cdpUrl });
379
+ // Return only the connection result, no extra messages
380
+ returnData.push({
381
+ json: connectionResult,
382
+ pairedItem: i,
383
+ });
315
384
  }
316
385
  catch (error) {
317
- connectionStatus = `Failed to connect: ${error.message}`;
318
- console.warn(`Failed to auto-connect browser via tool: ${error.message}`);
386
+ returnData.push({
387
+ json: {
388
+ success: false,
389
+ error: `Failed to connect: ${error.message}`
390
+ },
391
+ pairedItem: i,
392
+ });
319
393
  }
320
394
  }
321
- // Create a compact tool guide for the AI to fix hallucinations
322
- // We prioritise showing the most important tools first
323
- const coreTools = ['browser_navigate', 'browser_click', 'browser_type', 'browser_press_key', 'browser_scroll_to', 'browser_get_text', 'browser_take_screenshot', 'browser_evaluate'];
324
- const toolGuide = tools
325
- .filter(t => coreTools.includes(t.name))
326
- .map(t => `- ${t.name}: ${(t.description || '').substring(0, 100)}`)
327
- .join('\n');
328
- const otherToolsCount = tools.length - coreTools.length;
329
- returnData.push({
330
- json: {
331
- success: true,
332
- message: `MCP session initialized at ${credentials.mcpEndpoint}`,
333
- browserConnection: connectionStatus,
334
- browserResponse: connectionResult,
335
- toolGuide: `Available Tools (Top ${coreTools.length} of ${tools.length}):\n${toolGuide}\n...and ${otherToolsCount} more. Use these exact names!`,
336
- },
337
- pairedItem: i,
338
- });
395
+ else {
396
+ // No CDP URL provided
397
+ returnData.push({
398
+ json: {
399
+ success: true,
400
+ message: 'MCP session initialized (no browser connection)'
401
+ },
402
+ pairedItem: i,
403
+ });
404
+ }
339
405
  }
340
406
  else if (operation === 'close') {
341
407
  await sessionManager.close();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-smart-browser-automation",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "n8n node for AI-driven browser automation using MCP",
5
5
  "keywords": [
6
6
  "n8n-community-node-package",