Files
browser_mcp/tools_session.go
T

105 lines
4.0 KiB
Go

package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
"fn-registry/functions/browser"
)
// registerSessionTools wires browser_launch (MUTA), browser_connect, browser_disconnect.
func registerSessionTools(s *server.MCPServer, d *deps) {
if !d.readOnly {
s.AddTool(launchTool(), mcp.NewTypedToolHandler(d.handleLaunch))
}
s.AddTool(connectTool(), mcp.NewTypedToolHandler(d.handleConnect))
s.AddTool(disconnectTool(), mcp.NewTypedToolHandler(d.handleDisconnect))
}
// ---- browser_launch (MUTA) ----
type launchArgs struct {
Port int `json:"port"`
Headless bool `json:"headless"`
UserDataDir string `json:"user_data_dir"`
URL string `json:"url"`
}
func launchTool() mcp.Tool {
return mcp.NewTool("browser_launch",
mcp.WithDescription("Launch a Chrome/Chromium instance with CDP remote debugging enabled. By default launches a Chrome ISOLATED from the user's daily browser: port 9333 (default) and a dedicated user_data_dir under the temp dir. This keeps the agent off the daily chromium on 9222 (banking, email). Returns the launched PID. Waits up to 15s for the CDP port to be ready."),
mcp.WithNumber("port", mcp.Description("CDP remote debugging port. Default 9333 (the MCP's isolated Chrome). Pass 9222 only to attach to the daily browser.")),
mcp.WithBoolean("headless", mcp.Description("Run headless (--headless=new). Default false.")),
mcp.WithString("user_data_dir", mcp.Description("Chrome profile directory. Empty = a dedicated isolated dir (<tmp>/browser_mcp_userdata), kept separate from the daily browser profile.")),
mcp.WithString("url", mcp.Description("Optional initial URL to open on launch.")),
)
}
func (d *deps) handleLaunch(_ context.Context, _ mcp.CallToolRequest, a launchArgs) (*mcp.CallToolResult, error) {
// SECURITY (P0.3): default to an isolated user-data-dir so the MCP never
// reuses the user's daily browser profile. Created on demand.
userDataDir := a.UserDataDir
if userDataDir == "" {
userDataDir = filepath.Join(os.TempDir(), "browser_mcp_userdata")
_ = os.MkdirAll(userDataDir, 0o755)
}
opts := browser.ChromeLaunchOpts{
Port: portOr(a.Port),
Headless: a.Headless,
UserDataDir: userDataDir,
}
if a.URL != "" {
opts.ExtraArgs = append(opts.ExtraArgs, a.URL)
}
pid, err := browser.ChromeLaunch(opts)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
return mcp.NewToolResultText(fmt.Sprintf("launched pid=%d port=%d user_data_dir=%s", pid, opts.Port, userDataDir)), nil
}
// ---- browser_connect ----
type connectArgs struct {
Port int `json:"port"`
}
func connectTool() mcp.Tool {
return mcp.NewTool("browser_connect",
mcp.WithDescription("Open (and pool) a CDP WebSocket connection to a running Chrome's first 'page' tab on the given port. Subsequent tools reuse this live session."),
mcp.WithNumber("port", mcp.Description("CDP port. Default 9333 (Chrome isolated del MCP); usa 9222 explícito solo para adjuntarte al navegador diario.")),
)
}
func (d *deps) handleConnect(_ context.Context, _ mcp.CallToolRequest, a connectArgs) (*mcp.CallToolResult, error) {
port := portOr(a.Port)
if _, err := d.pool.get(port); err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
return mcp.NewToolResultText(fmt.Sprintf("connected port=%d", port)), nil
}
// ---- browser_disconnect ----
type disconnectArgs struct {
Port int `json:"port"`
}
func disconnectTool() mcp.Tool {
return mcp.NewTool("browser_disconnect",
mcp.WithDescription("Close and drop the pooled CDP connection for the given port (cancels any armed dialog handler). Does NOT kill Chrome."),
mcp.WithNumber("port", mcp.Description("CDP port. Default 9333 (Chrome isolated del MCP); usa 9222 explícito solo para adjuntarte al navegador diario.")),
)
}
func (d *deps) handleDisconnect(_ context.Context, _ mcp.CallToolRequest, a disconnectArgs) (*mcp.CallToolResult, error) {
port := portOr(a.Port)
d.pool.drop(port)
return mcp.NewToolResultText(fmt.Sprintf("disconnected port=%d", port)), nil
}