This commit is contained in:
Josh at WLTechBlog
2025-08-15 07:41:30 -05:00
parent dc6b288aa4
commit efab3cc11e
7 changed files with 533 additions and 4 deletions

View File

@@ -687,6 +687,115 @@ func main() {
}, nil
})
// Register console_logs tool
mcpServer.AddTool(mcp.Tool{
Name: "console_logs",
Description: "Get console logs from the browser tab",
InputSchema: mcp.ToolInputSchema{
Type: "object",
Properties: map[string]any{
"tab": map[string]any{
"type": "string",
"description": "Tab ID (optional, uses current tab)",
},
"clear": map[string]any{
"type": "boolean",
"description": "Clear logs after retrieval (default: false)",
"default": false,
},
},
Required: []string{},
},
}, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// Convert arguments to map
params, ok := request.Params.Arguments.(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid arguments format")
}
tab := getStringParam(params, "tab", cremoteServer.currentTab)
clear := getBoolParam(params, "clear", false)
// Get console logs
logs, err := cremoteServer.client.GetConsoleLogs(tab, clear)
if err != nil {
return nil, fmt.Errorf("failed to get console logs: %w", err)
}
// Format logs for display
var logText string
if len(logs) == 0 {
logText = "No console logs found."
} else {
logText = fmt.Sprintf("Found %d console log entries:\n\n", len(logs))
for i, log := range logs {
level := log["level"].(string)
message := log["message"].(string)
timestamp := log["timestamp"].(string)
logText += fmt.Sprintf("[%d] %s [%s]: %s\n", i+1, timestamp, level, message)
}
}
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.NewTextContent(logText),
},
IsError: false,
}, nil
})
// Register console_command tool
mcpServer.AddTool(mcp.Tool{
Name: "console_command",
Description: "Execute a command in the browser console",
InputSchema: mcp.ToolInputSchema{
Type: "object",
Properties: map[string]any{
"command": map[string]any{
"type": "string",
"description": "JavaScript command to execute in console",
},
"tab": map[string]any{
"type": "string",
"description": "Tab ID (optional, uses current tab)",
},
"timeout": map[string]any{
"type": "integer",
"description": "Timeout in seconds (default: 5)",
"default": 5,
},
},
Required: []string{"command"},
},
}, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// Convert arguments to map
params, ok := request.Params.Arguments.(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid arguments format")
}
command := getStringParam(params, "command", "")
tab := getStringParam(params, "tab", cremoteServer.currentTab)
timeout := getIntParam(params, "timeout", 5)
if command == "" {
return nil, fmt.Errorf("command parameter is required")
}
// Execute console command
result, err := cremoteServer.client.ExecuteConsoleCommand(tab, command, timeout)
if err != nil {
return nil, fmt.Errorf("failed to execute console command: %w", err)
}
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.NewTextContent(fmt.Sprintf("Console command executed successfully.\nCommand: %s\nResult: %s", command, result)),
},
IsError: false,
}, nil
})
// Start the server
log.Printf("Cremote MCP server ready")
if err := server.ServeStdio(mcpServer); err != nil {