Skip to content

Commit efc98fe

Browse files
yroblaCopilot
authored andcommitted
Update pkg/transport/bridge.go
Co-authored-by: Copilot <[email protected]> Update pkg/transport/bridge.go Co-authored-by: Copilot <[email protected]> Update pkg/transport/bridge.go Co-authored-by: Copilot <[email protected]> Update pkg/transport/bridge.go Co-authored-by: Copilot <[email protected]> Update pkg/transport/bridge.go Co-authored-by: Copilot <[email protected]> Update pkg/transport/bridge.go Co-authored-by: Copilot <[email protected]> Update pkg/transport/bridge.go Co-authored-by: Copilot <[email protected]> Update pkg/transport/bridge.go Co-authored-by: Copilot <[email protected]> Update pkg/transport/bridge.go Co-authored-by: Copilot <[email protected]> Update pkg/transport/bridge.go Co-authored-by: Copilot <[email protected]>
1 parent 750d71d commit efc98fe

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

pkg/transport/bridge.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ type StdioBridge struct {
2828
}
2929

3030
// NewStdioBridge creates a new StdioBridge instance.
31-
func NewStdioBridge(rawURL string) *StdioBridge {
32-
u, _ := url.Parse(rawURL)
33-
return &StdioBridge{baseURL: u, postURL: u}
31+
func NewStdioBridge(rawURL string) (*StdioBridge, error) {
32+
u, err := url.Parse(rawURL)
33+
if err != nil {
34+
return nil, fmt.Errorf("invalid URL: %w", err)
35+
}
36+
return &StdioBridge{baseURL: u, postURL: u}, nil
3437
}
3538

3639
// Start begins the transport loop for the StdioBridge.
@@ -55,7 +58,8 @@ func (b *StdioBridge) loop(ctx context.Context) {
5558
go b.runStreamableWriter(ctx)
5659
} else { // legacy SSE
5760
b.postURL = b.baseURL // reset
58-
b.wg.Add(2 - 0)
61+
const legacyWorkerCount = 2
62+
b.wg.Add(legacyWorkerCount)
5963
go b.runLegacyReader(ctx)
6064
go b.runLegacyWriter(ctx)
6165
}
@@ -72,8 +76,16 @@ func (b *StdioBridge) loop(ctx context.Context) {
7276
// Header include Mcp-Session-Id if needed
7377
func (b *StdioBridge) detectTransport(ctx context.Context) (string, error) {
7478
initReq := map[string]interface{}{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": map[string]interface{}{}}
75-
body, _ := json.Marshal(initReq)
76-
req, _ := http.NewRequestWithContext(ctx, "POST", b.baseURL.String(), bytes.NewReader(body))
79+
body, err := json.Marshal(initReq)
80+
if err != nil {
81+
logger.Errorf("failed to marshal initialization request: %v", err)
82+
return "", err
83+
}
84+
req, err := http.NewRequestWithContext(ctx, "POST", b.baseURL.String(), bytes.NewReader(body))
85+
if err != nil {
86+
logger.Errorf("failed to create HTTP request: %v", err)
87+
return "", err
88+
}
7789
req.Header.Set("Content-Type", "application/json")
7890
req.Header.Set("Accept", "application/json, text/event-stream")
7991
copyHeaders(req.Header, b.headers)
@@ -97,7 +109,11 @@ func (b *StdioBridge) handleInitializeResponse(resp *http.Response) {
97109
b.headers.Set("Mcp-Session-Id", sid)
98110
logger.Infof("Streamable HTTP session ID: %s", sid)
99111
}
100-
data, _ := io.ReadAll(resp.Body)
112+
data, err := io.ReadAll(resp.Body)
113+
if err != nil {
114+
logger.Errorf("failed to read response body: %v", err)
115+
return
116+
}
101117
payload := strings.TrimSpace(string(data))
102118
ct := resp.Header.Get("Content-Type")
103119
if strings.HasPrefix(ct, "application/json") {
@@ -153,8 +169,12 @@ func (b *StdioBridge) runStreamableWriter(ctx context.Context) {
153169
continue
154170
}
155171
ct := resp.Header.Get("Content-Type")
156-
data, _ := io.ReadAll(resp.Body)
172+
data, err := io.ReadAll(resp.Body)
157173
_ = resp.Body.Close()
174+
if err != nil {
175+
logger.Errorf("Error reading response body: %v", err)
176+
continue
177+
}
158178
payload := strings.TrimSpace(string(data))
159179
if resp.StatusCode == http.StatusNotFound {
160180
b.cancel()
@@ -170,7 +190,11 @@ func (b *StdioBridge) runStreamableWriter(ctx context.Context) {
170190

171191
func (b *StdioBridge) runLegacyReader(ctx context.Context) {
172192
defer b.wg.Done()
173-
req, _ := http.NewRequestWithContext(ctx, "GET", b.baseURL.String(), nil)
193+
req, err := http.NewRequestWithContext(ctx, "GET", b.baseURL.String(), nil)
194+
if err != nil {
195+
logger.Errorf("Failed to create GET request: %v", err)
196+
return
197+
}
174198
req.Header.Set("Accept", "text/event-stream")
175199
copyHeaders(req.Header, b.headers)
176200
resp, err := http.DefaultClient.Do(req)
@@ -216,7 +240,11 @@ func (b *StdioBridge) runLegacyWriter(ctx context.Context) {
216240
logger.Errorf("Invalid JSON input: %v", err)
217241
continue
218242
}
219-
req, _ := http.NewRequestWithContext(ctx, "POST", b.postURL.String(), strings.NewReader(raw))
243+
req, err := http.NewRequestWithContext(ctx, "POST", b.postURL.String(), strings.NewReader(raw))
244+
if err != nil {
245+
logger.Errorf("Failed to create HTTP request: %v", err)
246+
continue
247+
}
220248
req.Header.Set("Content-Type", "application/json")
221249
req.Header.Set("Accept", "application/json, text/event-stream")
222250
copyHeaders(req.Header, b.headers)
@@ -248,10 +276,18 @@ func (b *StdioBridge) updatePostURL(path string) {
248276
}
249277

250278
func (b *StdioBridge) sendInitialize(ctx context.Context) {
251-
reqBody, _ := json.Marshal(map[string]interface{}{
279+
reqBody, err := json.Marshal(map[string]interface{}{
252280
"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": map[string]interface{}{},
253281
})
254-
req, _ := http.NewRequestWithContext(ctx, "POST", b.postURL.String(), bytes.NewReader(reqBody))
282+
if err != nil {
283+
logger.Errorf("Failed to marshal initialize request body: %v", err)
284+
return
285+
}
286+
req, err := http.NewRequestWithContext(ctx, "POST", b.postURL.String(), bytes.NewReader(reqBody))
287+
if err != nil {
288+
logger.Errorf("Failed to create HTTP request: %v", err)
289+
return
290+
}
255291
req.Header.Set("Content-Type", "application/json")
256292
req.Header.Set("Accept", "application/json, text/event-stream")
257293
copyHeaders(req.Header, b.headers)

0 commit comments

Comments
 (0)