-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-inspect.go
More file actions
153 lines (135 loc) · 3.91 KB
/
mcp-inspect.go
File metadata and controls
153 lines (135 loc) · 3.91 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
package devbrowser
import (
"fmt"
"github.com/tinywasm/context"
"github.com/tinywasm/devbrowser/chromedp"
"github.com/tinywasm/mcp"
)
// InspectElementJS extracts detailed element information like Chrome DevTools.
// Returns a JSON-like string with box model, position, styles, and accessibility info.
const InspectElementJS = `
(selector) => {
const el = document.querySelector(selector);
if (!el) return JSON.stringify({ error: 'Element not found: ' + selector });
const rect = el.getBoundingClientRect();
const style = window.getComputedStyle(el);
// Box Model
const boxModel = {
width: rect.width,
height: rect.height,
padding: {
top: parseFloat(style.paddingTop),
right: parseFloat(style.paddingRight),
bottom: parseFloat(style.paddingBottom),
left: parseFloat(style.paddingLeft)
},
margin: {
top: parseFloat(style.marginTop),
right: parseFloat(style.marginRight),
bottom: parseFloat(style.marginBottom),
left: parseFloat(style.marginLeft)
},
border: {
top: parseFloat(style.borderTopWidth),
right: parseFloat(style.borderRightWidth),
bottom: parseFloat(style.borderBottomWidth),
left: parseFloat(style.borderLeftWidth)
}
};
// Position
const position = {
type: style.position,
top: rect.top,
left: rect.left,
right: rect.right,
bottom: rect.bottom,
// Offset from document
offsetTop: el.offsetTop,
offsetLeft: el.offsetLeft,
// Scroll position
scrollTop: el.scrollTop,
scrollLeft: el.scrollLeft
};
// Layout
const layout = {
display: style.display,
flexDirection: style.flexDirection,
justifyContent: style.justifyContent,
alignItems: style.alignItems,
gridTemplateColumns: style.gridTemplateColumns,
gridTemplateRows: style.gridTemplateRows,
gap: style.gap,
overflow: style.overflow,
zIndex: style.zIndex
};
// Typography
const typography = {
fontFamily: style.fontFamily,
fontSize: style.fontSize,
fontWeight: style.fontWeight,
lineHeight: style.lineHeight,
textAlign: style.textAlign,
color: style.color
};
// Background
const background = {
color: style.backgroundColor,
image: style.backgroundImage !== 'none' ? style.backgroundImage : null
};
// Accessibility
const accessibility = {
role: el.getAttribute('role'),
ariaLabel: el.getAttribute('aria-label'),
ariaDescribedBy: el.getAttribute('aria-describedby'),
tabIndex: el.tabIndex,
isKeyboardFocusable: el.tabIndex >= 0 || ['A', 'BUTTON', 'INPUT', 'SELECT', 'TEXTAREA'].includes(el.tagName)
};
// Element identity
const identity = {
tagName: el.tagName.toLowerCase(),
id: el.id || null,
className: el.className || null,
name: el.getAttribute('name')
};
return JSON.stringify({
identity,
boxModel,
position,
layout,
typography,
background,
accessibility
}, null, 2);
}
`
func (b *DevBrowser) GetInspectTools() []mcp.Tool {
return []mcp.Tool{
{
Name: "browser_inspect_element",
Description: "Inspect a specific element to get detailed CSS properties like Chrome DevTools. Returns box model (width, height, padding, margin, border), position (top, left, offset), layout (display, flex, grid), typography (font, color), and accessibility info.",
InputSchema: EncodeSchema(new(InspectElementArgs)),
Resource: "browser",
Action: 'r',
Execute: func(ctx *context.Context, req mcp.Request) (*mcp.Result, error) {
if !b.IsOpenFlag {
return nil, fmt.Errorf("Browser is not open. Please open it first with browser_open")
}
var args InspectElementArgs
if err := req.Bind(&args); err != nil {
return nil, err
}
var result string
js := fmt.Sprintf("(%s)(%q)", InspectElementJS, args.Selector)
err := chromedp.Run(b.Ctx,
chromedp.Evaluate(js, &result),
)
if err != nil {
return nil, fmt.Errorf("Failed to inspect element: %v", err)
}
msg := fmt.Sprintf("Inspect Element: %s\n%s", args.Selector, result)
b.Logger(msg)
return mcp.Text(msg), nil
},
},
}
}