-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_list_services.go
More file actions
75 lines (62 loc) · 1.76 KB
/
tool_list_services.go
File metadata and controls
75 lines (62 loc) · 1.76 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
//go:build linux
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
"github.com/AndroidGoLab/binder/servicemanager"
)
// ServiceEntry describes one entry in the list_services result.
type ServiceEntry struct {
Name string `json:"name"`
Alive bool `json:"alive"`
}
func (ts *ToolSet) registerListServices(s *server.MCPServer) {
tool := mcp.NewTool("list_services",
mcp.WithDescription(
"List all registered Android binder services and their liveness status. "+
"Returns a JSON array of {name, alive} objects.",
),
mcp.WithReadOnlyHintAnnotation(true),
mcp.WithDestructiveHintAnnotation(false),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, ts.handleListServices)
}
func (ts *ToolSet) handleListServices(
ctx context.Context,
_ mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleListServices")
defer func() { logger.Tracef(ctx, "/handleListServices") }()
names, err := ts.sm.ListServices(ctx)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("listing services: %v", err)), nil
}
entries := make([]ServiceEntry, 0, len(names))
for _, name := range names {
alive := ts.isServiceAlive(ctx, name)
entries = append(entries, ServiceEntry{
Name: string(name),
Alive: alive,
})
}
data, err := json.Marshal(entries)
if err != nil {
return nil, fmt.Errorf("marshaling service list: %w", err)
}
return mcp.NewToolResultText(string(data)), nil
}
func (ts *ToolSet) isServiceAlive(
ctx context.Context,
name servicemanager.ServiceName,
) bool {
svc, err := ts.sm.CheckService(ctx, name)
if err != nil || svc == nil {
return false
}
return svc.IsAlive(ctx)
}