scrapeless-mcp-server
Version:
Scrapeless Mcp Server
87 lines (86 loc) • 2.61 kB
JavaScript
import { customAlphabet } from "nanoid";
const notRecordRoleList = [
'Page Title',
'Root Web Area',
'RootWebArea',
'generic',
'none',
'paragraph',
'InlineTextBox',
'image'
];
function assembleSnapshot(snap, indent = 0) {
let str = '';
const spaces = ' '.repeat(indent);
for (const child of snap.children || []) {
if (child.role === 'Page Title')
continue;
if (child.role === 'Root Web Area')
continue;
if (!notRecordRoleList.includes(child.role)) {
str += `${spaces}- ${child.role}: ${child.name}\n`;
}
if (child.role === 'image') {
str += `${spaces} - image\n`;
}
if (child.children && child.children.length > 0) {
str += assembleSnapshot(child, !notRecordRoleList.includes(child.role) ? indent + 2 : indent);
}
}
return str;
}
export function defineTool(tool) {
return tool;
}
export async function wrapMcpResponse(request) {
try {
const data = await request();
return {
content: [
{
type: "text",
text: `Response:\n\n${JSON.stringify(data)}`,
},
],
};
}
catch (error) {
return {
content: [
{
type: "text",
text: `Failed to fetch data. Error: ${error.message}`,
},
],
};
}
}
export function wrapMcpBrowserResponse(content) {
return {
content: [
{
type: "text",
text: content,
},
],
};
}
export function uuid() {
const nanoid1 = customAlphabet("0123456789abcdefghijklmnopqrstuvwxyz", 4);
const nanoid2 = customAlphabet("0123456789abcdefghijklmnopqrstuvwxyz", 4);
const nanoid3 = customAlphabet("0123456789abcdefghijklmnopqrstuvwxyz", 4);
const nanoid4 = customAlphabet("0123456789abcdefghijklmnopqrstuvwxyz", 6);
return `${nanoid1()}-${nanoid2()}-${nanoid3()}-${nanoid4()}`;
}
export async function snapshot(page) {
const snap = await page.accessibility.snapshot({ interestingOnly: false, includeIframes: true });
if (!snap) {
return "No accessibility snapshot available.";
}
const url = page.url();
const title = await page.title();
let snapshotString = `- Page URL: ${url}\n`;
snapshotString += `- Page Title: ${title}\n`;
snapshotString += `\`\`\`yaml\n- Page Snapshot\n\`\`\`yaml`;
return snapshotString + assembleSnapshot(snap, 0);
}