browser-console-mcp 1.0.0
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/LICENSE +21 -0
- package/bin/browser-mcp-server.js +75 -0
- package/bin/cli.js +65 -0
- package/bin/copy-html2canvas.js +51 -0
- package/dist/browser/browser-inject.js +34 -0
- package/dist/browser/browser-mcp-server.js +718 -0
- package/dist/browser/index.d.ts +10 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/client/browser-console-mcp.js +2 -0
- package/dist/client/browser-console-mcp.js.map +1 -0
- package/dist/client/index.d.ts +78 -0
- package/dist/client/index.js +451 -0
- package/dist/server/index.d.ts +48 -0
- package/dist/server/index.js +154 -0
- package/dist/server/static-server.d.ts +44 -0
- package/dist/server/static-server.js +205 -0
- package/dist/static/html2canvas/dist/html2canvas.min.js +20 -0
- package/dist/static/html2canvas.min.js +20 -0
- package/mcp.json +15 -0
- package/package.json +61 -0
- package/readme.md +347 -0
- package/rollup.browser.config.js +23 -0
- package/rollup.config.js +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yolin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Browser MCP Server CLI
|
|
5
|
+
*
|
|
6
|
+
* Command line entry point for the browser MCP server
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { spawn } from "node:child_process";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
|
|
13
|
+
// Get current file directory
|
|
14
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
15
|
+
const __dirname = dirname(__filename);
|
|
16
|
+
const rootDir = join(__dirname, "..");
|
|
17
|
+
|
|
18
|
+
// Command line arguments
|
|
19
|
+
const args = process.argv.slice(2);
|
|
20
|
+
const port =
|
|
21
|
+
args.find((arg) => !arg.startsWith("-")) || process.env.PORT || "7898";
|
|
22
|
+
|
|
23
|
+
// Set environment variables
|
|
24
|
+
process.env.PORT = port;
|
|
25
|
+
|
|
26
|
+
// Ensure stdout is only used for JSON messages
|
|
27
|
+
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
|
|
28
|
+
process.stdout.write = (chunk, encoding, callback) => {
|
|
29
|
+
// Only allow JSON messages through
|
|
30
|
+
if (typeof chunk === "string" && !chunk.startsWith("{")) {
|
|
31
|
+
process.stderr.write(chunk); // Redirect non-JSON messages to stderr
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
return originalStdoutWrite(chunk, encoding, callback);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Start browser MCP server
|
|
38
|
+
console.error(`Starting Browser MCP server, listening on port ${port}...`);
|
|
39
|
+
|
|
40
|
+
// Ensure stdio is correctly passed
|
|
41
|
+
const child = spawn("node", [join(rootDir, "dist/browser/index.js")], {
|
|
42
|
+
stdio: ["inherit", "inherit", "inherit"], // Ensure stdin, stdout, stderr are all correctly passed
|
|
43
|
+
env: { ...process.env, PORT: port },
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Handle child process events
|
|
47
|
+
child.on("error", (err) => {
|
|
48
|
+
console.error("Failed to start child process:", err);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
child.on("exit", (code, signal) => {
|
|
53
|
+
if (code !== null) {
|
|
54
|
+
console.error(`Child process exited with code: ${code}`);
|
|
55
|
+
} else if (signal !== null) {
|
|
56
|
+
console.error(`Child process terminated by signal: ${signal}`);
|
|
57
|
+
}
|
|
58
|
+
process.exit(code || 0);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Pass parent process signals to child process
|
|
62
|
+
process.on("SIGINT", () => {
|
|
63
|
+
child.kill("SIGINT");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
process.on("SIGTERM", () => {
|
|
67
|
+
child.kill("SIGTERM");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Handle parent process exit
|
|
71
|
+
process.on("exit", () => {
|
|
72
|
+
if (!child.killed) {
|
|
73
|
+
child.kill();
|
|
74
|
+
}
|
|
75
|
+
});
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Browser Console MCP CLI
|
|
5
|
+
*
|
|
6
|
+
* Command line entry point for starting the MCP server
|
|
7
|
+
* Supports running directly through npx or after global installation
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* - Global installation: browser-console-mcp start
|
|
11
|
+
* - npx: npx browser-console-mcp start
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { spawn } from "node:child_process";
|
|
15
|
+
import { dirname, join } from "node:path";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
|
|
18
|
+
// Get current file directory
|
|
19
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
20
|
+
const __dirname = dirname(__filename);
|
|
21
|
+
const rootDir = join(__dirname, "..");
|
|
22
|
+
|
|
23
|
+
// Command line arguments
|
|
24
|
+
const args = process.argv.slice(2);
|
|
25
|
+
const command = args[0] || "help";
|
|
26
|
+
|
|
27
|
+
// Process command
|
|
28
|
+
switch (command) {
|
|
29
|
+
case "start":
|
|
30
|
+
// Start server-side MCP server
|
|
31
|
+
console.log("Starting Browser Console MCP server...");
|
|
32
|
+
spawn("node", [join(rootDir, "dist/server/index.js")], {
|
|
33
|
+
stdio: "inherit",
|
|
34
|
+
});
|
|
35
|
+
break;
|
|
36
|
+
|
|
37
|
+
case "start:browser":
|
|
38
|
+
// Start browser MCP server
|
|
39
|
+
console.log("Starting Browser MCP server...");
|
|
40
|
+
spawn("node", [join(rootDir, "dist/browser/index.js")], {
|
|
41
|
+
stdio: "inherit",
|
|
42
|
+
});
|
|
43
|
+
break;
|
|
44
|
+
|
|
45
|
+
default:
|
|
46
|
+
console.log(`
|
|
47
|
+
Browser Console MCP CLI
|
|
48
|
+
|
|
49
|
+
Usage:
|
|
50
|
+
browser-console-mcp <command> [options]
|
|
51
|
+
npx browser-console-mcp <command> [options]
|
|
52
|
+
|
|
53
|
+
Commands:
|
|
54
|
+
start Start server-side MCP server
|
|
55
|
+
start:browser Start browser MCP server
|
|
56
|
+
help Display help information
|
|
57
|
+
|
|
58
|
+
Examples:
|
|
59
|
+
browser-console-mcp start
|
|
60
|
+
npx browser-console-mcp start
|
|
61
|
+
browser-console-mcp start:browser
|
|
62
|
+
npx browser-console-mcp start:browser
|
|
63
|
+
`);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copy html2canvas to static resources directory
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
// Get current file directory
|
|
10
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
11
|
+
const __dirname = path.dirname(__filename);
|
|
12
|
+
const projectRoot = path.resolve(__dirname, "..");
|
|
13
|
+
|
|
14
|
+
// Source file path
|
|
15
|
+
const sourcePath = path.join(
|
|
16
|
+
projectRoot,
|
|
17
|
+
"node_modules",
|
|
18
|
+
"html2canvas",
|
|
19
|
+
"dist",
|
|
20
|
+
"html2canvas.min.js",
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
// Target directory
|
|
24
|
+
const targetDir = path.join(projectRoot, "dist", "static");
|
|
25
|
+
const targetPath = path.join(targetDir, "html2canvas.min.js");
|
|
26
|
+
|
|
27
|
+
// Ensure target directory exists
|
|
28
|
+
function ensureDirectoryExists(dirPath) {
|
|
29
|
+
if (!fs.existsSync(dirPath)) {
|
|
30
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
31
|
+
console.log(`Creating directory: ${dirPath}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Copy file
|
|
36
|
+
function copyFile(source, target) {
|
|
37
|
+
try {
|
|
38
|
+
const data = fs.readFileSync(source);
|
|
39
|
+
fs.writeFileSync(target, data);
|
|
40
|
+
console.log(`File copied successfully: ${source} -> ${target}`);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error(`Failed to copy file: ${error.message}`);
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Execute copy
|
|
48
|
+
console.log("Starting to copy html2canvas...");
|
|
49
|
+
ensureDirectoryExists(targetDir);
|
|
50
|
+
copyFile(sourcePath, targetPath);
|
|
51
|
+
console.log("html2canvas copy completed");
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser MCP 注入脚本
|
|
3
|
+
*
|
|
4
|
+
* 这个脚本用于在浏览器中注入MCP客户端
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
(() => {
|
|
8
|
+
// 检查是否已经加载了MCP客户端
|
|
9
|
+
if (window.mcp) {
|
|
10
|
+
console.log("[Browser MCP] MCP客户端已加载");
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// 获取当前脚本的URL,以确定服务器地址
|
|
15
|
+
const currentScript = document.currentScript;
|
|
16
|
+
const scriptUrl = currentScript ? currentScript.src : "";
|
|
17
|
+
const serverUrl = scriptUrl
|
|
18
|
+
? new URL(scriptUrl).origin
|
|
19
|
+
: "http://localhost:7898";
|
|
20
|
+
|
|
21
|
+
// 加载MCP客户端脚本
|
|
22
|
+
const script = document.createElement("script");
|
|
23
|
+
script.src = `${serverUrl}/browser-console-mcp.js`;
|
|
24
|
+
script.onload = () => {
|
|
25
|
+
console.log("[Browser MCP] MCP客户端加载成功");
|
|
26
|
+
};
|
|
27
|
+
script.onerror = (error) => {
|
|
28
|
+
console.error("[Browser MCP] MCP客户端加载失败:", error);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
document.head.appendChild(script);
|
|
32
|
+
|
|
33
|
+
console.log("[Browser MCP] 正在加载MCP客户端...");
|
|
34
|
+
})();
|