utils.go•1.54 kB
package utils
import (
"fmt"
"github.com/go-rod/rod"
"github.com/go-rod/rod-mcp/types/js"
"github.com/mark3labs/mcp-go/mcp"
)
func QueryEleByAria(frame *rod.Page, selector string) (*rod.Element, error) {
return frame.ElementByJS(
rod.Eval(js.QueryEleByAria, selector),
)
}
// OptionalStringArrayParam is a helper function that can be used to fetch a requested parameter from the request.
// It does the following checks:
// 1. Checks if the parameter is present in the request, if not, it returns its zero-value
// 2. If it is present, iterates the elements and checks each is a string
func OptionalStringArrayParam(r mcp.CallToolRequest, p string) ([]string, error) {
// Check if the parameter is present in the request
if _, ok := r.Params.Arguments[p]; !ok {
return []string{}, nil
}
switch v := r.Params.Arguments[p].(type) {
case nil:
return []string{}, nil
case []string:
return v, nil
case []any:
strSlice := make([]string, len(v))
for i, v := range v {
s, ok := v.(string)
if !ok {
return []string{}, fmt.Errorf("parameter %s is not of type string, is %T", p, v)
}
strSlice[i] = s
}
return strSlice, nil
default:
return []string{}, fmt.Errorf("parameter %s could not be coerced to []string, is %T", p, r.Params.Arguments[p])
}
}
func MergeMaps[K comparable, V any](maps ...map[K]V) map[K]V {
result := make(map[K]V)
for _, m := range maps {
for k, v := range m {
result[k] = v // merged map,when the same key exists, the last one will be used
}
}
return result
}