Repository navigation
Expand file tree
/
Copy pathmcp-navigation.go
More file actions
96 lines (86 loc) · 2.8 KB
/
Copy pathmcp-navigation.go
File metadata and controls
96 lines (86 loc) · 2.8 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
package devbrowser
import (
"fmt"
"net/url"
"strings"
"webtyp.com/devbrowser/chromedp"
"webtyp.com/context"
"webtyp.com/mcp"
)
func (b *DevBrowser) GetNavigationTools() []mcp.Tool {
return []mcp.Tool{
{
Name: "browser_navigate",
Description: "Navigate the browser to a specific Url or relative path (e.g. /login)",
InputSchema: mcp.SchemaOf(new(NavigateArgs)),
Resource: "browser",
Action: 'u',
Execute: func(Ctx *context.Context, req mcp.Request) (*mcp.Result, error) {
if !b.IsOpenFlag {
return nil, ErrBrowserNotOpen
}
var args NavigateArgs
if err := req.Bind(&args); err != nil {
return nil, err
}
targetURL, err := b.resolveToolURL(args.Url)
if err != nil {
return nil, err
}
if err := b.navigateTool(targetURL); err != nil {
return nil, fmt.Errorf("Error navigating to %s: %v", targetURL, err)
}
var current string
if ctx := b.toolCtx(); ctx == nil {
return mcp.Text(fmt.Sprintf("Navigated to %s", targetURL)), nil
} else if err := chromedp.Run(ctx, chromedp.Location(¤t)); err != nil {
return mcp.Text(fmt.Sprintf("Navigated to %s (could not get current Url: %v)", targetURL, err)), nil
}
return mcp.Text(fmt.Sprintf("Navigated to %s (current: %s)", targetURL, current)), nil
},
},
}
}
// resolveToolURL makes a relative path absolute against the tab the developer
// has in front ("/login" on user3.localhost stays on user3.localhost); with
// no page loaded yet, against the app's localhost port.
func (b *DevBrowser) resolveToolURL(target string) (string, error) {
if strings.Contains(target, "://") {
return target, nil
}
var baseURL string
var current string
if ctx := b.toolCtx(); ctx != nil && chromedp.Run(ctx, chromedp.Location(¤t)) == nil {
if u, err := url.Parse(current); err == nil && u.Host != "" && (u.Scheme == "http" || u.Scheme == "https") {
baseURL = u.Scheme + "://" + u.Host
}
}
if baseURL == "" {
if b.LastPort == "" {
return "", fmt.Errorf("browser has no active app port; open the app first")
}
scheme := "http"
if b.LastHttps {
scheme = "https"
}
baseURL = fmt.Sprintf("%s://localhost:%s", scheme, b.LastPort)
}
base, err := url.Parse(baseURL)
if err != nil {
return "", fmt.Errorf("failed to parse base Url: %v", err)
}
rel, err := url.Parse(target)
if err != nil {
return "", fmt.Errorf("failed to parse target path: %v", err)
}
return base.ResolveReference(rel).String(), nil
}
// navigateTool navigates the tab the developer has in front; the main tab
// goes through NavigateToURL, which also keeps the load guard's origin.
func (b *DevBrowser) navigateTool(target string) error {
ctx := b.toolCtx()
if ctx == nil || ctx == b.Ctx {
return b.NavigateToURL(target)
}
return chromedp.Run(ctx, chromedp.Navigate(target))
}