n8n-nodes-smart-browser-automation 1.1.10 → 1.1.12

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.
@@ -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,90 @@ 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
+ };
242
+ }
243
+ }
244
+ // Auto-initialize if possible (blindly)
245
+ await sessionManager.initialize(credentials.mcpEndpoint, credentials.browserMode === 'cdp', credentials.cdpEndpoint);
246
+ }
247
+ // Normalize action name
248
+ let toolName = input.action;
249
+ if (!toolName.startsWith('browser_') && toolName !== 'connect_cdp') {
250
+ toolName = `browser_${toolName}`;
251
+ }
252
+ // Handle params
253
+ let toolArgs = input.params || {};
254
+ if (typeof toolArgs === 'string') {
255
+ try {
256
+ toolArgs = JSON.parse(toolArgs);
257
+ }
258
+ catch (e) {
259
+ // ignore
260
+ }
261
+ }
262
+ console.log(`[Router] Routing '${input.action}' to '${toolName}' with args:`, toolArgs);
263
+ try {
264
+ const result = await sessionManager.callTool(toolName, toolArgs);
265
+ return result;
266
+ }
267
+ catch (error) {
268
+ return {
269
+ content: [{ type: 'text', text: `Error executing ${toolName}: ${error.message}` }],
270
+ isError: true
271
+ };
272
+ }
273
+ }
274
+ };
275
+ // Add a custom tool for specific CDP connection (legacy support)
219
276
  const connectTool = {
220
277
  name: 'browser_connect_cdp',
221
278
  displayName: 'Connect to Browser (CDP)',
222
- description: 'Connect to a specific browser instance using a CDP URL (wss://...)',
279
+ description: 'Connect to a specific browser instance using a CDP URL. Params: { "endpoint": "wss://..." }',
223
280
  properties: [
224
281
  {
225
282
  displayName: 'Endpoint URL',
@@ -227,7 +284,7 @@ class SmartBrowserAutomation {
227
284
  type: 'string',
228
285
  default: '',
229
286
  required: true,
230
- description: 'The wss:// or http:// endpoint for the browser CDP connection',
287
+ description: 'The wss:// or http:// endpoint',
231
288
  },
232
289
  ],
233
290
  async execute(input) {
@@ -235,12 +292,12 @@ class SmartBrowserAutomation {
235
292
  const credentials = await this.getCredentials('smartBrowserAutomationApi');
236
293
  await sessionManager.initialize(credentials.mcpEndpoint, true, input.endpoint);
237
294
  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.` }],
295
+ content: [{ type: 'text', text: `Connected to browser at ${input.endpoint}.` }],
239
296
  isError: false
240
297
  };
241
298
  }
242
299
  };
243
- return [connectTool, ...tools];
300
+ return [routerTool, connectTool];
244
301
  }
245
302
  async execute() {
246
303
  const items = this.getInputData();
@@ -301,7 +358,7 @@ class SmartBrowserAutomation {
301
358
  const operation = this.getNodeParameter('operation', i);
302
359
  if (operation === 'initialize') {
303
360
  const cdpUrl = credentials.cdpEndpoint;
304
- const tools = await sessionManager.initialize(credentials.mcpEndpoint, true, cdpUrl);
361
+ await sessionManager.initialize(credentials.mcpEndpoint, true, cdpUrl);
305
362
  let connectionStatus = 'Skipped (No CDP URL provided)';
306
363
  let connectionResult = null;
307
364
  // New logic: Auto-call browser_connect_cdp tool if a URL is provided
@@ -318,13 +375,26 @@ class SmartBrowserAutomation {
318
375
  console.warn(`Failed to auto-connect browser via tool: ${error.message}`);
319
376
  }
320
377
  }
378
+ // Guide for the Router Tool
379
+ const actionGuide = `
380
+ Actions available via 'browser_tool':
381
+ - navigate: { "url": "https://..." }
382
+ - click: { "selector": "..." }
383
+ - type: { "selector": "...", "text": "..." }
384
+ - press_key: { "key": "Enter" }
385
+ - scroll_to: { "selector": "..." }
386
+ - get_text: { "selector": "..." }
387
+ - take_screenshot: {}
388
+ - evaluate: { "script": "return document.title;" }
389
+ ...and all other MCP tools.
390
+ `;
321
391
  returnData.push({
322
392
  json: {
323
393
  success: true,
324
394
  message: `MCP session initialized at ${credentials.mcpEndpoint}`,
325
395
  browserConnection: connectionStatus,
326
396
  browserResponse: connectionResult,
327
- totalToolsAvailable: tools.length,
397
+ toolGuide: actionGuide,
328
398
  },
329
399
  pairedItem: i,
330
400
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-smart-browser-automation",
3
- "version": "1.1.10",
3
+ "version": "1.1.12",
4
4
  "description": "n8n node for AI-driven browser automation using MCP",
5
5
  "keywords": [
6
6
  "n8n-community-node-package",