-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path10-mcp-server.js
More file actions
139 lines (116 loc) · 3.84 KB
/
Copy path10-mcp-server.js
File metadata and controls
139 lines (116 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { randomUUID } from 'crypto';
import {
chromium
} from 'playwright';
import * as fs from 'fs';
const sessionId = randomUUID();
fs.mkdirSync(`screenshots/${sessionId}/`, { recursive: true });
fs.mkdirSync(`artefacts/${sessionId}/`, { recursive: true });
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Create an MCP server
const server = new McpServer({
name: "Web Automation Server",
version: "1.0.0"
});
server.tool("getPageInfo",
async () => {
const title = await page.title();
const url = page.url();
return { content: [{ type: "text", text: `Current page: ${title}, URL: ${url}` }] }
}
);
server.tool("navigate",
{
url: z.string(),
},
async ({ url }) => {
console.log("Navigating to", url);
await page.goto(url, { timeout: 80000, waitUntil: 'domcontentloaded' });
const title = await page.title();
return { content: [{ type: "text", text: title }] }
}
);
server.tool("screenshot",
async () => {
const filename = `screenshots/${sessionId}/screenshot_${randomUUID()}.png`;
console.log("Taking screenshot", filename);
await page.screenshot({ path: filename, quality: 80, type: "jpeg" });
return { content: [{ type: "text", text: filename }] }
}
);
server.tool("type",
{
text: z.string(),
submit: z.boolean().optional().default(false),
},
async ({ text, submit }) => {
try {
await page.keyboard.type(text);
if (submit) {
console.log(`Pressing Enter to submit`);
await page.keyboard.press('Enter');
return { content: [{ type: "text", text: "submited" }] }
}
return { content: [{ type: "text", text: "not submited" }] }
} catch (error) {
console.log(`Error typing text: ${error}`);
return { content: [{ type: "text", text: error.message }] }
}
}
);
server.tool("click",
{
x: z.number(),
y: z.number()
},
async ({x, y}) => {
await page.mouse.click(x, y);
await sleep(1000);
return { content: [{ type: "text", text: `clicked at ${x},${y}` }] }
}
);
server.tool("scroll",
{
direction: z.string(),
amount: z.number().optional().default(500)
},
async ({ direction, amount }) => {
if (direction.toLowerCase() === "down") {
await page.evaluate(`window.scrollBy(0, ${amount})`);
await page.waitForTimeout(2000);
} else if (direction.toLowerCase() === "up") {
await page.evaluate(`window.scrollBy(0, ${amount})`);
await page.waitForTimeout(2000);
} else {
return { content: [{ type: "text", text: "not scrolled" }] }
}
await sleep(1000);
return { content: [{ type: "text", text: "scrolled" }] }
}
);
server.tool("writeFile",
{
filename: z.string(),
content: z.string()
},
async ({ filename, content }) => {
try {
fs.writeFileSync(`artefacts/${sessionId}/${filename}`, content, 'utf8');
return { content: [{ type: "text", text: filename }] }
} catch (error) {
console.log(`Error writing file: ${error}`);
return { content: [{ type: "text", text: error.message }] }
}
}
);
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://example.com', { waitUntil: 'networkidle' });
console.log("Browser resources initialized");
const transport = new StdioServerTransport();
await server.connect(transport);