Repository navigation
Expand file tree
/
Copy pathmcp-network.go
More file actions
154 lines (137 loc) · 3.73 KB
/
Copy pathmcp-network.go
File metadata and controls
154 lines (137 loc) · 3.73 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
package devbrowser
import (
"fmt"
"strings"
"sync"
"time"
"webtyp.com/context"
"webtyp.com/devbrowser/cdproto/network"
"webtyp.com/devbrowser/chromedp"
"webtyp.com/mcp"
)
func (b *DevBrowser) GetNetworkTools() []mcp.Tool {
return []mcp.Tool{
{
Name: "browser_get_network_logs",
Description: "Get network requests and responses to debug API calls, asset loading failures, CORS errors, or slow requests. Shows Url, status, method, and timing.",
InputSchema: mcp.SchemaOf(new(GetNetworkLogsArgs)),
Resource: "browser",
Action: 'r',
Execute: func(Ctx *context.Context, req mcp.Request) (*mcp.Result, error) {
if !b.IsOpenFlag {
return nil, ErrBrowserNotOpen
}
var args GetNetworkLogsArgs
if err := req.Bind(&args); err != nil {
return nil, err
}
filter := args.Filter
if filter == "" {
filter = "all"
}
limit := args.Limit
if limit == 0 {
limit = 50
}
b.NetworkMutex.Lock()
defer b.NetworkMutex.Unlock()
var filteredLogs []NetworkLogEntry
for _, log := range b.NetworkLogs {
if filter == "all" || strings.ToLower(log.Type) == filter {
filteredLogs = append(filteredLogs, log)
}
}
if len(filteredLogs) > int(limit) {
filteredLogs = filteredLogs[len(filteredLogs)-int(limit):]
}
if len(filteredLogs) == 0 {
var msg string
if filter == "all" {
msg = "No network requests captured"
} else {
msg = fmt.Sprintf("No %s requests found", filter)
}
return mcp.Text(msg), nil
}
var result strings.Builder
for i, log := range filteredLogs {
if i > 0 {
result.WriteString("\n")
}
status := fmt.Sprintf("%d", log.Status)
if log.Failed {
status = "Failed"
}
fmt.Fprintf(&result, "%s %s %s (%dms) [%s] %s", status, log.Method, log.URL, log.Duration, log.Type, log.ErrorText)
}
return mcp.Text(result.String()), nil
},
},
}
}
func (b *DevBrowser) initializeNetworkCapture() {
if b.mainTab != nil {
b.captureNetwork(b.mainTab)
}
}
// captureNetwork records tab's requests into its own buffer (b.NetworkLogs
// while tab is the active one, see networkOf).
func (b *DevBrowser) captureNetwork(tab *browserTab) {
type requestInfo struct {
Method string
Url string
Time time.Time
}
requests := make(map[network.RequestID]requestInfo)
var mutex sync.Mutex
chromedp.ListenTarget(tab.ctx, func(ev interface{}) {
switch ev := ev.(type) {
case *network.EventRequestWillBeSent:
mutex.Lock()
requests[ev.RequestID] = requestInfo{
Method: ev.Request.Method,
Url: ev.Request.URL,
Time: time.Now(),
}
mutex.Unlock()
if ev.Type == "Document" {
b.NetworkMutex.Lock()
*b.networkOf(tab) = []NetworkLogEntry{}
b.NetworkMutex.Unlock()
}
case *network.EventResponseReceived:
mutex.Lock()
reqInfo, ok := requests[ev.RequestID]
mutex.Unlock()
if ok {
duration := time.Since(reqInfo.Time).Milliseconds()
b.NetworkMutex.Lock()
*b.networkOf(tab) = append(*b.networkOf(tab), NetworkLogEntry{
URL: ev.Response.URL,
Method: reqInfo.Method,
Status: int(ev.Response.Status),
Type: string(ev.Type),
Duration: duration,
})
b.NetworkMutex.Unlock()
}
case *network.EventLoadingFailed:
mutex.Lock()
reqInfo, ok := requests[ev.RequestID]
mutex.Unlock()
if ok {
duration := time.Since(reqInfo.Time).Milliseconds()
b.NetworkMutex.Lock()
*b.networkOf(tab) = append(*b.networkOf(tab), NetworkLogEntry{
URL: reqInfo.Url,
Method: reqInfo.Method,
Type: string(ev.Type),
Duration: duration,
Failed: true,
ErrorText: ev.ErrorText,
})
b.NetworkMutex.Unlock()
}
}
})
}