Skip to content

fix: SSE client hangs after 30 seconds #88

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

Merged
merged 1 commit into from
Apr 10, 2025
Merged
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
51 changes: 20 additions & 31 deletions client/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ import (
// while sending requests over regular HTTP POST calls. The client handles
// automatic reconnection and message routing between requests and responses.
type SSEMCPClient struct {
baseURL *url.URL
endpoint *url.URL
httpClient *http.Client
requestID atomic.Int64
responses map[int64]chan RPCResponse
mu sync.RWMutex
done chan struct{}
initialized bool
notifications []func(mcp.JSONRPCNotification)
notifyMu sync.RWMutex
endpointChan chan struct{}
capabilities mcp.ServerCapabilities
headers map[string]string
sseReadTimeout time.Duration
baseURL *url.URL
endpoint *url.URL
httpClient *http.Client
requestID atomic.Int64
responses map[int64]chan RPCResponse
mu sync.RWMutex
done chan struct{}
initialized bool
notifications []func(mcp.JSONRPCNotification)
notifyMu sync.RWMutex
endpointChan chan struct{}
capabilities mcp.ServerCapabilities
headers map[string]string
}

type ClientOption func(*SSEMCPClient)
Expand All @@ -47,12 +46,6 @@ func WithHeaders(headers map[string]string) ClientOption {
}
}

func WithSSEReadTimeout(timeout time.Duration) ClientOption {
return func(sc *SSEMCPClient) {
sc.sseReadTimeout = timeout
}
}

// NewSSEMCPClient creates a new SSE-based MCP client with the given base URL.
// Returns an error if the URL is invalid.
func NewSSEMCPClient(baseURL string, options ...ClientOption) (*SSEMCPClient, error) {
Expand All @@ -62,13 +55,12 @@ func NewSSEMCPClient(baseURL string, options ...ClientOption) (*SSEMCPClient, er
}

smc := &SSEMCPClient{
baseURL: parsedURL,
httpClient: &http.Client{},
responses: make(map[int64]chan RPCResponse),
done: make(chan struct{}),
endpointChan: make(chan struct{}),
sseReadTimeout: 30 * time.Second,
headers: make(map[string]string),
baseURL: parsedURL,
httpClient: &http.Client{},
responses: make(map[int64]chan RPCResponse),
done: make(chan struct{}),
endpointChan: make(chan struct{}),
headers: make(map[string]string),
}

for _, opt := range options {
Expand Down Expand Up @@ -128,12 +120,9 @@ func (c *SSEMCPClient) readSSE(reader io.ReadCloser) {
br := bufio.NewReader(reader)
var event, data string

ctx, cancel := context.WithTimeout(context.Background(), c.sseReadTimeout)
defer cancel()

for {
select {
case <-ctx.Done():
case <-c.done:
return
default:
line, err := br.ReadString('\n')
Expand Down