Skip to content

feat: Add Multi-Instance Support via Custom Channel Builders #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/redis_channels/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module github.com/mark3labs/mcp-go/examples/redis_channels

go 1.24.2

require (
github.com/mark3labs/mcp-go v0.22.0
github.com/redis/go-redis/v9 v9.7.3
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
)

replace github.com/mark3labs/mcp-go => ../../
34 changes: 34 additions & 0 deletions examples/redis_channels/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0wM=
github.com/redis/go-redis/v9 v9.7.3/go.mod h1:bGUrSggJ9X9GUmZpZNEOQKaANxSGgOEBRltRTZHSvrA=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
160 changes: 160 additions & 0 deletions examples/redis_channels/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"time"

"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
redis "github.com/redis/go-redis/v9"
)

// NewRedisEventQueueBuilder creates a builder that produces Redis-backed event queues
func NewRedisEventQueueBuilder(redisClient *redis.Client) server.SSEOption {
return server.WithEventQueueBuilder(func(sessionID string) chan string {
// Create a buffered channel for local use
localChan := make(chan string, 100)

// Key for this session's events in Redis
redisKey := fmt.Sprintf("sse:events:%s", sessionID)

// Start a goroutine to receive messages from Redis and forward to local channel
go func() {
ctx := context.Background()
pubsub := redisClient.Subscribe(ctx, redisKey)
defer pubsub.Close()

ch := pubsub.Channel()
for {
select {
case msg := <-ch:
select {
case localChan <- msg.Payload:
// Message forwarded successfully
default:
// Channel is full, might need handling
}
case <-time.After(30 * time.Minute):
// Timeout after inactivity
return
}
}
}()

// Create a wrapper channel that publishes to Redis when written to
wrappedChan := make(chan string, 100)
go func() {
ctx := context.Background()
for event := range wrappedChan {
// Forward to local channel for this instance
select {
case localChan <- event:
// Also publish to Redis for other instances
redisClient.Publish(ctx, redisKey, event)
default:
// Local channel is full, just publish to Redis
redisClient.Publish(ctx, redisKey, event)
}
}
close(localChan) // Close local channel when wrapped is closed
}()

return wrappedChan
})
}

// NewRedisNotificationChannelBuilder creates a builder that produces Redis-backed notification channels
func NewRedisNotificationChannelBuilder(redisClient *redis.Client) server.SSEOption {
return server.WithNotificationChannelBuilder(func(sessionID string) chan mcp.JSONRPCNotification {
// Create a buffered channel for local use
localChan := make(chan mcp.JSONRPCNotification, 100)

// Key for this session's notifications in Redis
redisKey := fmt.Sprintf("sse:notifications:%s", sessionID)

// Start a goroutine to receive notifications from Redis and forward to local channel
go func() {
ctx := context.Background()
pubsub := redisClient.Subscribe(ctx, redisKey)
defer pubsub.Close()

ch := pubsub.Channel()
for {
select {
case msg := <-ch:
var notification mcp.JSONRPCNotification
if err := json.Unmarshal([]byte(msg.Payload), &notification); err == nil {
select {
case localChan <- notification:
// Notification forwarded successfully
default:
// Channel is full, might need handling
}
}
case <-time.After(30 * time.Minute):
// Timeout after inactivity
return
}
}
}()

// Create a wrapper channel that publishes to Redis when written to
wrappedChan := make(chan mcp.JSONRPCNotification, 100)
go func() {
ctx := context.Background()
for notification := range wrappedChan {
notificationData, err := json.Marshal(notification)
if err != nil {
continue
}

// Forward to local channel for this instance
select {
case localChan <- notification:
// Also publish to Redis for other instances
redisClient.Publish(ctx, redisKey, string(notificationData))
default:
// Local channel is full, just publish to Redis
redisClient.Publish(ctx, redisKey, string(notificationData))
}
}
close(localChan) // Close local channel when wrapped is closed
}()

return wrappedChan
})
}

func main() {
// Create MCP server
mcpServer := server.NewMCPServer(
"example-server",
"1.0.0",
server.WithResourceCapabilities(true, true),
server.WithToolCapabilities(true),
)

// Configure Redis client
redisOpts := &redis.Options{
Addr: "localhost:6379",
}
redisClient := redis.NewClient(redisOpts)

// Create SSE server with Redis-backed channels
sseServer := server.NewSSEServer(
mcpServer,
server.WithBaseURL("https://api.example.com"),
server.WithBasePath("/api"),
NewRedisEventQueueBuilder(redisClient),
NewRedisNotificationChannelBuilder(redisClient),
)

// Start the server
log.Println("Starting server on :8080")
if err := sseServer.Start(":8080"); err != nil {
log.Fatalf("Server failed: %v", err)
}
}
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.