@@ -18,13 +18,13 @@ const MCPURLPath = ".api/mcp/v1"
1818func fetchToolDefinitions (ctx context.Context , client api.Client , endpoint string ) (map [string ]* ToolDef , error ) {
1919 resp , err := doJSONRPC (ctx , client , endpoint , "tools/list" , nil )
2020 if err != nil {
21- return nil , errors .Wrap (err , "failed to list tools from mcp endpoint" )
21+ return nil , errors .Wrapf (err , "JSON-RPC tools/ list request failed to %q" , MCPURLPath )
2222 }
2323 defer resp .Body .Close ()
2424
2525 data , err := readSSEResponseData (resp )
2626 if err != nil {
27- return nil , errors .Wrap (err , "failed to read list tools SSE response" )
27+ return nil , errors .Wrap (err , "failed to read tools/list SSE response" )
2828 }
2929
3030 var rpcResp struct {
@@ -53,7 +53,11 @@ func doToolCall(ctx context.Context, client api.Client, endpoint string, tool st
5353 Arguments : vars ,
5454 }
5555
56- return doJSONRPC (ctx , client , endpoint , "tools/call" , params )
56+ resp , err := doJSONRPC (ctx , client , endpoint , "tools/call" , params )
57+ if err != nil {
58+ return nil , errors .Wrapf (err , "JSON-RPC tools/call request failed to %q" , MCPURLPath )
59+ }
60+ return resp , err
5761}
5862
5963func doJSONRPC (ctx context.Context , client api.Client , endpoint string , method string , params any ) (* http.Response , error ) {
@@ -122,18 +126,19 @@ func decodeToolResponse(resp *http.Response) (map[string]json.RawMessage, error)
122126 return nil , errors .Wrapf (err , "failed to unmarshal MCP JSON-RPC response" )
123127 }
124128 if jsonRPCResp .Error != nil {
125- return nil , errors .Newf ("MCP tools/call failed : %d %s" , jsonRPCResp .Error .Code , jsonRPCResp .Error .Message )
129+ return nil , errors .Newf ("MCP JSON-RPC error : %d %s" , jsonRPCResp .Error .Code , jsonRPCResp .Error .Message )
126130 }
127131
128132 if jsonRPCResp .Result .IsError {
129- if len (jsonRPCResp .Result .Content ) > 0 {
133+ content := jsonRPCResp .Result .Content [0 ]
134+ if len (content ) > 0 {
130135 var textContent struct {
131136 Text string `json:"text"`
132137 }
133- if err := json .Unmarshal (jsonRPCResp . Result . Content [ 0 ] , & textContent ); err == nil && textContent .Text != "" {
138+ if err := json .Unmarshal (content , & textContent ); err == nil && textContent .Text != "" {
134139 return nil , errors .Newf ("MCP tool error: %s" , textContent .Text )
135140 }
136- return nil , errors .Newf ("MCP tool error: %s" , string (jsonRPCResp . Result . Content [ 0 ] ))
141+ return nil , errors .Newf ("MCP tool error: %s" , string (content ))
137142 }
138143 return nil , errors .New ("MCP tool returned an error" )
139144 }
0 commit comments