-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-errors.go
More file actions
90 lines (76 loc) · 2.28 KB
/
mcp-errors.go
File metadata and controls
90 lines (76 loc) · 2.28 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
package devbrowser
import (
"fmt"
"strings"
"time"
"github.com/tinywasm/context"
"github.com/tinywasm/devbrowser/cdproto/runtime"
"github.com/tinywasm/devbrowser/chromedp"
"github.com/tinywasm/mcp"
)
func (b *DevBrowser) GetErrorTools() []mcp.Tool {
return []mcp.Tool{
{
Name: "browser_get_errors",
Description: "Get JavaScript runtime errors and uncaught exceptions to quickly identify crashes, bugs, or WASM panics. Returns error messages with stack traces.",
InputSchema: EncodeSchema(new(GetErrorsArgs)),
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 GetErrorsArgs
if err := req.Bind(&args); err != nil {
return nil, err
}
limit := args.Limit
if limit == 0 {
limit = 20
}
b.ErrorsMutex.Lock()
defer b.ErrorsMutex.Unlock()
if len(b.JsErrors) == 0 {
return mcp.Text("No JavaScript errors captured"), nil
}
start := 0
if len(b.JsErrors) > limit {
start = len(b.JsErrors) - limit
}
var result strings.Builder
for i, err := range b.JsErrors[start:] {
if i > 0 {
result.WriteString("\n---\n")
}
result.WriteString(fmt.Sprintf("Error: %s\n at %s:%d:%d\n%s", err.Message, err.Source, err.LineNumber, err.ColumnNumber, err.StackTrace))
}
return mcp.Text(result.String()), nil
},
},
}
}
func (b *DevBrowser) initializeErrorCapture() {
chromedp.ListenTarget(b.Ctx, func(ev interface{}) {
switch ev := ev.(type) {
case *runtime.EventExceptionThrown:
b.ErrorsMutex.Lock()
defer b.ErrorsMutex.Unlock()
exception := ev.ExceptionDetails
jsErr := JSError{
Message: exception.Text,
Source: exception.URL,
LineNumber: int(exception.LineNumber),
ColumnNumber: int(exception.ColumnNumber),
Timestamp: time.Now(),
}
if exception.Exception != nil && exception.Exception.Description != "" {
jsErr.StackTrace = exception.Exception.Description
}
b.JsErrors = append(b.JsErrors, jsErr)
case *runtime.EventExecutionContextsCleared:
b.ErrorsMutex.Lock()
b.JsErrors = []JSError{}
b.ErrorsMutex.Unlock()
}
})
}