11package aibridge
22
33import (
4- "encoding/json"
54 "fmt"
65 "net/http"
76 "time"
@@ -10,6 +9,7 @@ import (
109 "github.com/anthropics/anthropic-sdk-go/option"
1110 "github.com/google/uuid"
1211 mcplib "github.com/mark3labs/mcp-go/mcp" // TODO: abstract this away so callers need no knowledge of underlying lib.
12+ "github.com/tidwall/sjson"
1313
1414 "github.com/coder/aibridge/mcp"
1515
@@ -268,20 +268,21 @@ func (i *AnthropicMessagesBlockingInterception) ProcessRequest(w http.ResponseWr
268268 return nil
269269 }
270270
271- resp .Usage = cumulativeUsage
272-
273271 // Overwrite response identifier since proxy obscures injected tool call invocations.
274- resp .ID = i .ID ().String ()
272+ sj , err := sjson .Set (resp .RawJSON (), "id" , i .ID ().String ())
273+ if err != nil {
274+ return fmt .Errorf ("marshal response id failed: %w" , err )
275+ }
275276
276- out , err := json .Marshal (resp )
277+ // Overwrite the response's usage with the cumulative usage across any inner loops which invokes injected MCP tools.
278+ sj , err = sjson .Set (sj , "usage" , cumulativeUsage )
277279 if err != nil {
278- http .Error (w , "error marshaling response" , http .StatusInternalServerError )
279- return fmt .Errorf ("failed to marshal response: %w" , err )
280+ return fmt .Errorf ("marshal response usage failed: %w" , err )
280281 }
281282
282283 w .Header ().Set ("Content-Type" , "application/json" )
283284 w .WriteHeader (http .StatusOK )
284- _ , _ = w .Write (out )
285+ _ , _ = w .Write ([] byte ( sj ) )
285286
286287 return nil
287288}
0 commit comments