@@ -14,18 +14,17 @@ import (
1414)
1515
1616const MCPURLPath = ".api/mcp/v1"
17- const MCPDeepSearchURLPath = ".api/mcp/deepsearch"
1817
1918func fetchToolDefinitions (ctx context.Context , client api.Client , endpoint string ) (map [string ]* ToolDef , error ) {
2019 resp , err := doJSONRPC (ctx , client , endpoint , "tools/list" , nil )
2120 if err != nil {
22- 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 )
2322 }
2423 defer resp .Body .Close ()
2524
2625 data , err := readSSEResponseData (resp )
2726 if err != nil {
28- 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" )
2928 }
3029
3130 var rpcResp struct {
@@ -54,7 +53,11 @@ func doToolCall(ctx context.Context, client api.Client, endpoint string, tool st
5453 Arguments : vars ,
5554 }
5655
57- 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
5861}
5962
6063func doJSONRPC (ctx context.Context , client api.Client , endpoint string , method string , params any ) (* http.Response , error ) {
@@ -123,18 +126,19 @@ func decodeToolResponse(resp *http.Response) (map[string]json.RawMessage, error)
123126 return nil , errors .Wrapf (err , "failed to unmarshal MCP JSON-RPC response" )
124127 }
125128 if jsonRPCResp .Error != nil {
126- 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 )
127130 }
128131
129132 if jsonRPCResp .Result .IsError {
130- if len (jsonRPCResp .Result .Content ) > 0 {
133+ content := jsonRPCResp .Result .Content [0 ]
134+ if len (content ) > 0 {
131135 var textContent struct {
132136 Text string `json:"text"`
133137 }
134- if err := json .Unmarshal (jsonRPCResp . Result . Content [ 0 ] , & textContent ); err == nil && textContent .Text != "" {
138+ if err := json .Unmarshal (content , & textContent ); err == nil && textContent .Text != "" {
135139 return nil , errors .Newf ("MCP tool error: %s" , textContent .Text )
136140 }
137- return nil , errors .Newf ("MCP tool error: %s" , string (jsonRPCResp . Result . Content [ 0 ] ))
141+ return nil , errors .Newf ("MCP tool error: %s" , string (content ))
138142 }
139143 return nil , errors .New ("MCP tool returned an error" )
140144 }
0 commit comments