-
Notifications
You must be signed in to change notification settings - Fork 469
feat: add WithHTTPClient option for SSEMCPClient configuration #133
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -46,6 +46,18 @@ func WithHeaders(headers map[string]string) ClientOption { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func WithSSEReadTimeout(timeout time.Duration) ClientOption { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return func(sc *SSEMCPClient) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sc.sseReadTimeout = timeout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func WithHTTPClient(client *http.Client) ClientOption { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return func(sc *SSEMCPClient) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sc.httpClient = client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+49
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider implementing the read timeout functionality After adding the Here's a suggested implementation for using the timeout in the func (c *SSEMCPClient) readSSE(reader io.ReadCloser) {
defer reader.Close()
br := bufio.NewReader(reader)
var event, data string
for {
select {
case <-c.done:
return
default:
+ // Set read deadline if timeout is specified
+ if c.sseReadTimeout > 0 {
+ if deadline, ok := reader.(interface{ SetReadDeadline(time.Time) error }); ok {
+ _ = deadline.SetReadDeadline(time.Now().Add(c.sseReadTimeout))
+ }
+ }
line, err := br.ReadString('\n')
if err != nil {
// ...existing error handling...
}
// ...rest of the function...
}
}
} Note that this implementation handles the case where the reader supports setting deadlines via the 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 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) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
sseReadTimeout
field inSSEMCPClient
structThe
WithSSEReadTimeout
function attempts to setsc.sseReadTimeout
, but there's no corresponding field in theSSEMCPClient
struct (lines 25-39). This will cause a compilation error.You need to add the field to the struct definition:
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 }
Additionally, the
sseReadTimeout
field is never used in the implementation. You should add timeout handling in thereadSSE
function to actually use this configuration option.📝 Committable suggestion