Repository navigation
Expand file tree
/
Copy pathmcp-intercept.go
More file actions
192 lines (168 loc) · 4.95 KB
/
Copy pathmcp-intercept.go
File metadata and controls
192 lines (168 loc) · 4.95 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package devbrowser
import (
"context"
"encoding/base64"
"fmt"
"strings"
twcontext "webtyp.com/context"
"webtyp.com/devbrowser/cdproto/fetch"
"webtyp.com/devbrowser/chromedp"
"webtyp.com/mcp"
)
func (b *DevBrowser) GetInterceptTools() []mcp.Tool {
return []mcp.Tool{
{
Name: "browser_intercept_request",
Description: "Capture request and response bodies for XHR/fetch calls. Use 'start' to begin, 'stop' to end, and 'get' to retrieve captured data.",
InputSchema: mcp.SchemaOf(new(InterceptRequestArgs)),
Resource: "browser",
Action: 'u',
Execute: func(ctx *twcontext.Context, req mcp.Request) (*mcp.Result, error) {
if !b.IsOpenFlag {
return nil, ErrBrowserNotOpen
}
var args InterceptRequestArgs
if err := req.Bind(&args); err != nil {
return nil, err
}
switch args.Action {
case "start":
return b.startInterception(args.Filter)
case "stop":
return b.stopInterception()
case "get":
return b.getInterceptedRequests(args.Filter, int(args.Limit))
default:
return nil, fmt.Errorf("Unknown action: %s. Use 'start', 'stop', or 'get'", args.Action)
}
},
},
}
}
func (b *DevBrowser) startInterception(filter string) (*mcp.Result, error) {
b.InterceptMutex.Lock()
if b.InterceptActive {
b.InterceptMutex.Unlock()
return mcp.Text("Interception is already active"), nil
}
ctx := b.toolCtx() // the tab the developer has in front
b.InterceptActive = true
b.InterceptedReqs = nil
b.interceptCtx = ctx
b.InterceptMutex.Unlock()
// Enable with RequestStageResponse to get bodies
err := chromedp.Run(ctx, fetch.Enable().WithPatterns([]*fetch.RequestPattern{
{URLPattern: "*", RequestStage: fetch.RequestStageResponse},
}))
if err != nil {
b.InterceptMutex.Lock()
b.InterceptActive = false
b.interceptCtx = nil
b.InterceptMutex.Unlock()
return nil, err
}
return mcp.Text("Request interception started"), nil
}
func (b *DevBrowser) stopInterception() (*mcp.Result, error) {
b.InterceptMutex.Lock()
b.InterceptActive = false
ctx := b.interceptCtx
b.interceptCtx = nil
b.InterceptMutex.Unlock()
if ctx == nil || ctx.Err() != nil {
return mcp.Text("Request interception stopped"), nil
}
err := chromedp.Run(ctx, fetch.Disable())
if err != nil {
return nil, err
}
return mcp.Text("Request interception stopped"), nil
}
func (b *DevBrowser) getInterceptedRequests(filter string, limit int) (*mcp.Result, error) {
b.InterceptMutex.Lock()
defer b.InterceptMutex.Unlock()
var filtered []InterceptedRequest
for _, r := range b.InterceptedReqs {
if filter == "" || strings.Contains(r.URL, filter) {
filtered = append(filtered, r)
}
}
if limit > 0 && len(filtered) > limit {
filtered = filtered[len(filtered)-limit:]
}
if len(filtered) == 0 {
return mcp.Text("No requests intercepted"), nil
}
var res strings.Builder
for i, r := range filtered {
if i > 0 {
res.WriteString("\n---\n")
}
fmt.Fprintf(&res, "%d %s %s\n", r.Status, r.Method, r.URL)
if r.RequestBody != "" {
fmt.Fprintf(&res, "Request Body: %s\n", r.RequestBody)
}
if r.ResponseBody != "" {
fmt.Fprintf(&res, "Response Body: %s\n", r.ResponseBody)
}
}
return mcp.Text(res.String()), nil
}
func (b *DevBrowser) initializeInterceptCapture() {
if b.mainTab != nil {
b.captureIntercept(b.mainTab)
}
}
// captureIntercept records tab's paused requests while interception runs on
// it (startInterception enables it on the tab in front).
func (b *DevBrowser) captureIntercept(tab *browserTab) {
chromedp.ListenTarget(tab.ctx, func(ev interface{}) {
b.InterceptMutex.Lock()
active := b.InterceptActive && b.interceptCtx == tab.ctx
b.InterceptMutex.Unlock()
if !active {
return
}
switch ev := ev.(type) {
case *fetch.EventRequestPaused:
go func() {
if ev.ResponseStatusCode != 0 {
intercepted := InterceptedRequest{
URL: ev.Request.URL,
Method: ev.Request.Method,
Status: int(ev.ResponseStatusCode),
}
if ev.Request.HasPostData && len(ev.Request.PostDataEntries) > 0 {
var postData strings.Builder
for _, entry := range ev.Request.PostDataEntries {
decoded, err := base64.StdEncoding.DecodeString(entry.Bytes)
if err == nil {
postData.Write(decoded)
} else {
postData.WriteString(entry.Bytes)
}
}
intercepted.RequestBody = postData.String()
}
var body []byte
err := chromedp.Run(tab.ctx, chromedp.ActionFunc(func(ctx context.Context) error {
var err error
body, err = fetch.GetResponseBody(ev.RequestID).Do(ctx)
return err
}))
if err == nil {
intercepted.ResponseBody = string(body)
}
b.InterceptMutex.Lock()
if len(b.InterceptedReqs) >= 100 {
b.InterceptedReqs = b.InterceptedReqs[1:]
}
b.InterceptedReqs = append(b.InterceptedReqs, intercepted)
b.InterceptMutex.Unlock()
}
// Always continue
chromedp.Run(tab.ctx, fetch.ContinueRequest(ev.RequestID))
}()
}
})
}