-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_list_notifications.go
More file actions
98 lines (80 loc) · 2.51 KB
/
tool_list_notifications.go
File metadata and controls
98 lines (80 loc) · 2.51 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
97
98
//go:build linux
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// NotificationEntry describes one active notification parsed from dumpsys.
type NotificationEntry struct {
Key string `json:"key"`
Package string `json:"package,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
}
func registerListNotifications(s *server.MCPServer) {
tool := mcp.NewTool("list_notifications",
mcp.WithDescription(
"List active notifications on the device. Parses output from "+
"'dumpsys notification --noredact'. Returns package and key for each.",
),
mcp.WithReadOnlyHintAnnotation(true),
mcp.WithDestructiveHintAnnotation(false),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, handleListNotifications)
}
func handleListNotifications(
ctx context.Context,
_ mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleListNotifications")
defer func() { logger.Tracef(ctx, "/handleListNotifications") }()
out, err := shellExec("dumpsys notification --noredact 2>/dev/null | grep -A 2 'NotificationRecord' | head -200")
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("dumpsys notification: %v", err)), nil
}
entries := parseNotifications(out)
data, err := json.Marshal(entries)
if err != nil {
return nil, fmt.Errorf("marshaling notifications: %w", err)
}
return mcp.NewToolResultText(string(data)), nil
}
// parseNotifications extracts notification entries from dumpsys output.
// Lines look like: " NotificationRecord(0x... | ... pkg=com.foo key=0|com.foo|...)"
func parseNotifications(output string) []NotificationEntry {
var entries []NotificationEntry
for _, line := range strings.Split(output, "\n") {
line = strings.TrimSpace(line)
if !strings.Contains(line, "NotificationRecord") {
continue
}
entry := NotificationEntry{}
// Extract "key=..." and "pkg=..." fields.
if idx := strings.Index(line, "pkg="); idx >= 0 {
rest := line[idx+4:]
if sp := strings.IndexAny(rest, " )"); sp > 0 {
entry.Package = rest[:sp]
} else {
entry.Package = rest
}
}
if idx := strings.Index(line, "key="); idx >= 0 {
rest := line[idx+4:]
if sp := strings.IndexAny(rest, " )"); sp > 0 {
entry.Key = rest[:sp]
} else {
entry.Key = rest
}
}
if entry.Key != "" || entry.Package != "" {
entries = append(entries, entry)
}
}
return entries
}