-
-
Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathroute_mcp.go
More file actions
130 lines (114 loc) · 4.26 KB
/
route_mcp.go
File metadata and controls
130 lines (114 loc) · 4.26 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"context"
"fmt"
"net/http"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
type contextKey string
const deviceKeyCtxKey contextKey = "device_key"
func init() {
registerRoute("mcp", func(router fiber.Router) {
mcpGenericStreamable := setupGenericMCPServer()
mcpSpecificStreamable := setupSpecificMCPServer()
// Basic endpoint - requires device_key in tool arguments
router.All("/mcp", func(c *fiber.Ctx) error {
return adaptor.HTTPHandlerFunc(mcpGenericStreamable.ServeHTTP)(c)
})
// Device-specific endpoint - device_key is pre-filled from URL path
router.All("/mcp/:device_key", func(c *fiber.Ctx) error {
deviceKey := c.Params("device_key")
return adaptor.HTTPHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), deviceKeyCtxKey, deviceKey)
mcpSpecificStreamable.ServeHTTP(w, r.WithContext(ctx))
})(c)
})
})
}
func setupGenericMCPServer() *server.StreamableHTTPServer {
s := server.NewMCPServer("Bark MCP Server", version,
server.WithToolCapabilities(true),
server.WithRecovery(),
)
opts := getCommonToolOpts()
opts = append(opts,
mcp.WithString("device_key",
mcp.Required(),
mcp.Description("Device Key"),
),
)
s.AddTool(mcp.NewTool("notify", opts...), notifyHandler)
return server.NewStreamableHTTPServer(s,
// Disable SSE streaming to avoid long-lived server->client connections on this deployment path.
server.WithDisableStreaming(true),
)
}
func setupSpecificMCPServer() *server.StreamableHTTPServer {
s := server.NewMCPServer("Bark MCP Server (Specific)", version,
server.WithToolCapabilities(true),
server.WithRecovery(),
)
s.AddTool(mcp.NewTool("notify", getCommonToolOpts()...), notifyHandler)
return server.NewStreamableHTTPServer(s,
// Disable SSE streaming to avoid long-lived server->client connections on this deployment path.
server.WithDisableStreaming(true),
)
}
func notifyHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
args, ok := request.Params.Arguments.(map[string]any)
if !ok {
return mcp.NewToolResultError("Invalid arguments format"), nil
}
// Resolve device_key: tool args > context (from URL)
var deviceKey string
if val, ok := args["device_key"]; ok {
if tmpDeviceKey, ok := val.(string); ok {
deviceKey = tmpDeviceKey
}
}
if val := ctx.Value(deviceKeyCtxKey); val != nil {
if tmpDeviceKey, ok := val.(string); ok {
deviceKey = tmpDeviceKey
}
}
if len(deviceKey) == 0 {
return mcp.NewToolResultError("device_key is required"), nil
}
args["device_key"] = deviceKey
code, err := push(args)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("Failed to send notification: %v (code %d)", err, code)), nil
}
return mcp.NewToolResultText("Notification sent successfully"), nil
}
func getCommonToolOpts() []mcp.ToolOption {
return []mcp.ToolOption{
mcp.WithDescription("Send a notification to a device via Bark"),
mcp.WithString("title", mcp.Description("Notification title")),
mcp.WithString("subtitle", mcp.Description("Notification subtitle")),
mcp.WithString("body", mcp.Description("Notification content")),
mcp.WithString("markdown", mcp.Description("Basic Markdown notification content. Overrides body.")),
mcp.WithString("level",
mcp.Description("Notification level"),
mcp.Enum("critical", "active", "timeSensitive", "passive"),
),
mcp.WithNumber("volume",
mcp.Description("Alert volume for important notification"),
mcp.DefaultNumber(5),
mcp.Max(10),
mcp.Min(0),
),
mcp.WithNumber("badge", mcp.Description("Badge number")),
mcp.WithString("call", mcp.Description("Set to '1' to repeat the notification ringtone")),
mcp.WithString("sound", mcp.Description("Notification sound")),
mcp.WithString("icon", mcp.Description("Notification icon URL")),
mcp.WithString("image", mcp.Description("Notification image URL")),
mcp.WithString("group", mcp.Description("Notification group")),
mcp.WithString("isArchive", mcp.Description("Set to '1' to save the notification or any other value to skip saving")),
mcp.WithString("url", mcp.Description("Click action URL")),
mcp.WithString("copy", mcp.Description("Text to copy on copy action")),
}
}