real-browser-mcp-server 1.2.0 → 1.2.2
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.
- package/README.md +96 -9
- package/dist/lib/cjs/index.d.ts +2 -0
- package/dist/lib/cjs/index.d.ts.map +1 -0
- package/dist/lib/cjs/index.js +385 -0
- package/dist/lib/cjs/index.js.map +1 -0
- package/dist/lib/cjs/module/pageController.d.ts +2 -0
- package/dist/lib/cjs/module/pageController.d.ts.map +1 -0
- package/{lib → dist/lib}/cjs/module/pageController.js +28 -29
- package/dist/lib/cjs/module/pageController.js.map +1 -0
- package/dist/lib/cjs/module/turnstile.d.ts +2 -0
- package/dist/lib/cjs/module/turnstile.d.ts.map +1 -0
- package/{lib → dist/lib}/cjs/module/turnstile.js +24 -12
- package/dist/lib/cjs/module/turnstile.js.map +1 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +118 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/mcp/handlers/browser.d.ts +30 -0
- package/dist/src/mcp/handlers/browser.d.ts.map +1 -0
- package/dist/src/mcp/handlers/browser.js +231 -0
- package/dist/src/mcp/handlers/browser.js.map +1 -0
- package/dist/src/mcp/handlers/dom.d.ts +134 -0
- package/dist/src/mcp/handlers/dom.d.ts.map +1 -0
- package/dist/src/mcp/handlers/dom.js +551 -0
- package/dist/src/mcp/handlers/dom.js.map +1 -0
- package/dist/src/mcp/handlers/extract.d.ts +59 -0
- package/dist/src/mcp/handlers/extract.d.ts.map +1 -0
- package/dist/src/mcp/handlers/extract.js +455 -0
- package/dist/src/mcp/handlers/extract.js.map +1 -0
- package/dist/src/mcp/handlers/form-handlers.d.ts +9 -0
- package/dist/src/mcp/handlers/form-handlers.d.ts.map +1 -0
- package/dist/src/mcp/handlers/form-handlers.js +56 -0
- package/dist/src/mcp/handlers/form-handlers.js.map +1 -0
- package/dist/src/mcp/handlers/helpers.d.ts +47 -0
- package/dist/src/mcp/handlers/helpers.d.ts.map +1 -0
- package/dist/src/mcp/handlers/helpers.js +515 -0
- package/dist/src/mcp/handlers/helpers.js.map +1 -0
- package/dist/src/mcp/handlers/index.d.ts +6 -0
- package/dist/src/mcp/handlers/index.d.ts.map +1 -0
- package/dist/src/mcp/handlers/index.js +61 -0
- package/dist/src/mcp/handlers/index.js.map +1 -0
- package/dist/src/mcp/handlers/media-handlers.d.ts +10 -0
- package/dist/src/mcp/handlers/media-handlers.d.ts.map +1 -0
- package/dist/src/mcp/handlers/media-handlers.js +535 -0
- package/dist/src/mcp/handlers/media-handlers.js.map +1 -0
- package/dist/src/mcp/handlers/network.d.ts +147 -0
- package/dist/src/mcp/handlers/network.d.ts.map +1 -0
- package/dist/src/mcp/handlers/network.js +1135 -0
- package/dist/src/mcp/handlers/network.js.map +1 -0
- package/dist/src/mcp/handlers/state.d.ts +34 -0
- package/dist/src/mcp/handlers/state.d.ts.map +1 -0
- package/dist/src/mcp/handlers/state.js +225 -0
- package/dist/src/mcp/handlers/state.js.map +1 -0
- package/dist/src/mcp/handlers/utility-handlers.d.ts +167 -0
- package/dist/src/mcp/handlers/utility-handlers.d.ts.map +1 -0
- package/dist/src/mcp/handlers/utility-handlers.js +280 -0
- package/dist/src/mcp/handlers/utility-handlers.js.map +1 -0
- package/dist/src/mcp/handlers/vision.d.ts +127 -0
- package/dist/src/mcp/handlers/vision.d.ts.map +1 -0
- package/dist/src/mcp/handlers/vision.js +483 -0
- package/dist/src/mcp/handlers/vision.js.map +1 -0
- package/dist/src/mcp/index.d.ts +3 -0
- package/dist/src/mcp/index.d.ts.map +1 -0
- package/dist/src/mcp/index.js +166 -0
- package/dist/src/mcp/index.js.map +1 -0
- package/dist/src/mcp/server.d.ts +2 -0
- package/dist/src/mcp/server.d.ts.map +1 -0
- package/dist/src/mcp/server.js +117 -0
- package/dist/src/mcp/server.js.map +1 -0
- package/dist/src/mcp/tools.d.ts +8 -0
- package/dist/src/mcp/tools.d.ts.map +1 -0
- package/{src → dist/src}/mcp/tools.js +12 -11
- package/dist/src/mcp/tools.js.map +1 -0
- package/dist/src/shared/cache-manager.d.ts +80 -0
- package/dist/src/shared/cache-manager.d.ts.map +1 -0
- package/dist/src/shared/cache-manager.js +221 -0
- package/dist/src/shared/cache-manager.js.map +1 -0
- package/dist/src/shared/tools.d.ts +2 -0
- package/dist/src/shared/tools.d.ts.map +1 -0
- package/dist/src/shared/tools.js +599 -0
- package/dist/src/shared/tools.js.map +1 -0
- package/dist/src/types.d.ts +365 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +9 -0
- package/dist/src/types.js.map +1 -0
- package/dist/test/cjs/test.d.ts +11 -0
- package/dist/test/cjs/test.d.ts.map +1 -0
- package/dist/test/cjs/test.js +289 -0
- package/dist/test/cjs/test.js.map +1 -0
- package/dist/test/mcp/smoke-test.d.ts +29 -0
- package/dist/test/mcp/smoke-test.d.ts.map +1 -0
- package/dist/test/mcp/smoke-test.js +132 -0
- package/dist/test/mcp/smoke-test.js.map +1 -0
- package/lib/esm/index.mjs +232 -79
- package/lib/esm/module/pageController.mjs +21 -18
- package/lib/esm/module/turnstile.mjs +7 -0
- package/package.json +25 -15
- package/typings.d.ts +12 -6
- package/.github/ISSUE_TEMPLATE/general_issue.yaml +0 -58
- package/.github/SETUP.md +0 -111
- package/.github/workflows/publish.yml +0 -135
- package/Dockerfile +0 -79
- package/lib/cjs/adblocker.bin +0 -0
- package/lib/cjs/index.js +0 -249
- package/src/ai/action-parser.js +0 -274
- package/src/ai/core.js +0 -378
- package/src/ai/element-finder.js +0 -466
- package/src/ai/index.js +0 -82
- package/src/ai/page-analyzer.js +0 -304
- package/src/ai/selector-healer.js +0 -236
- package/src/index.js +0 -121
- package/src/mcp/handlers.js +0 -5071
- package/src/mcp/index.js +0 -190
- package/src/mcp/server.js +0 -144
- package/src/shared/tools.js +0 -618
- package/test/cjs/test.js +0 -259
- package/test/esm/package.json +0 -13
- package/test/esm/test.js +0 -226
- package/test/esm/test_option2.js +0 -46
- package/test/esm/test_playwright_ghost.js +0 -30
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
// CRITICAL: Redirect ALL console.log to STDERR BEFORE any imports.
|
|
5
|
+
// MCP uses STDIO transport — STDOUT must contain ONLY JSON-RPC messages.
|
|
6
|
+
// Any console.log from ANY dependency will corrupt the JSON-RPC stream.
|
|
7
|
+
console.log = function (...args) { console.error(...args); };
|
|
8
|
+
/**
|
|
9
|
+
* Brave Real Browser MCP Server - Entry Point
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* npm run dev - Start MCP server
|
|
13
|
+
* npm run mcp - Start MCP server (alias)
|
|
14
|
+
*
|
|
15
|
+
* For AI Assistants:
|
|
16
|
+
* Claude Desktop, Cursor, Copilot, etc.
|
|
17
|
+
*/
|
|
18
|
+
const { TOOL_DISPLAY } = require('./tools');
|
|
19
|
+
const { startServer, shutdownServer } = require('./server');
|
|
20
|
+
const { cleanup } = require('./handlers');
|
|
21
|
+
// ANSI colors for terminal
|
|
22
|
+
const colors = {
|
|
23
|
+
reset: '\x1b[0m',
|
|
24
|
+
bright: '\x1b[1m',
|
|
25
|
+
dim: '\x1b[2m',
|
|
26
|
+
green: '\x1b[32m',
|
|
27
|
+
yellow: '\x1b[33m',
|
|
28
|
+
blue: '\x1b[34m',
|
|
29
|
+
magenta: '\x1b[35m',
|
|
30
|
+
cyan: '\x1b[36m',
|
|
31
|
+
red: '\x1b[31m',
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Display startup banner and tools
|
|
35
|
+
*/
|
|
36
|
+
function displayStartupBanner() {
|
|
37
|
+
console.error('');
|
|
38
|
+
console.error(`${colors.bright}${colors.cyan}╔════════════════════════════════════════════════════════════╗${colors.reset}`);
|
|
39
|
+
console.error(`${colors.bright}${colors.cyan}║${colors.reset} ${colors.bright}${colors.magenta}🦁 Real Browser MCP Server${colors.reset} ${colors.cyan}║${colors.reset}`);
|
|
40
|
+
console.error(`${colors.bright}${colors.cyan}║${colors.reset} ${colors.dim}Playwright + Patchright + Stealth + Turnstile${colors.reset} ${colors.cyan}║${colors.reset}`);
|
|
41
|
+
console.error(`${colors.bright}${colors.cyan}╚════════════════════════════════════════════════════════════╝${colors.reset}`);
|
|
42
|
+
console.error('');
|
|
43
|
+
console.error(`${colors.bright}${colors.green}📦 Available Tools (${TOOL_DISPLAY.length}):${colors.reset}`);
|
|
44
|
+
console.error(`${colors.dim}${'─'.repeat(60)}${colors.reset}`);
|
|
45
|
+
// Display tools in 2 columns
|
|
46
|
+
const half = Math.ceil(TOOL_DISPLAY.length / 2);
|
|
47
|
+
for (let i = 0; i < half; i++) {
|
|
48
|
+
const left = TOOL_DISPLAY[i];
|
|
49
|
+
const right = TOOL_DISPLAY[i + half];
|
|
50
|
+
const leftStr = left
|
|
51
|
+
? `${left.emoji} ${colors.yellow}${left.name.padEnd(22)}${colors.reset}`
|
|
52
|
+
: '';
|
|
53
|
+
const rightStr = right
|
|
54
|
+
? `${right.emoji} ${colors.yellow}${right.name}${colors.reset}`
|
|
55
|
+
: '';
|
|
56
|
+
console.error(` ${leftStr}${rightStr}`);
|
|
57
|
+
}
|
|
58
|
+
console.error(`${colors.dim}${'─'.repeat(60)}${colors.reset}`);
|
|
59
|
+
console.error('');
|
|
60
|
+
// Fetch SDK version dynamically
|
|
61
|
+
let sdkVersion = 'Latest';
|
|
62
|
+
try {
|
|
63
|
+
const pkgPath = require('path').join(__dirname, '..', '..', 'package.json');
|
|
64
|
+
const pkg = require('fs').readFileSync(pkgPath, 'utf8');
|
|
65
|
+
const deps = JSON.parse(pkg).dependencies || {};
|
|
66
|
+
if (deps['@modelcontextprotocol/sdk']) {
|
|
67
|
+
sdkVersion = deps['@modelcontextprotocol/sdk'].replace(/[\^~]/g, '');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (e) { /* ignore */ }
|
|
71
|
+
console.error(`${colors.bright}${colors.blue}🔗 Transport:${colors.reset} STDIO (for MCP clients)`);
|
|
72
|
+
console.error(`${colors.bright}${colors.blue}📡 Protocol:${colors.reset} Model Context Protocol (SDK v${sdkVersion})`);
|
|
73
|
+
console.error('');
|
|
74
|
+
console.error(`${colors.dim}Press CTRL+C to stop the server${colors.reset}`);
|
|
75
|
+
console.error('');
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Display tool details (verbose mode)
|
|
79
|
+
*/
|
|
80
|
+
function displayToolDetails() {
|
|
81
|
+
console.error(`${colors.bright}${colors.green}📋 Tool Details:${colors.reset}`);
|
|
82
|
+
console.error('');
|
|
83
|
+
for (const tool of TOOL_DISPLAY) {
|
|
84
|
+
console.error(` ${tool.emoji} ${colors.bright}${colors.yellow}${tool.name}${colors.reset}`);
|
|
85
|
+
console.error(` ${colors.dim}${tool.description}${colors.reset}`);
|
|
86
|
+
if (tool.descriptionHindi) {
|
|
87
|
+
console.error(` ${colors.dim}(${tool.descriptionHindi})${colors.reset}`);
|
|
88
|
+
}
|
|
89
|
+
console.error('');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Setup graceful shutdown handlers
|
|
94
|
+
*/
|
|
95
|
+
function setupShutdownHandlers(server) {
|
|
96
|
+
let isShuttingDown = false;
|
|
97
|
+
const gracefulShutdown = async (signal) => {
|
|
98
|
+
if (isShuttingDown)
|
|
99
|
+
return;
|
|
100
|
+
isShuttingDown = true;
|
|
101
|
+
console.error('');
|
|
102
|
+
console.error(`${colors.bright}${colors.yellow}🧹 Cleanup in progress...${colors.reset}`);
|
|
103
|
+
try {
|
|
104
|
+
await cleanup();
|
|
105
|
+
console.error(`${colors.bright}${colors.green}✅ Browser closed${colors.reset}`);
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
console.error(`${colors.dim}Browser already closed${colors.reset}`);
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
await shutdownServer(server);
|
|
112
|
+
console.error(`${colors.bright}${colors.green}✅ MCP Server stopped${colors.reset}`);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
// Server may already be closed
|
|
116
|
+
}
|
|
117
|
+
console.error(`${colors.bright}${colors.magenta}👋 Goodbye!${colors.reset}`);
|
|
118
|
+
console.error('');
|
|
119
|
+
process.exit(0);
|
|
120
|
+
};
|
|
121
|
+
// Handle various termination signals
|
|
122
|
+
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
123
|
+
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
124
|
+
process.on('SIGHUP', () => gracefulShutdown('SIGHUP'));
|
|
125
|
+
// Handle uncaught errors
|
|
126
|
+
process.on('uncaughtException', async (error) => {
|
|
127
|
+
console.error(`${colors.bright}${colors.red}❌ Uncaught Exception:${colors.reset}`, error.message);
|
|
128
|
+
await gracefulShutdown('error');
|
|
129
|
+
});
|
|
130
|
+
process.on('unhandledRejection', async (reason) => {
|
|
131
|
+
console.error(`${colors.bright}${colors.red}❌ Unhandled Rejection:${colors.reset}`, reason);
|
|
132
|
+
await gracefulShutdown('error');
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Main entry point
|
|
137
|
+
*/
|
|
138
|
+
async function main() {
|
|
139
|
+
// Check for verbose flag
|
|
140
|
+
const verbose = process.argv.includes('--verbose') || process.argv.includes('-v');
|
|
141
|
+
// Display startup info
|
|
142
|
+
displayStartupBanner();
|
|
143
|
+
if (verbose) {
|
|
144
|
+
displayToolDetails();
|
|
145
|
+
}
|
|
146
|
+
try {
|
|
147
|
+
// Start MCP server
|
|
148
|
+
console.error(`${colors.bright}${colors.green}🚀 Starting MCP Server...${colors.reset}`);
|
|
149
|
+
const { server } = await startServer();
|
|
150
|
+
console.error(`${colors.bright}${colors.green}✅ MCP Server running${colors.reset}`);
|
|
151
|
+
console.error(`${colors.dim}Waiting for client connections via STDIO...${colors.reset}`);
|
|
152
|
+
console.error('');
|
|
153
|
+
// Setup shutdown handlers
|
|
154
|
+
setupShutdownHandlers(server);
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
console.error(`${colors.bright}${colors.red}❌ Failed to start server:${colors.reset}`, error.message);
|
|
158
|
+
process.exit(1);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// Run main
|
|
162
|
+
main().catch((error) => {
|
|
163
|
+
console.error(`${colors.bright}${colors.red}❌ Fatal error:${colors.reset}`, error.message);
|
|
164
|
+
process.exit(1);
|
|
165
|
+
});
|
|
166
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/mcp/index.ts"],"names":[],"mappings":";;;AACA,mEAAmE;AACnE,yEAAyE;AACzE,wEAAwE;AACxE,OAAO,CAAC,GAAG,GAAG,UAAU,GAAG,IAAI,IAAI,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AAE7D;;;;;;;;;GASG;AAEH,MAAM,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AAC5C,MAAM,EAAE,WAAW,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAC5D,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;AAE1C,2BAA2B;AAC3B,MAAM,MAAM,GAAG;IACb,KAAK,EAAE,SAAS;IAChB,MAAM,EAAE,SAAS;IACjB,GAAG,EAAE,SAAS;IACd,KAAK,EAAE,UAAU;IACjB,MAAM,EAAE,UAAU;IAClB,IAAI,EAAE,UAAU;IAChB,OAAO,EAAE,UAAU;IACnB,IAAI,EAAE,UAAU;IAChB,GAAG,EAAE,UAAU;CAChB,CAAC;AAEF;;GAEG;AACH,SAAS,oBAAoB;IAC3B,OA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,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../src/mcp/server.ts"],"names":[],"mappings":"AA8IA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const _originalConsoleLog = console.log;
|
|
4
|
+
console.log = function (...args) {
|
|
5
|
+
console.error(...args);
|
|
6
|
+
};
|
|
7
|
+
const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
|
|
8
|
+
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
|
|
9
|
+
const { CallToolRequestSchema, ListToolsRequestSchema, ErrorCode, McpError, } = require('@modelcontextprotocol/sdk/types.js');
|
|
10
|
+
const { TOOLS } = require('./tools');
|
|
11
|
+
const { executeTool, cleanup } = require('./handlers');
|
|
12
|
+
// Single source of truth: read version from package.json (avoids version drift)
|
|
13
|
+
let PKG_VERSION = '0.0.0';
|
|
14
|
+
try {
|
|
15
|
+
PKG_VERSION = require('../../package.json').version || PKG_VERSION;
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
console.error('⚠️ Could not read version from package.json:', e.message);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create and configure MCP Server
|
|
22
|
+
*/
|
|
23
|
+
function createServer() {
|
|
24
|
+
const server = new Server({
|
|
25
|
+
name: 'real-browser-mcp-server',
|
|
26
|
+
version: PKG_VERSION,
|
|
27
|
+
}, {
|
|
28
|
+
capabilities: {
|
|
29
|
+
tools: {},
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
// Handle list tools request
|
|
33
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
34
|
+
return {
|
|
35
|
+
tools: TOOLS.map((tool) => ({
|
|
36
|
+
name: tool.name,
|
|
37
|
+
description: `${tool.emoji} ${tool.description}`,
|
|
38
|
+
inputSchema: tool.inputSchema,
|
|
39
|
+
})),
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
// Handle call tool request
|
|
43
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
44
|
+
const { name, arguments: args } = request.params;
|
|
45
|
+
// Find tool definition
|
|
46
|
+
const tool = TOOLS.find((t) => t.name === name);
|
|
47
|
+
if (!tool) {
|
|
48
|
+
throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`);
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
// Execute the tool
|
|
52
|
+
const result = await executeTool(name, args || {});
|
|
53
|
+
// Format response
|
|
54
|
+
if (result.success === false && result.error) {
|
|
55
|
+
return {
|
|
56
|
+
content: [
|
|
57
|
+
{
|
|
58
|
+
type: 'text',
|
|
59
|
+
text: JSON.stringify({ error: result.error }, null, 2),
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
isError: true,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (result.mcpContent) {
|
|
66
|
+
return { content: result.mcpContent };
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
content: [
|
|
70
|
+
{
|
|
71
|
+
type: 'text',
|
|
72
|
+
text: JSON.stringify(result, null, 2),
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
return {
|
|
79
|
+
content: [
|
|
80
|
+
{
|
|
81
|
+
type: 'text',
|
|
82
|
+
text: JSON.stringify({ error: error.message }, null, 2),
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
isError: true,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return server;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Start MCP Server with STDIO transport
|
|
93
|
+
*/
|
|
94
|
+
async function startServer() {
|
|
95
|
+
const server = createServer();
|
|
96
|
+
const transport = new StdioServerTransport();
|
|
97
|
+
// Connect server to transport
|
|
98
|
+
await server.connect(transport);
|
|
99
|
+
return { server, transport };
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Graceful shutdown
|
|
103
|
+
*/
|
|
104
|
+
async function shutdownServer(server) {
|
|
105
|
+
// Cleanup browser resources
|
|
106
|
+
await cleanup();
|
|
107
|
+
// Close server
|
|
108
|
+
if (server) {
|
|
109
|
+
await server.close();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
module.exports = {
|
|
113
|
+
createServer,
|
|
114
|
+
startServer,
|
|
115
|
+
shutdownServer,
|
|
116
|
+
};
|
|
117
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/mcp/server.ts"],"names":[],"mappings":";;AAAA,MAAM,mBAAmB,GAAG,OAAO,CAAC,GAAG,CAAC;AACxC,OAAO,CAAC,GAAG,GAAG,UAAU,GAAG,IAAI;IAC7B,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;AACzB,CAAC,CAAC;AAEF,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,2CAA2C,CAAC,CAAC;AACxE,MAAM,EAAE,oBAAoB,EAAE,GAAG,OAAO,CAAC,2CAA2C,CAAC,CAAC;AACtF,MAAM,EACJ,qBAAqB,EACrB,sBAAsB,EACtB,SAAS,EACT,QAAQ,GACT,GAAG,OAAO,CAAC,oCAAoC,CAAC,CAAC;AAElD,MAAM,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AACrC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;AAEvD,gFAAgF;AAChF,IAAI,WAAW,GAAG,OAAO,CAAC;AAC1B,IAAI,CAAC;IACH,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC,OAAO,I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E,CAAC;AAC/B,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,cAAc,CAAC,MAAW;IACvC,4BAA4B;IAC5B,MAAM,OAAO,EAAE,CAAC;IAEhB,eAAe;IACf,IAAI,MAAM,EAAE,CAAC;QACX,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;AACH,CAAC;AAED,MAAM,CAAC,OAAO,GAAG;IACf,YAAY;IACZ,WAAW;IACX,cAAc;CACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../../src/mcp/tools.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAcH,OAAO,EAAE,CAAA"}
|
|
@@ -1,18 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* Brave Real Browser MCP Server - Tool Definitions
|
|
3
|
-
*
|
|
4
|
+
*
|
|
4
5
|
* Re-exports from shared tools for backward compatibility
|
|
5
6
|
* All tool definitions are now in src/shared/tools.js
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
9
|
const { TOOLS, TOOL_DISPLAY, CATEGORIES, getToolByName, getToolsByCategory, getToolNames, getRequiredParams } = require('../shared/tools');
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
getRequiredParams
|
|
10
|
+
module.exports = {
|
|
11
|
+
TOOLS,
|
|
12
|
+
TOOL_DISPLAY,
|
|
13
|
+
CATEGORIES,
|
|
14
|
+
getToolByName,
|
|
15
|
+
getToolsByCategory,
|
|
16
|
+
getToolNames,
|
|
17
|
+
getRequiredParams
|
|
18
18
|
};
|
|
19
|
+
//# sourceMappingURL=tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../../../src/mcp/tools.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAEH,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,kBAAkB,EAAE,YAAY,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAE3I,MAAM,CAAC,OAAO,GAAG;IACf,KAAK;IACL,YAAY;IACZ,UAAU;IACV,aAAa;IACb,kBAAkB;IAClB,YAAY;IACZ,iBAAiB;CAClB,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache Manager — JSON-based persistent storage
|
|
3
|
+
*
|
|
4
|
+
* Provides a centralized cache that persists to disk as JSON.
|
|
5
|
+
* Survives server restarts. Used by AI Core and browser state.
|
|
6
|
+
*/
|
|
7
|
+
export interface CacheEntry {
|
|
8
|
+
value: unknown;
|
|
9
|
+
createdAt: number;
|
|
10
|
+
ttl: number | null;
|
|
11
|
+
}
|
|
12
|
+
export interface CacheConfig {
|
|
13
|
+
/** Directory for cache files */
|
|
14
|
+
cacheDir: string;
|
|
15
|
+
/** Auto-save interval in ms (0 = disabled) */
|
|
16
|
+
autoSaveInterval: number;
|
|
17
|
+
/** Max number of entries before pruning */
|
|
18
|
+
maxEntries: number;
|
|
19
|
+
/** Default TTL in ms (null = never expires) */
|
|
20
|
+
defaultTTL: number | null;
|
|
21
|
+
}
|
|
22
|
+
export declare class CacheManager {
|
|
23
|
+
private cache;
|
|
24
|
+
private cacheFile;
|
|
25
|
+
private config;
|
|
26
|
+
private saveTimer;
|
|
27
|
+
private dirty;
|
|
28
|
+
constructor(config?: Partial<CacheConfig>);
|
|
29
|
+
/**
|
|
30
|
+
* Get a value from cache. Returns undefined if not found or expired.
|
|
31
|
+
*/
|
|
32
|
+
get<T = unknown>(key: string): T | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Set a value in cache with optional TTL.
|
|
35
|
+
*/
|
|
36
|
+
set(key: string, value: unknown, ttl?: number | null): void;
|
|
37
|
+
/**
|
|
38
|
+
* Check if a key exists and is not expired.
|
|
39
|
+
*/
|
|
40
|
+
has(key: string): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Delete a specific key from cache.
|
|
43
|
+
*/
|
|
44
|
+
delete(key: string): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Clear all cache entries.
|
|
47
|
+
*/
|
|
48
|
+
clear(): void;
|
|
49
|
+
/**
|
|
50
|
+
* Get all keys in cache.
|
|
51
|
+
*/
|
|
52
|
+
keys(): string[];
|
|
53
|
+
/**
|
|
54
|
+
* Get cache statistics.
|
|
55
|
+
*/
|
|
56
|
+
stats(): {
|
|
57
|
+
size: number;
|
|
58
|
+
maxEntries: number;
|
|
59
|
+
cacheFile: string;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Load cache from JSON file on disk.
|
|
63
|
+
*/
|
|
64
|
+
loadFromDisk(): void;
|
|
65
|
+
/**
|
|
66
|
+
* Save cache to JSON file on disk.
|
|
67
|
+
*/
|
|
68
|
+
saveToDisk(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Remove expired entries and oldest entries if over capacity.
|
|
71
|
+
*/
|
|
72
|
+
private prune;
|
|
73
|
+
/**
|
|
74
|
+
* Cleanup: stop timer and save final state.
|
|
75
|
+
*/
|
|
76
|
+
destroy(): void;
|
|
77
|
+
}
|
|
78
|
+
/** Singleton instance for project-wide use */
|
|
79
|
+
export declare const cacheManager: CacheManager;
|
|
80
|
+
//# sourceMappingURL=cache-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-manager.d.ts","sourceRoot":"","sources":["../../../src/shared/cache-manager.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,OAAO,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;CACpB;AAED,MAAM,WAAW,W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
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Cache Manager — JSON-based persistent storage
|
|
4
|
+
*
|
|
5
|
+
* Provides a centralized cache that persists to disk as JSON.
|
|
6
|
+
* Survives server restarts. Used by AI Core and browser state.
|
|
7
|
+
*/
|
|
8
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
+
if (k2 === undefined) k2 = k;
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
15
|
+
}) : (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
o[k2] = m[k];
|
|
18
|
+
}));
|
|
19
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
20
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
21
|
+
}) : function(o, v) {
|
|
22
|
+
o["default"] = v;
|
|
23
|
+
});
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.cacheManager = exports.CacheManager = void 0;
|
|
43
|
+
const fs = __importStar(require("fs"));
|
|
44
|
+
const path = __importStar(require("path"));
|
|
45
|
+
const DEFAULT_CONFIG = {
|
|
46
|
+
cacheDir: path.join(process.cwd(), '.cache'),
|
|
47
|
+
autoSaveInterval: 30000, // 30 seconds
|
|
48
|
+
maxEntries: 1000,
|
|
49
|
+
defaultTTL: null,
|
|
50
|
+
};
|
|
51
|
+
class CacheManager {
|
|
52
|
+
cache;
|
|
53
|
+
cacheFile;
|
|
54
|
+
config;
|
|
55
|
+
saveTimer = null;
|
|
56
|
+
dirty = false;
|
|
57
|
+
constructor(config = {}) {
|
|
58
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
59
|
+
this.cacheFile = path.join(this.config.cacheDir, 'browser_state.json');
|
|
60
|
+
this.cache = new Map();
|
|
61
|
+
this.loadFromDisk();
|
|
62
|
+
// Auto-save periodically
|
|
63
|
+
if (this.config.autoSaveInterval > 0) {
|
|
64
|
+
this.saveTimer = setInterval(() => {
|
|
65
|
+
if (this.dirty) {
|
|
66
|
+
this.saveToDisk();
|
|
67
|
+
}
|
|
68
|
+
}, this.config.autoSaveInterval);
|
|
69
|
+
// Prevent timer from keeping Node alive
|
|
70
|
+
if (this.saveTimer && typeof this.saveTimer.unref === 'function') {
|
|
71
|
+
this.saveTimer.unref();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Get a value from cache. Returns undefined if not found or expired.
|
|
77
|
+
*/
|
|
78
|
+
get(key) {
|
|
79
|
+
const entry = this.cache.get(key);
|
|
80
|
+
if (!entry)
|
|
81
|
+
return undefined;
|
|
82
|
+
// Check TTL
|
|
83
|
+
if (entry.ttl !== null && Date.now() - entry.createdAt > entry.ttl) {
|
|
84
|
+
this.cache.delete(key);
|
|
85
|
+
this.dirty = true;
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
return entry.value;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Set a value in cache with optional TTL.
|
|
92
|
+
*/
|
|
93
|
+
set(key, value, ttl) {
|
|
94
|
+
// Prune if at capacity
|
|
95
|
+
if (this.cache.size >= this.config.maxEntries && !this.cache.has(key)) {
|
|
96
|
+
this.prune();
|
|
97
|
+
}
|
|
98
|
+
this.cache.set(key, {
|
|
99
|
+
value,
|
|
100
|
+
createdAt: Date.now(),
|
|
101
|
+
ttl: ttl !== undefined ? ttl : this.config.defaultTTL,
|
|
102
|
+
});
|
|
103
|
+
this.dirty = true;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Check if a key exists and is not expired.
|
|
107
|
+
*/
|
|
108
|
+
has(key) {
|
|
109
|
+
return this.get(key) !== undefined;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Delete a specific key from cache.
|
|
113
|
+
*/
|
|
114
|
+
delete(key) {
|
|
115
|
+
const existed = this.cache.delete(key);
|
|
116
|
+
if (existed)
|
|
117
|
+
this.dirty = true;
|
|
118
|
+
return existed;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Clear all cache entries.
|
|
122
|
+
*/
|
|
123
|
+
clear() {
|
|
124
|
+
this.cache.clear();
|
|
125
|
+
this.dirty = true;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get all keys in cache.
|
|
129
|
+
*/
|
|
130
|
+
keys() {
|
|
131
|
+
return [...this.cache.keys()];
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Get cache statistics.
|
|
135
|
+
*/
|
|
136
|
+
stats() {
|
|
137
|
+
return {
|
|
138
|
+
size: this.cache.size,
|
|
139
|
+
maxEntries: this.config.maxEntries,
|
|
140
|
+
cacheFile: this.cacheFile,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Load cache from JSON file on disk.
|
|
145
|
+
*/
|
|
146
|
+
loadFromDisk() {
|
|
147
|
+
try {
|
|
148
|
+
if (fs.existsSync(this.cacheFile)) {
|
|
149
|
+
const raw = fs.readFileSync(this.cacheFile, 'utf-8');
|
|
150
|
+
const data = JSON.parse(raw);
|
|
151
|
+
for (const [key, entry] of Object.entries(data)) {
|
|
152
|
+
// Skip expired entries while loading
|
|
153
|
+
if (entry.ttl !== null && Date.now() - entry.createdAt > entry.ttl) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
this.cache.set(key, entry);
|
|
157
|
+
}
|
|
158
|
+
console.error(`📦 [CacheManager] Loaded ${this.cache.size} entries from disk`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
163
|
+
console.error(`⚠️ [CacheManager] Failed to load cache: ${msg}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Save cache to JSON file on disk.
|
|
168
|
+
*/
|
|
169
|
+
saveToDisk() {
|
|
170
|
+
try {
|
|
171
|
+
const dir = path.dirname(this.cacheFile);
|
|
172
|
+
if (!fs.existsSync(dir)) {
|
|
173
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
174
|
+
}
|
|
175
|
+
const data = Object.fromEntries(this.cache);
|
|
176
|
+
fs.writeFileSync(this.cacheFile, JSON.stringify(data, null, 2), 'utf-8');
|
|
177
|
+
this.dirty = false;
|
|
178
|
+
}
|
|
179
|
+
catch (e) {
|
|
180
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
181
|
+
console.error(`⚠️ [CacheManager] Failed to save cache: ${msg}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Remove expired entries and oldest entries if over capacity.
|
|
186
|
+
*/
|
|
187
|
+
prune() {
|
|
188
|
+
const now = Date.now();
|
|
189
|
+
// Remove expired entries first
|
|
190
|
+
for (const [key, entry] of this.cache) {
|
|
191
|
+
if (entry.ttl !== null && now - entry.createdAt > entry.ttl) {
|
|
192
|
+
this.cache.delete(key);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// If still over capacity, remove oldest entries
|
|
196
|
+
if (this.cache.size >= this.config.maxEntries) {
|
|
197
|
+
const entries = [...this.cache.entries()].sort((a, b) => a[1].createdAt - b[1].createdAt);
|
|
198
|
+
const toRemove = entries.slice(0, Math.floor(this.config.maxEntries * 0.2)); // Remove oldest 20%
|
|
199
|
+
for (const [key] of toRemove) {
|
|
200
|
+
this.cache.delete(key);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
this.dirty = true;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Cleanup: stop timer and save final state.
|
|
207
|
+
*/
|
|
208
|
+
destroy() {
|
|
209
|
+
if (this.saveTimer) {
|
|
210
|
+
clearInterval(this.saveTimer);
|
|
211
|
+
this.saveTimer = null;
|
|
212
|
+
}
|
|
213
|
+
if (this.dirty) {
|
|
214
|
+
this.saveToDisk();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
exports.CacheManager = CacheManager;
|
|
219
|
+
/** Singleton instance for project-wide use */
|
|
220
|
+
exports.cacheManager = new CacheManager();
|
|
221
|
+
//# sourceMappingURL=cache-manager.js.map
|