mirror of
https://github.com/n8n-io/n8n
synced 2026-04-21 15:47:20 +00:00
Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> Co-authored-by: Albert Alises <albert.alises@gmail.com> Co-authored-by: Jaakko Husso <jaakko@n8n.io> Co-authored-by: Dimitri Lavrenük <20122620+dlavrenuek@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> Co-authored-by: Tuukka Kantola <Tuukkaa@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com> Co-authored-by: Raúl Gómez Morales <raul00gm@gmail.com> Co-authored-by: Elias Meire <elias@meire.dev> Co-authored-by: Dimitri Lavrenük <dimitri.lavrenuek@n8n.io> Co-authored-by: Tomi Turtiainen <10324676+tomi@users.noreply.github.com> Co-authored-by: Mutasem Aldmour <mutasem@n8n.io>
61 lines
1.9 KiB
TypeScript
61 lines
1.9 KiB
TypeScript
import type { Config as BrowserConfig } from '@n8n/mcp-browser';
|
|
|
|
import { logger, type LogLevel } from '../../logger';
|
|
import type { ToolDefinition, ToolModule } from '../types';
|
|
|
|
export interface BrowserModuleConfig {
|
|
defaultBrowser?: string;
|
|
logLevel?: LogLevel;
|
|
}
|
|
|
|
function toBrowserConfig(config: BrowserModuleConfig): Partial<BrowserConfig> {
|
|
const browserConfig: Partial<BrowserConfig> = {};
|
|
if (config.defaultBrowser) {
|
|
browserConfig.defaultBrowser = config.defaultBrowser as BrowserConfig['defaultBrowser'];
|
|
}
|
|
return browserConfig;
|
|
}
|
|
|
|
/**
|
|
* ToolModule that exposes @n8n/mcp-browser tools through the gateway.
|
|
*
|
|
* Use `BrowserModule.create()` to construct — it dynamically imports
|
|
* `@n8n/mcp-browser` and initialises the BrowserConnection and tools.
|
|
*/
|
|
export class BrowserModule implements ToolModule {
|
|
private connection: { shutdown(): Promise<void> };
|
|
|
|
definitions: ToolDefinition[];
|
|
|
|
private constructor(definitions: ToolDefinition[], connection: { shutdown(): Promise<void> }) {
|
|
this.definitions = definitions;
|
|
this.connection = connection;
|
|
}
|
|
|
|
/**
|
|
* Create a BrowserModule if `@n8n/mcp-browser` is available.
|
|
* Returns `null` when the package cannot be imported.
|
|
*/
|
|
static async create(config: BrowserModuleConfig = {}): Promise<BrowserModule | null> {
|
|
try {
|
|
const { createBrowserTools, configureLogger } = await import('@n8n/mcp-browser');
|
|
if (config.logLevel) {
|
|
configureLogger({ level: config.logLevel });
|
|
}
|
|
const { tools, connection } = createBrowserTools(toBrowserConfig(config));
|
|
return new BrowserModule(tools, connection);
|
|
} catch {
|
|
logger.info('Browser module not supported', { reason: '@n8n/mcp-browser not available' });
|
|
return null;
|
|
}
|
|
}
|
|
|
|
isSupported() {
|
|
return true;
|
|
}
|
|
|
|
/** Shut down the BrowserConnection and close the browser. */
|
|
async shutdown(): Promise<void> {
|
|
await this.connection.shutdown();
|
|
}
|
|
}
|