@@ -28,9 +28,12 @@ type StdioBridge struct {
28
28
}
29
29
30
30
// 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
34
37
}
35
38
36
39
// Start begins the transport loop for the StdioBridge.
@@ -55,7 +58,8 @@ func (b *StdioBridge) loop(ctx context.Context) {
55
58
go b .runStreamableWriter (ctx )
56
59
} else { // legacy SSE
57
60
b .postURL = b .baseURL // reset
58
- b .wg .Add (2 - 0 )
61
+ const legacyWorkerCount = 2
62
+ b .wg .Add (legacyWorkerCount )
59
63
go b .runLegacyReader (ctx )
60
64
go b .runLegacyWriter (ctx )
61
65
}
@@ -72,8 +76,16 @@ func (b *StdioBridge) loop(ctx context.Context) {
72
76
// Header include Mcp-Session-Id if needed
73
77
func (b * StdioBridge ) detectTransport (ctx context.Context ) (string , error ) {
74
78
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
+ }
77
89
req .Header .Set ("Content-Type" , "application/json" )
78
90
req .Header .Set ("Accept" , "application/json, text/event-stream" )
79
91
copyHeaders (req .Header , b .headers )
@@ -97,7 +109,11 @@ func (b *StdioBridge) handleInitializeResponse(resp *http.Response) {
97
109
b .headers .Set ("Mcp-Session-Id" , sid )
98
110
logger .Infof ("Streamable HTTP session ID: %s" , sid )
99
111
}
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
+ }
101
117
payload := strings .TrimSpace (string (data ))
102
118
ct := resp .Header .Get ("Content-Type" )
103
119
if strings .HasPrefix (ct , "application/json" ) {
@@ -153,8 +169,12 @@ func (b *StdioBridge) runStreamableWriter(ctx context.Context) {
153
169
continue
154
170
}
155
171
ct := resp .Header .Get ("Content-Type" )
156
- data , _ := io .ReadAll (resp .Body )
172
+ data , err := io .ReadAll (resp .Body )
157
173
_ = resp .Body .Close ()
174
+ if err != nil {
175
+ logger .Errorf ("Error reading response body: %v" , err )
176
+ continue
177
+ }
158
178
payload := strings .TrimSpace (string (data ))
159
179
if resp .StatusCode == http .StatusNotFound {
160
180
b .cancel ()
@@ -170,7 +190,11 @@ func (b *StdioBridge) runStreamableWriter(ctx context.Context) {
170
190
171
191
func (b * StdioBridge ) runLegacyReader (ctx context.Context ) {
172
192
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
+ }
174
198
req .Header .Set ("Accept" , "text/event-stream" )
175
199
copyHeaders (req .Header , b .headers )
176
200
resp , err := http .DefaultClient .Do (req )
@@ -216,7 +240,11 @@ func (b *StdioBridge) runLegacyWriter(ctx context.Context) {
216
240
logger .Errorf ("Invalid JSON input: %v" , err )
217
241
continue
218
242
}
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
+ }
220
248
req .Header .Set ("Content-Type" , "application/json" )
221
249
req .Header .Set ("Accept" , "application/json, text/event-stream" )
222
250
copyHeaders (req .Header , b .headers )
@@ -248,10 +276,18 @@ func (b *StdioBridge) updatePostURL(path string) {
248
276
}
249
277
250
278
func (b * StdioBridge ) sendInitialize (ctx context.Context ) {
251
- reqBody , _ := json .Marshal (map [string ]interface {}{
279
+ reqBody , err := json .Marshal (map [string ]interface {}{
252
280
"jsonrpc" : "2.0" , "id" : 1 , "method" : "initialize" , "params" : map [string ]interface {}{},
253
281
})
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
+ }
255
291
req .Header .Set ("Content-Type" , "application/json" )
256
292
req .Header .Set ("Accept" , "application/json, text/event-stream" )
257
293
copyHeaders (req .Header , b .headers )
0 commit comments