Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

Add MCP profile connect tool

zhaodce630a8f8f17
diff --git a/internal/mcp/client.go b/internal/mcp/client.go
index 2b0c54fd..0e11a204 100644
--- a/internal/mcp/client.go
+++ b/internal/mcp/client.go
@@ -9,6 +9,7 @@ import (
"io"
"net/http"
"net/url"
+ "strings"
"time"
)
@@ -34,6 +35,14 @@ func (c *Client) url(path string) string {
return c.BaseURL + path
}
+func (c *Client) profileInstancePath(profile string) string {
+ return "/profiles/" + url.PathEscape(profile) + "/instance"
+}
+
+func (c *Client) dashboardProfilesURL() string {
+ return strings.TrimRight(c.BaseURL, "/") + "/dashboard/profiles"
+}
+
func (c *Client) do(req *http.Request) ([]byte, int, error) {
if c.Token != "" {
req.Header.Set("Authorization", "Bearer "+c.Token)
diff --git a/internal/mcp/client_test.go b/internal/mcp/client_test.go
index 46213a59..3ecdb4d9 100644
--- a/internal/mcp/client_test.go
+++ b/internal/mcp/client_test.go
@@ -139,3 +139,21 @@ func TestClientAuthHeaderAbsentWhenNoToken(t *testing.T) {
t.Fatal(err)
}
}
+
+func TestClientProfileInstancePath(t *testing.T) {
+ c := NewClient("http://localhost:9867", "")
+ got := c.profileInstancePath("work profile")
+ want := "/profiles/work+profile/instance"
+ if got != want {
+ t.Fatalf("profileInstancePath = %q, want %q", got, want)
+ }
+}
+
+func TestClientDashboardProfilesURL(t *testing.T) {
+ c := NewClient("http://localhost:9867/", "")
+ got := c.dashboardProfilesURL()
+ want := "http://localhost:9867/dashboard/profiles"
+ if got != want {
+ t.Fatalf("dashboardProfilesURL = %q, want %q", got, want)
+ }
+}
diff --git a/internal/mcp/handlers.go b/internal/mcp/handlers.go
index 3da5d906..c61a8ebe 100644
--- a/internal/mcp/handlers.go
+++ b/internal/mcp/handlers.go
@@ -43,6 +43,7 @@ func handlerMap(c *Client) map[string]func(context.Context, mcp.CallToolRequest)
"pinchtab_close_tab": handleCloseTab(c),
"pinchtab_health": handleHealth(c),
"pinchtab_cookies": handleCookies(c),
+ "pinchtab_connect_profile": handleConnectProfile(c),
// Utility
"pinchtab_wait": handleWait(),
@@ -74,6 +75,23 @@ func resultFromBytes(body []byte, code int) (*mcp.CallToolResult, error) {
return mcp.NewToolResultText(string(body)), nil
}
+type profileInstanceStatus struct {
+ Name string `json:"name"`
+ Running bool `json:"running"`
+ Status string `json:"status"`
+ Port string `json:"port"`
+ ID string `json:"id"`
+ Error string `json:"error"`
+}
+
+func jsonResult(v any) (*mcp.CallToolResult, error) {
+ body, err := json.MarshalIndent(v, "", " ")
+ if err != nil {
+ return mcp.NewToolResultError(fmt.Sprintf("encode response: %v", err)), nil
+ }
+ return mcp.NewToolResultText(string(body)), nil
+}
+
// ── Navigation handlers ────────────────────────────────────────────────
func handleNavigate(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.CallToolResult, error) {
@@ -347,6 +365,51 @@ func handleCookies(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.C
}
}
+func handleConnectProfile(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.CallToolResult, error) {
+ return func(ctx context.Context, r mcp.CallToolRequest) (*mcp.CallToolResult, error) {
+ profile, err := r.RequireString("profile")
+ if err != nil {
+ return mcp.NewToolResultError(err.Error()), nil
+ }
+
+ body, code, err := c.Get(ctx, c.profileInstancePath(profile), nil)
+ if err != nil {
+ return mcp.NewToolResultError(err.Error()), nil
+ }
+ if code >= 400 {
+ return resultFromBytes(body, code)
+ }
+
+ var status profileInstanceStatus
+ if err := json.Unmarshal(body, &status); err != nil {
+ return resultFromBytes(body, code)
+ }
+
+ resp := map[string]any{
+ "profile": status.Name,
+ "running": status.Running,
+ "status": status.Status,
+ "id": status.ID,
+ "port": status.Port,
+ }
+ if status.Error != "" {
+ resp["error"] = status.Error
+ }
+ if status.Running && status.Port != "" {
+ resp["url"] = c.dashboardProfilesURL()
+ resp["message"] = fmt.Sprintf("Open the dashboard to access the running profile %q.", status.Name)
+ return jsonResult(resp)
+ }
+
+ if status.Status == "starting" {
+ resp["message"] = fmt.Sprintf("Profile %q is starting; no connect URL is available yet.", status.Name)
+ } else {
+ resp["message"] = fmt.Sprintf("Profile %q does not have a running instance.", status.Name)
+ }
+ return jsonResult(resp)
+ }
+}
+
// ── Utility handlers ───────────────────────────────────────────────────
func handleWait() func(context.Context, mcp.CallToolRequest) (*mcp.CallToolResult, error) {
diff --git a/internal/mcp/handlers_test.go b/internal/mcp/handlers_test.go
index a4eaba4f..1a2d26e8 100644
--- a/internal/mcp/handlers_test.go
+++ b/internal/mcp/handlers_test.go
@@ -378,6 +378,74 @@ func TestHandleCookies(t *testing.T) {
}
}
+func TestHandleConnectProfileRunning(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/profiles/work/instance" {
+ t.Fatalf("path = %s, want /profiles/work/instance", r.URL.Path)
+ }
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "name": "work",
+ "running": true,
+ "status": "running",
+ "port": "9868",
+ "id": "inst_123",
+ })
+ }))
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_connect_profile", map[string]any{
+ "profile": "work",
+ }, srv)
+
+ text := resultText(t, r)
+ if !strings.Contains(text, `"profile": "work"`) {
+ t.Fatalf("expected profile in response, got %s", text)
+ }
+ if !strings.Contains(text, `"url": "`+srv.URL+`/dashboard/profiles"`) {
+ t.Fatalf("expected dashboard URL in response, got %s", text)
+ }
+}
+
+func TestHandleConnectProfileNotRunning(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "name": "work",
+ "running": false,
+ "status": "stopped",
+ "port": "",
+ "id": "",
+ })
+ }))
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_connect_profile", map[string]any{
+ "profile": "work",
+ }, srv)
+
+ text := resultText(t, r)
+ if !strings.Contains(text, `"running": false`) {
+ t.Fatalf("expected running=false in response, got %s", text)
+ }
+ if strings.Contains(text, `"url":`) {
+ t.Fatalf("did not expect url in stopped response, got %s", text)
+ }
+ if !strings.Contains(text, `does not have a running instance`) {
+ t.Fatalf("expected no-instance message, got %s", text)
+ }
+}
+
+func TestHandleConnectProfileMissingProfile(t *testing.T) {
+ srv := mockPinchTab()
+ defer srv.Close()
+
+ r := callTool(t, "pinchtab_connect_profile", map[string]any{}, srv)
+ if !r.IsError {
+ t.Fatal("expected error for missing profile")
+ }
+}
+
func TestHandleWait(t *testing.T) {
srv := mockPinchTab()
defer srv.Close()
diff --git a/internal/mcp/server_test.go b/internal/mcp/server_test.go
index 51d23ef6..a5ad0bda 100644
--- a/internal/mcp/server_test.go
+++ b/internal/mcp/server_test.go
@@ -18,8 +18,8 @@ func TestNewServerRegistersAllTools(t *testing.T) {
// The server should have registered all tools.
// We verify by checking that NewServer doesn't panic — the panic
// in NewServer fires if any tool lacks a handler.
- if len(tools) != 21 {
- t.Errorf("expected 21 tools, got %d", len(tools))
+ if len(tools) != 22 {
+ t.Errorf("expected 22 tools, got %d", len(tools))
}
}
diff --git a/internal/mcp/tools.go b/internal/mcp/tools.go
index 1e892d6d..41d9b14c 100644
--- a/internal/mcp/tools.go
+++ b/internal/mcp/tools.go
@@ -110,6 +110,10 @@ func allTools() []mcp.Tool {
mcp.WithDescription("Get cookies for the current page"),
mcp.WithString("tabId", mcp.Description("Target tab ID")),
),
+ mcp.NewTool("pinchtab_connect_profile",
+ mcp.WithDescription("Get the user-facing connect URL and instance status for a profile"),
+ mcp.WithString("profile", mcp.Required(), mcp.Description("Profile name or profile ID")),
+ ),
// ── Utility ─────────────────────────────────────────────────
mcp.NewTool("pinchtab_wait",