Repository navigation
Expand file tree
/
Copy pathmcp-structure.go
More file actions
145 lines (121 loc) · 4.45 KB
/
Copy pathmcp-structure.go
File metadata and controls
145 lines (121 loc) · 4.45 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
package devbrowser
import (
"fmt"
"webtyp.com/context"
"webtyp.com/devbrowser/chromedp"
"webtyp.com/mcp"
)
// GetStructureJS is the JavaScript used to extract the page structure for LLM understanding.
const GetStructureJS = `
(() => {
const getStructure = (el, depth = 0) => {
if (depth > 12 || !el) return '';
const tag = el.tagName.toLowerCase();
const indent = ' '.repeat(depth);
const style = window.getComputedStyle(el);
// Skip invisible elements
if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') return '';
// Get direct text content
const directText = Array.from(el.childNodes)
.filter(n => n.nodeType === 3)
.map(n => n.textContent.trim())
.filter(t => t)
.join(' ');
let result = indent + '<' + tag;
if (el.id) result += ' id="' + el.id + '"';
// Handle class names more cleanly
if (el.className && typeof el.className === 'string') {
const classes = el.className.split(/\s+/).filter(c => c).join(' ');
if (classes) result += ' class="' + classes + '"';
}
// Add ARIA roles and labels which are critical for LLMs
const role = el.getAttribute('role');
const ariaLabel = el.getAttribute('aria-label');
const placeholder = el.getAttribute('placeholder');
const value = el.value;
const name = el.getAttribute('name');
const type = el.getAttribute('type');
// NEW: Add critical navigation and media attributes
const href = el.getAttribute('href');
const src = el.getAttribute('src');
const alt = el.getAttribute('alt');
const title = el.getAttribute('title');
if (role) result += ' role="' + role + '"';
if (ariaLabel) result += ' aria-label="' + ariaLabel + '"';
if (placeholder) result += ' placeholder="' + placeholder + '"';
if (name) result += ' name="' + name + '"';
if (type) result += ' type="' + type + '"';
if (value && tag !== 'body') result += ' value="' + value + '"';
if (href) result += ' href="' + href + '"';
if (src) result += ' src="' + src + '"';
if (alt) result += ' alt="' + alt + '"';
if (title) result += ' title="' + title + '"';
// Add critical visual styles (only if non-default)
const styles = [];
if (style.display !== 'block' && style.display !== 'inline' && style.display !== 'inline-block') {
styles.push('display:' + style.display);
if (style.display === 'flex') styles.push('FLEX');
if (style.display === 'grid') styles.push('GRID');
}
if (style.position !== 'static') styles.push('position:' + style.position);
if (style.position === 'absolute' || style.position === 'fixed') {
styles.push('top:' + style.top);
styles.push('left:' + style.left);
}
// Indicate interactive items
const isClickable = window.getComputedStyle(el).cursor === 'pointer' ||
tag === 'button' || tag === 'a' || tag === 'input' || tag === 'select';
if (isClickable) styles.push('clickable');
if (styles.length > 0) result += ' [' + styles.join(' ') + ']';
result += '>';
if (directText) result += ' ' + directText;
result += '\n';
// Recurse for children
Array.from(el.children).forEach(child => {
result += getStructure(child, depth + 1);
});
return result;
};
return getStructure(document.body);
})()
`
func (b *DevBrowser) GetStructureTools() []mcp.Tool {
return []mcp.Tool{
{
Name: "browser_get_content",
Description: "Get a text-based representation of the page content, optimized for LLM reading. Reduced token count compared to screenshots.",
InputSchema: mcp.SchemaOf(new(GetContentArgs)),
Resource: "browser",
Action: 'r',
Execute: func(Ctx *context.Context, req mcp.Request) (*mcp.Result, error) {
if !b.IsOpenFlag {
return nil, ErrBrowserNotOpen
}
var args GetContentArgs
if err := req.Bind(&args); err != nil {
return nil, err
}
var pageTitle, pageURL, structure string
var windowWidth, windowHeight int
err := chromedp.Run(b.toolCtx(),
chromedp.Title(&pageTitle),
chromedp.Location(&pageURL),
chromedp.Evaluate(`window.innerWidth`, &windowWidth),
chromedp.Evaluate(`window.innerHeight`, &windowHeight),
chromedp.Evaluate(GetStructureJS, &structure),
)
if err != nil {
return nil, fmt.Errorf("Failed to get page structure: %v", err)
}
report := fmt.Sprintf(
"Url: %s\nTitle: %s\nViewport: %dx%d\n\n%s",
pageURL,
pageTitle,
windowWidth, windowHeight,
structure,
)
return mcp.Text(report), nil
},
},
}
}