Skip to content

Commit a5b56c5

Browse files
committed
fix: io.ReadAll errors, json.Marshal errors, bedrock host extraction, mcp marshal, openapi path params, context bounds
1 parent 1325449 commit a5b56c5

6 files changed

Lines changed: 28 additions & 12 deletions

File tree

azure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (p *AzureOpenAIProvider) Stream(ctx context.Context, config StreamConfig, m
201201
defer resp.Body.Close()
202202

203203
if resp.StatusCode != http.StatusOK {
204-
respBody, _ := io.ReadAll(resp.Body)
204+
respBody, _ := io.ReadAll(resp.Body) // best-effort for error message
205205
return Message{}, fmt.Errorf("Azure OpenAI error (%d): %s", resp.StatusCode, string(respBody))
206206
}
207207

bedrock.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ func (p *BedrockProvider) signRequest(req *http.Request, payload string) {
235235

236236
host := req.Host
237237
if host == "" {
238-
host = strings.Split(strings.Split(req.URL.Host, ":")[0], ".")[0]
238+
// Strip port if present, use full hostname (not just first segment).
239+
host = req.URL.Hostname()
239240
}
240241

241242
hashedPayload := fmt.Sprintf("%x", sha256.Sum256([]byte(payload)))

context.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,12 @@ func CompactMessagesTiered(messages []Message, cfg ContextConfig) []Message {
310310
var summary string
311311
if len(calls) > 0 {
312312
summary = fmt.Sprintf("[Assistant used %d tool(s)]", len(calls))
313-
} else if len(msg.Content) > 200 {
314-
summary = msg.Content[:200]
315313
} else {
316-
summary = msg.Content
314+
if len(msg.Content) > 200 {
315+
summary = msg.Content[:200]
316+
} else {
317+
summary = msg.Content
318+
}
317319
}
318320
compacted = append(compacted, Message{Role: "assistant", Content: summary})
319321
// Skip adjacent tool result messages.

mcp/helpers.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,24 @@ func ConnectHTTP(ctx context.Context, url string, headers map[string]string) (*M
4141

4242
// initialize performs the MCP initialize handshake.
4343
func (c *McpClient) initialize(ctx context.Context) error {
44-
params, _ := json.Marshal(map[string]interface{}{
44+
params, err := json.Marshal(map[string]interface{}{
4545
"protocolVersion": "2024-11-05",
4646
"capabilities": map[string]interface{}{},
4747
"clientInfo": map[string]interface{}{
4848
"name": "iteragent",
4949
"version": "0.1.0",
5050
},
5151
})
52+
if err != nil {
53+
return fmt.Errorf("marshal initialize params: %w", err)
54+
}
5255
req := JsonRpcRequest{
5356
JSONRPC: "2.0",
5457
Method: "initialize",
5558
Params: params,
5659
}
57-
_, err := c.Transport.Send(ctx, req)
58-
return err
60+
_, sendErr := c.Transport.Send(ctx, req)
61+
return sendErr
5962
}
6063

6164
// ListTools returns the list of tools advertised by the MCP server.
@@ -82,10 +85,13 @@ func (c *McpClient) ListTools(ctx context.Context) ([]Tool, error) {
8285

8386
// CallTool invokes a tool by name with the given arguments.
8487
func (c *McpClient) CallTool(ctx context.Context, name string, args map[string]interface{}) (*CallToolResult, error) {
85-
paramsRaw, _ := json.Marshal(map[string]interface{}{
88+
paramsRaw, err := json.Marshal(map[string]interface{}{
8689
"name": name,
8790
"arguments": args,
8891
})
92+
if err != nil {
93+
return nil, fmt.Errorf("marshal call params: %w", err)
94+
}
8995
req := JsonRpcRequest{
9096
JSONRPC: "2.0",
9197
Method: "tools/call",

openapi/adapter.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ func (a *Adapter) callOperation(ctx context.Context, path, method string, op *Op
371371
case "query":
372372
queryParams = append(queryParams, fmt.Sprintf("%s=%s", url.QueryEscape(param.Name), url.QueryEscape(val)))
373373
case "path":
374-
u = strings.ReplaceAll(u, "{"+param.Name+"}", val)
374+
// Use exact token replacement to avoid partial matches (e.g. {id} vs {id_extra}).
375+
u = strings.ReplaceAll(u, "{"+param.Name+"}", url.PathEscape(val))
375376
}
376377
}
377378

@@ -381,7 +382,10 @@ func (a *Adapter) callOperation(ctx context.Context, path, method string, op *Op
381382

382383
var bodyReader io.Reader
383384
if op.RequestBody != nil {
384-
bodyData, _ := json.Marshal(args)
385+
bodyData, err := json.Marshal(args)
386+
if err != nil {
387+
return "", fmt.Errorf("marshal request body: %w", err)
388+
}
385389
bodyReader = strings.NewReader(string(bodyData))
386390
} else {
387391
bodyReader = strings.NewReader("")

vertex.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ func (p *VertexProvider) Complete(ctx context.Context, messages []Message, opts
131131
}
132132
defer resp.Body.Close()
133133

134-
respBody, _ := io.ReadAll(resp.Body)
134+
respBody, err := io.ReadAll(resp.Body)
135+
if err != nil {
136+
return "", fmt.Errorf("read response body: %w", err)
137+
}
135138

136139
if resp.StatusCode != http.StatusOK {
137140
return "", fmt.Errorf("Vertex AI error (%d): %s", resp.StatusCode, string(respBody))

0 commit comments

Comments
 (0)