Files
archie/internal/tools/mcp/transport_sse.go
T

582 lines
16 KiB
Go

package mcp
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
"time"
)
// SSETransportConfig configures the SSE MCP transport.
type SSETransportConfig struct {
// SSEEndpoint is the GET URL for the server→client event stream (e.g.
// "https://mcp.example.com/sse"). Required.
SSEEndpoint string
// MessageEndpoint is the POST URL for client messages. Empty discovers it
// from the server's "endpoint" event.
MessageEndpoint string
// Headers are additional HTTP headers sent with every request.
Headers map[string]string
// ReconnectBackoff is the initial delay before the first reconnect
// after the SSE stream drops. Default: 1s.
ReconnectBackoff time.Duration
// MaxReconnectBackoff caps the exponential backoff delay. Default: 30s.
MaxReconnectBackoff time.Duration
// POSTTimeout is the timeout for each POST request to the message
// endpoint. Default: 30s.
POSTTimeout time.Duration
}
func (c SSETransportConfig) effectiveReconnectBackoff() time.Duration {
if c.ReconnectBackoff <= 0 {
return time.Second
}
return c.ReconnectBackoff
}
func (c SSETransportConfig) effectiveMaxReconnectBackoff() time.Duration {
if c.MaxReconnectBackoff <= 0 {
return 30 * time.Second
}
return c.MaxReconnectBackoff
}
func (c SSETransportConfig) effectivePOSTTimeout() time.Duration {
if c.POSTTimeout <= 0 {
return 30 * time.Second
}
return c.POSTTimeout
}
// SSETransport implements [Transport] over MCP SSE. It maintains a
// long-lived GET connection for server→client events and sends
// client→server messages via HTTP POST. Responses to [Send] are
// correlated with the matching SSE "message" event by JSON-RPC ID.
type SSETransport struct {
config SSETransportConfig
mu sync.Mutex
state TransportState
// messageEndpoint is the actual POST URL the server told us to use,
// discovered from the endpoint event (or the config fallback).
messageEndpoint string
pending map[string]chan []byte
// stopCh is closed by Stop(). Background goroutines select on this.
stopCh chan struct{}
// lifecycleCancel cancels the lifecycle context handed to the background
// reader and reconnect goroutines. Stop calls it so an in-flight reconnect
// HTTP request can be interrupted immediately instead of blocking Stop
// until the OS-level connect/read gives up.
lifecycleCancel context.CancelFunc
// The SSE reader goroutine.
readerWg sync.WaitGroup
// Track HTTP response body so Stop can close it.
respBody io.Closer
// serverRequestHandler answers requests the server sends to this client
// (sampling/createMessage). Nil until a Client registers itself.
serverRequestHandler ServerRequestHandler
// serverRequestWg tracks in-flight handler goroutines so Stop waits for
// their responses to be posted before the connection is closed. The
// handler context is the SSE lifecycle context, carried through the
// reader call chain and cancelled by Stop.
serverRequestWg sync.WaitGroup
}
// SetServerRequestHandler registers the handler for server-initiated
// requests. It implements serverRequestRouter.
func (t *SSETransport) SetServerRequestHandler(handler ServerRequestHandler) {
t.mu.Lock()
defer t.mu.Unlock()
t.serverRequestHandler = handler
}
// NewSSETransport creates a new SSE transport with the given config.
// Callers must call [*SSETransport.Start] before sending messages.
func NewSSETransport(config SSETransportConfig) *SSETransport {
return &SSETransport{
config: config,
state: StateStopped,
pending: map[string]chan []byte{},
}
}
// Start establishes the SSE connection and waits for the server's endpoint
// event so the caller knows the transport is ready.
func (t *SSETransport) Start(ctx context.Context) error {
t.mu.Lock()
if t.state == StateRunning {
t.mu.Unlock()
return fmt.Errorf("mcp sse: already running")
}
t.state = StateStarting
t.stopCh = make(chan struct{})
lifecycleCtx, cancel := context.WithCancel(ctx)
t.lifecycleCancel = cancel
t.messageEndpoint = t.config.MessageEndpoint // fallback; endpoint event overrides
t.mu.Unlock()
return t.connect(lifecycleCtx)
}
// Stop closes the SSE connection, fails pending requests, and transitions
// to StateStopped. Calling Stop on a stopped transport is a no-op.
func (t *SSETransport) Stop(_ context.Context) error {
t.mu.Lock()
if t.state == StateStopped {
t.mu.Unlock()
return nil
}
t.state = StateStopping
if t.stopCh != nil {
select {
case <-t.stopCh:
default:
close(t.stopCh)
}
}
if t.lifecycleCancel != nil {
t.lifecycleCancel()
}
if t.respBody != nil {
_ = t.respBody.Close()
t.respBody = nil
}
t.mu.Unlock()
t.readerWg.Wait()
// The reader is done, so no further handler goroutines can be added;
// wait for the responses of any already dispatched.
t.serverRequestWg.Wait()
t.mu.Lock()
for id, ch := range t.pending {
close(ch)
delete(t.pending, id)
}
t.state = StateStopped
t.mu.Unlock()
return nil
}
// Send posts a JSON-RPC request to the message endpoint and waits for the
// matching response to arrive through the SSE event stream, correlated by
// JSON-RPC message ID.
func (t *SSETransport) Send(ctx context.Context, body []byte) ([]byte, error) {
msgID, err := extractMessageID(body)
if err != nil {
return nil, fmt.Errorf("mcp sse: extract id: %w", err)
}
t.mu.Lock()
if t.state != StateRunning {
t.mu.Unlock()
return nil, fmt.Errorf("mcp sse: transport is not running")
}
ch := make(chan []byte, 1)
t.pending[msgID] = ch
t.mu.Unlock()
defer func() {
t.mu.Lock()
delete(t.pending, msgID)
t.mu.Unlock()
}()
if err := t.postJSON(ctx, body); err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case resp, ok := <-ch:
if !ok {
return nil, fmt.Errorf("mcp sse: transport closed while waiting for response")
}
return resp, nil
}
}
// Notify posts a JSON-RPC notification to the message endpoint without
// waiting for a response (notifications have no "id" by definition).
func (t *SSETransport) Notify(ctx context.Context, body []byte) error {
t.mu.Lock()
if t.state != StateRunning {
t.mu.Unlock()
return fmt.Errorf("mcp sse: transport is not running")
}
t.mu.Unlock()
return t.postJSON(ctx, body)
}
// ── Internal connection management ───────────────────────────────────────
// connect establishes the SSE GET connection and blocks until the
// endpoint event arrives (or the context is cancelled).
func (t *SSETransport) connect(ctx context.Context) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, t.config.SSEEndpoint, nil)
if err != nil {
_ = t.setError(fmt.Errorf("mcp sse: build GET request: %w", err))
return err
}
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Cache-Control", "no-cache")
for k, v := range t.config.Headers {
req.Header.Set(k, v)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
_ = t.setError(fmt.Errorf("mcp sse: connect: %w", err))
return err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
_ = resp.Body.Close()
err := fmt.Errorf("mcp sse: server returned %d", resp.StatusCode)
_ = t.setError(err)
return err
}
t.mu.Lock()
t.respBody = resp.Body
t.mu.Unlock()
// Read the endpoint event (the first event the server sends).
reader := bufio.NewReader(resp.Body)
msgURL, err := readEndpointEvent(reader)
if err != nil {
_ = resp.Body.Close()
_ = t.setError(fmt.Errorf("mcp sse: read endpoint event: %w", err))
return err
}
if msgURL != "" {
t.mu.Lock()
t.messageEndpoint = msgURL
t.mu.Unlock()
}
t.mu.Lock()
t.state = StateRunning
t.mu.Unlock()
// Start the background SSE event reader.
t.readerWg.Add(1)
go t.runReader(ctx, resp.Body, reader)
return nil
}
// runReader loops reading SSE events from the stream, routing
// "message" events to the corresponding pending channel.
func (t *SSETransport) runReader(ctx context.Context, body io.Closer, reader *bufio.Reader) {
defer t.readerWg.Done()
defer func() { _ = body.Close() }()
for {
event, data, err := readSSEEvent(reader)
if err != nil {
// Stream closed or errored. Attempt reconnect.
t.mu.Lock()
cancelled := t.isStopped()
stopping := t.state == StateStopping || t.state == StateStopped
t.mu.Unlock()
if cancelled || stopping {
return
}
t.reconnect(ctx)
return
}
switch event {
case "message":
t.deliverMessage(ctx, data)
case "endpoint":
// Server sent a new endpoint URL (session refresh).
// Already handled on connect; accept mid-stream updates too.
t.mu.Lock()
if data != "" {
t.messageEndpoint = data
}
t.mu.Unlock()
default:
// ping, heartbeat, etc. — ignore.
}
}
}
// reconnect attempts to re-establish the SSE connection with exponential
// backoff. On success, the reader goroutine restarts.
func (t *SSETransport) reconnect(ctx context.Context) {
backoff := t.config.effectiveReconnectBackoff()
for {
t.mu.Lock()
if t.isStopped() {
t.state = StateStopped
t.mu.Unlock()
return
}
if t.state != StateRunning {
t.mu.Unlock()
return
}
t.mu.Unlock()
timer := time.NewTimer(backoff)
select {
case <-t.stopCh:
timer.Stop()
return
case <-timer.C:
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, t.config.SSEEndpoint, nil)
if err != nil {
backoff = nextBackoff(backoff, t.config.effectiveMaxReconnectBackoff())
continue
}
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Cache-Control", "no-cache")
resp, err := http.DefaultClient.Do(req)
if err != nil {
backoff = nextBackoff(backoff, t.config.effectiveMaxReconnectBackoff())
continue
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
_ = resp.Body.Close()
backoff = nextBackoff(backoff, t.config.effectiveMaxReconnectBackoff())
continue
}
reader := bufio.NewReader(resp.Body)
msgURL, err := readEndpointEvent(reader)
if err != nil {
_ = resp.Body.Close()
backoff = nextBackoff(backoff, t.config.effectiveMaxReconnectBackoff())
continue
}
// Guarded commit: Stop may have won while the reconnect GET was in
// flight. Do not install a reader (or a response body Stop has already
// missed) once Stop has closed stopCh.
t.mu.Lock()
if t.isStopped() || t.state != StateRunning {
t.state = StateStopped
t.mu.Unlock()
_ = resp.Body.Close()
return
}
if msgURL != "" {
t.messageEndpoint = msgURL
}
t.respBody = resp.Body
t.readerWg.Add(1)
t.mu.Unlock()
go t.runReader(ctx, resp.Body, reader)
return
}
}
func nextBackoff(d, ceiling time.Duration) time.Duration {
d *= 2
if d > ceiling {
return ceiling
}
return d
}
// ── Delivery ─────────────────────────────────────────────────────────────
// deliverMessage routes an incoming SSE message event. A response goes to
// the caller waiting on its id; a server-initiated request goes to the
// registered handler; a notification has no response to route.
func (t *SSETransport) deliverMessage(ctx context.Context, data string) {
var msg Message
if err := json.Unmarshal([]byte(data), &msg); err != nil {
return
}
if msg.IsRequest() {
t.dispatchServerRequest(ctx, msg)
return
}
if len(msg.ID) == 0 {
return
}
msgID := string(msg.ID)
t.mu.Lock()
ch, ok := t.pending[msgID]
if ok {
delete(t.pending, msgID)
}
t.mu.Unlock()
if ok {
select {
case ch <- []byte(data):
default:
}
}
}
// dispatchServerRequest runs the registered handler for a server-initiated
// request without blocking the reader, and posts its response back to the
// message endpoint.
func (t *SSETransport) dispatchServerRequest(ctx context.Context, msg Message) {
t.mu.Lock()
handler := t.serverRequestHandler
t.mu.Unlock()
t.serverRequestWg.Go(func() {
var (
result json.RawMessage
rpcErr *ErrorData
)
if handler == nil {
rpcErr = &ErrorData{Code: ErrCodeMethodNotFound, Message: "method not found: " + msg.Method}
} else {
result, rpcErr = handler(ctx, msg.Method, msg.Params)
}
body, err := json.Marshal(Message{JSONRPC: "2.0", ID: msg.ID, Result: result, Error: rpcErr})
if err != nil {
return
}
// Unreportable here: the server's request has no other answer path,
// and a failed POST is a connection the reader is already
// reconnecting.
_ = t.postJSON(ctx, body)
})
}
// ── POST ─────────────────────────────────────────────────────────────────
// postJSON sends a JSON-RPC body to the message endpoint via POST.
func (t *SSETransport) postJSON(ctx context.Context, body []byte) error {
t.mu.Lock()
endpoint := t.messageEndpoint
t.mu.Unlock()
if endpoint == "" {
return fmt.Errorf("mcp sse: no message endpoint (awaiting server's endpoint event)")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("mcp sse: build POST: %w", err)
}
req.Header.Set("Content-Type", "application/json")
for k, v := range t.config.Headers {
req.Header.Set(k, v)
}
client := &http.Client{Timeout: t.config.effectivePOSTTimeout()}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("mcp sse: post: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
msg, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
return fmt.Errorf("mcp sse: HTTP %d: %s", resp.StatusCode, string(msg))
}
return nil
}
// ── State helpers ────────────────────────────────────────────────────────
func (t *SSETransport) setError(err error) error {
t.mu.Lock()
t.state = StateError
t.mu.Unlock()
return err
}
// ── SSE parsing ──────────────────────────────────────────────────────────
// readEndpointEvent reads the mandatory first event from an SSE stream.
// Per the MCP SSE spec, the server MUST send an "endpoint" event as its
// first message, whose data payload is the URL the client should POST to.
func readEndpointEvent(r *bufio.Reader) (string, error) {
event, data, err := readSSEEvent(r)
if err != nil {
return "", err
}
if event != "endpoint" {
return "", fmt.Errorf("expected endpoint event, got %q", event)
}
return strings.TrimSpace(data), nil
}
// readSSEEvent reads one SSE event and returns its type and data. Comment
// lines are skipped; multiple data lines are joined with newlines.
func readSSEEvent(r *bufio.Reader) (event, data string, err error) {
var dataLines []string
for {
line, err := r.ReadString('\n')
if err != nil {
return "", "", err
}
line = strings.TrimRight(line, "\r\n")
if line == "" {
// Empty line terminates the event.
if event == "" {
event = "message" // default event type per SSE spec
}
return event, strings.Join(dataLines, "\n"), nil
}
if strings.HasPrefix(line, ":") {
continue // comment
}
if after, ok := strings.CutPrefix(line, "event: "); ok {
event = after
continue
}
if after, ok := strings.CutPrefix(line, "event:"); ok {
event = strings.TrimSpace(after)
continue
}
if after, ok := strings.CutPrefix(line, "data: "); ok {
dataLines = append(dataLines, after)
continue
}
if after, ok := strings.CutPrefix(line, "data:"); ok {
dataLines = append(dataLines, strings.TrimSpace(after))
continue
}
// Other fields (id:, retry:) are ignored.
}
}
// isStopped returns true if the transport stop channel has been closed.
func (t *SSETransport) isStopped() bool {
select {
case <-t.stopCh:
return true
default:
return false
}
}
// State returns the current transport state.
func (t *SSETransport) State() TransportState {
t.mu.Lock()
defer t.mu.Unlock()
return t.state
}
// Compile-time interface check.
var _ Transport = (*SSETransport)(nil)