|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/mark3labs/mcp-go/server" |
| 12 | + redis "github.com/redis/go-redis/v9" |
| 13 | +) |
| 14 | + |
| 15 | +// NewRedisEventQueueBuilder creates a builder that produces Redis-backed event queues |
| 16 | +func NewRedisEventQueueBuilder(redisClient *redis.Client) server.SSEOption { |
| 17 | + return server.WithEventQueueBuilder(func(sessionID string) chan string { |
| 18 | + // Create a buffered channel for local use |
| 19 | + localChan := make(chan string, 100) |
| 20 | + |
| 21 | + // Key for this session's events in Redis |
| 22 | + redisKey := fmt.Sprintf("sse:events:%s", sessionID) |
| 23 | + |
| 24 | + // Start a goroutine to receive messages from Redis and forward to local channel |
| 25 | + go func() { |
| 26 | + ctx := context.Background() |
| 27 | + pubsub := redisClient.Subscribe(ctx, redisKey) |
| 28 | + defer pubsub.Close() |
| 29 | + |
| 30 | + ch := pubsub.Channel() |
| 31 | + for { |
| 32 | + select { |
| 33 | + case msg := <-ch: |
| 34 | + select { |
| 35 | + case localChan <- msg.Payload: |
| 36 | + // Message forwarded successfully |
| 37 | + default: |
| 38 | + // Channel is full, might need handling |
| 39 | + } |
| 40 | + case <-time.After(30 * time.Minute): |
| 41 | + // Timeout after inactivity |
| 42 | + return |
| 43 | + } |
| 44 | + } |
| 45 | + }() |
| 46 | + |
| 47 | + // Create a wrapper channel that publishes to Redis when written to |
| 48 | + wrappedChan := make(chan string, 100) |
| 49 | + go func() { |
| 50 | + ctx := context.Background() |
| 51 | + for event := range wrappedChan { |
| 52 | + // Forward to local channel for this instance |
| 53 | + select { |
| 54 | + case localChan <- event: |
| 55 | + // Also publish to Redis for other instances |
| 56 | + redisClient.Publish(ctx, redisKey, event) |
| 57 | + default: |
| 58 | + // Local channel is full, just publish to Redis |
| 59 | + redisClient.Publish(ctx, redisKey, event) |
| 60 | + } |
| 61 | + } |
| 62 | + close(localChan) // Close local channel when wrapped is closed |
| 63 | + }() |
| 64 | + |
| 65 | + return wrappedChan |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +// NewRedisNotificationChannelBuilder creates a builder that produces Redis-backed notification channels |
| 70 | +func NewRedisNotificationChannelBuilder(redisClient *redis.Client) server.SSEOption { |
| 71 | + return server.WithNotificationChannelBuilder(func(sessionID string) chan mcp.JSONRPCNotification { |
| 72 | + // Create a buffered channel for local use |
| 73 | + localChan := make(chan mcp.JSONRPCNotification, 100) |
| 74 | + |
| 75 | + // Key for this session's notifications in Redis |
| 76 | + redisKey := fmt.Sprintf("sse:notifications:%s", sessionID) |
| 77 | + |
| 78 | + // Start a goroutine to receive notifications from Redis and forward to local channel |
| 79 | + go func() { |
| 80 | + ctx := context.Background() |
| 81 | + pubsub := redisClient.Subscribe(ctx, redisKey) |
| 82 | + defer pubsub.Close() |
| 83 | + |
| 84 | + ch := pubsub.Channel() |
| 85 | + for { |
| 86 | + select { |
| 87 | + case msg := <-ch: |
| 88 | + var notification mcp.JSONRPCNotification |
| 89 | + if err := json.Unmarshal([]byte(msg.Payload), ¬ification); err == nil { |
| 90 | + select { |
| 91 | + case localChan <- notification: |
| 92 | + // Notification forwarded successfully |
| 93 | + default: |
| 94 | + // Channel is full, might need handling |
| 95 | + } |
| 96 | + } |
| 97 | + case <-time.After(30 * time.Minute): |
| 98 | + // Timeout after inactivity |
| 99 | + return |
| 100 | + } |
| 101 | + } |
| 102 | + }() |
| 103 | + |
| 104 | + // Create a wrapper channel that publishes to Redis when written to |
| 105 | + wrappedChan := make(chan mcp.JSONRPCNotification, 100) |
| 106 | + go func() { |
| 107 | + ctx := context.Background() |
| 108 | + for notification := range wrappedChan { |
| 109 | + notificationData, err := json.Marshal(notification) |
| 110 | + if err != nil { |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + // Forward to local channel for this instance |
| 115 | + select { |
| 116 | + case localChan <- notification: |
| 117 | + // Also publish to Redis for other instances |
| 118 | + redisClient.Publish(ctx, redisKey, string(notificationData)) |
| 119 | + default: |
| 120 | + // Local channel is full, just publish to Redis |
| 121 | + redisClient.Publish(ctx, redisKey, string(notificationData)) |
| 122 | + } |
| 123 | + } |
| 124 | + close(localChan) // Close local channel when wrapped is closed |
| 125 | + }() |
| 126 | + |
| 127 | + return wrappedChan |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | +func main() { |
| 132 | + // Create MCP server |
| 133 | + mcpServer := server.NewMCPServer( |
| 134 | + "example-server", |
| 135 | + "1.0.0", |
| 136 | + server.WithResourceCapabilities(true, true), |
| 137 | + server.WithToolCapabilities(true), |
| 138 | + ) |
| 139 | + |
| 140 | + // Configure Redis client |
| 141 | + redisOpts := &redis.Options{ |
| 142 | + Addr: "localhost:6379", |
| 143 | + } |
| 144 | + redisClient := redis.NewClient(redisOpts) |
| 145 | + |
| 146 | + // Create SSE server with Redis-backed channels |
| 147 | + sseServer := server.NewSSEServer( |
| 148 | + mcpServer, |
| 149 | + server.WithBaseURL("https://api.example.com"), |
| 150 | + server.WithBasePath("/api"), |
| 151 | + NewRedisEventQueueBuilder(redisClient), |
| 152 | + NewRedisNotificationChannelBuilder(redisClient), |
| 153 | + ) |
| 154 | + |
| 155 | + // Start the server |
| 156 | + log.Println("Starting server on :8080") |
| 157 | + if err := sseServer.Start(":8080"); err != nil { |
| 158 | + log.Fatalf("Server failed: %v", err) |
| 159 | + } |
| 160 | +} |
0 commit comments