Skip to content

feat(SSEServer): add WithAppendQueryToMessageEndpoint() #136

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion server/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type SSEServer struct {
keepAliveInterval time.Duration

mu sync.RWMutex

appendQueryToMessageEndpoint bool
}

// SSEOption defines a function type for configuring SSEServer
Expand Down Expand Up @@ -114,6 +116,17 @@ func WithMessageEndpoint(endpoint string) SSEOption {
}
}

// WithAppendQueryToMessageEndpoint configures the SSE server to append the original request's
// query parameters to the message endpoint URL that is sent to clients during the SSE connection
// initialization. This is useful when you need to preserve query parameters from the initial
// SSE connection request and carry them over to subsequent message requests, maintaining
// context or authentication details across the communication channel.
func WithAppendQueryToMessageEndpoint() SSEOption {
return func(s *SSEServer) {
s.appendQueryToMessageEndpoint = true
}
}

// WithUseFullURLForMessageEndpoint controls whether the SSE server returns a complete URL (including baseURL)
// or just the path portion for the message endpoint. Set to false when clients will concatenate
// the baseURL themselves to avoid malformed URLs like "http://localhost/mcphttp://localhost/mcp/message".
Expand Down Expand Up @@ -308,7 +321,11 @@ func (s *SSEServer) handleSSE(w http.ResponseWriter, r *http.Request) {
}

// Send the initial endpoint event
fmt.Fprintf(w, "event: endpoint\ndata: %s\r\n\r\n", s.GetMessageEndpointForClient(sessionID))
endpoint := s.GetMessageEndpointForClient(sessionID)
if s.appendQueryToMessageEndpoint && len(r.URL.RawQuery) > 0 {
endpoint += "&" + r.URL.RawQuery
}
fmt.Fprintf(w, "event: endpoint\ndata: %s\r\n\r\n", endpoint)
flusher.Flush()

// Main event loop - this runs in the HTTP handler goroutine
Expand Down