-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·166 lines (151 loc) · 5.52 KB
/
index.js
File metadata and controls
executable file
·166 lines (151 loc) · 5.52 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
const { Server } = require("@modelcontextprotocol/sdk/server/index.js");
const { StdioServerTransport } = require("@modelcontextprotocol/sdk/server/stdio.js");
const {
CallToolRequestSchema,
ListToolsRequestSchema,
} = require("@modelcontextprotocol/sdk/types.js");
const https = require("https");
const GRABSHOT_BASE = "https://grabshot.dev";
function makeRequest(path, apiKey, params = {}) {
return new Promise((resolve, reject) => {
const url = new URL(path, GRABSHOT_BASE);
Object.entries(params).forEach(([k, v]) => {
if (v !== undefined && v !== null) url.searchParams.set(k, String(v));
});
const options = {
hostname: url.hostname,
path: url.pathname + url.search,
method: "GET",
headers: { "X-API-Key": apiKey },
};
const req = https.request(options, (res) => {
const chunks = [];
res.on("data", (c) => chunks.push(c));
res.on("end", () => {
const body = Buffer.concat(chunks);
if (res.headers["content-type"]?.includes("image")) {
resolve({
type: "image",
data: body.toString("base64"),
mimeType: res.headers["content-type"],
});
} else {
try {
resolve({ type: "json", data: JSON.parse(body.toString()) });
} catch {
resolve({ type: "text", data: body.toString() });
}
}
});
});
req.on("error", reject);
req.end();
});
}
const server = new Server(
{ name: "grabshot-mcp-server", version: "1.0.0" },
{ capabilities: { tools: {} } }
);
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: "take_screenshot",
description:
"Capture a screenshot of any website URL. Supports full-page capture, device frames (iPhone, MacBook, iPad, etc.), AI cleanup (removes popups/banners), custom viewport sizes, and multiple formats (png, jpeg, webp).",
inputSchema: {
type: "object",
properties: {
url: { type: "string", description: "Website URL to screenshot (must include https://)" },
full_page: { type: "boolean", description: "Capture full page (not just viewport)", default: false },
device_frame: {
type: "string",
description: "Wrap screenshot in a device frame",
enum: ["none", "iphone-15-pro", "macbook-pro", "ipad-pro", "pixel-8", "galaxy-s24"],
},
ai_cleanup: { type: "boolean", description: "Use AI to remove popups, cookie banners, and overlays", default: false },
width: { type: "number", description: "Viewport width in pixels", default: 1280 },
height: { type: "number", description: "Viewport height in pixels", default: 800 },
format: { type: "string", enum: ["png", "jpeg", "webp"], default: "png" },
delay: { type: "number", description: "Wait milliseconds before capture (for dynamic content)", default: 0 },
},
required: ["url"],
},
},
{
name: "get_usage",
description: "Check your GrabShot API usage and remaining quota for the current billing period.",
inputSchema: { type: "object", properties: {} },
},
],
}));
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const apiKey = process.env.GRABSHOT_API_KEY;
if (!apiKey) {
return {
content: [{ type: "text", text: "Error: GRABSHOT_API_KEY environment variable is not set. Get a free API key at https://grabshot.dev" }],
isError: true,
};
}
const { name, arguments: args } = request.params;
if (name === "take_screenshot") {
try {
const result = await makeRequest("/api/screenshot", apiKey, {
url: args.url,
fullPage: args.full_page,
deviceFrame: args.device_frame,
aiCleanup: args.ai_cleanup,
width: args.width,
height: args.height,
format: args.format || "png",
delay: args.delay,
});
if (result.type === "image") {
return {
content: [
{ type: "image", data: result.data, mimeType: result.mimeType },
{ type: "text", text: `Screenshot captured successfully for ${args.url}` },
],
};
} else if (result.type === "json" && result.data.error) {
return {
content: [{ type: "text", text: `Error: ${result.data.error}` }],
isError: true,
};
}
return { content: [{ type: "text", text: JSON.stringify(result.data) }] };
} catch (err) {
return {
content: [{ type: "text", text: `Request failed: ${err.message}` }],
isError: true,
};
}
}
if (name === "get_usage") {
try {
const result = await makeRequest("/api/usage", apiKey);
if (result.type === "json") {
const d = result.data;
return {
content: [{
type: "text",
text: `GrabShot Usage:\n- Plan: ${d.plan || "free"}\n- Screenshots used: ${d.used || 0}/${d.limit || 25}\n- Period: ${d.period || "monthly"}`,
}],
};
}
return { content: [{ type: "text", text: result.data }] };
} catch (err) {
return {
content: [{ type: "text", text: `Failed to get usage: ${err.message}` }],
isError: true,
};
}
}
return { content: [{ type: "text", text: `Unknown tool: ${name}` }], isError: true };
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("GrabShot MCP server running on stdio");
}
main().catch(console.error);