-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmcp.go
86 lines (78 loc) · 2.35 KB
/
mcp.go
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
package main
import (
"context"
"github.com/urfave/cli/v2"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
func required[T comparable](r mcp.CallToolRequest, p string) T {
var zero T
if _, ok := r.Params.Arguments[p]; !ok {
return zero
}
if _, ok := r.Params.Arguments[p].(T); !ok {
return zero
}
if r.Params.Arguments[p].(T) == zero {
return zero
}
return r.Params.Arguments[p].(T)
}
func optional[T any](r mcp.CallToolRequest, p string) (T, bool) {
var zero T
if _, ok := r.Params.Arguments[p]; !ok {
return zero, false
}
if _, ok := r.Params.Arguments[p].(T); !ok {
return zero, false
}
return r.Params.Arguments[p].(T), true
}
func doMcp(cCtx *cli.Context) error {
s := server.NewMCPServer(
"algia",
version,
)
s.AddTool(mcp.NewTool("send_satoshi",
mcp.WithDescription("send zap to note with specified amount"),
mcp.WithString("note", mcp.Description("Note ID"), mcp.Required()),
mcp.WithNumber("amount", mcp.Description("Zap amount satoshi to the note"), mcp.Required()),
), func(ctx context.Context, r mcp.CallToolRequest) (*mcp.CallToolResult, error) {
err := callZap(&zapArg{
cfg: cCtx.App.Metadata["config"].(*Config),
amount: required[uint64](r, "amount"),
id: required[string](r, "note"),
})
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
return mcp.NewToolResultText("OK"), nil
})
s.AddTool(mcp.NewTool("favorite_nostr_event",
mcp.WithDescription("favorite note"),
mcp.WithString("id", mcp.Description("ID"), mcp.Required()),
), func(ctx context.Context, r mcp.CallToolRequest) (*mcp.CallToolResult, error) {
err := callLike(&likeArg{
cfg: cCtx.App.Metadata["config"].(*Config),
id: required[string](r, "note"),
})
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
return mcp.NewToolResultText("OK"), nil
})
s.AddTool(mcp.NewTool("publish_nostr_event",
mcp.WithDescription("publish note"),
mcp.WithString("content", mcp.Description("Content"), mcp.Required()),
), func(ctx context.Context, r mcp.CallToolRequest) (*mcp.CallToolResult, error) {
err := callPost(&postArg{
cfg: cCtx.App.Metadata["config"].(*Config),
content: required[string](r, "content"),
})
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
return mcp.NewToolResultText("OK"), nil
})
return server.ServeStdio(s)
}