Skip to content

Commit ab3a81b

Browse files
committed
print structuredContent from JSON RPC response
1 parent a2a2534 commit ab3a81b

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

cmd/src/mcp.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,15 @@ func handleMcpTool(ctx context.Context, client api.Client, tool *mcp.ToolDef, va
9797
return err
9898
}
9999

100-
data, err := mcp.ParseToolResponse(ctx, resp)
100+
result, err := mcp.ParseToolResponse(resp)
101101
if err != nil {
102102
return err
103103
}
104104

105-
fmt.Printf(string(data))
105+
output, err := json.MarshalIndent(result, "", " ")
106+
if err != nil {
107+
return err
108+
}
109+
fmt.Println(string(output))
106110
return nil
107111
}

internal/mcp/mcp_request.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"io"
88
"net/http"
99

10-
"github.com/sourcegraph/sourcegraph/lib/errors"
1110
"github.com/sourcegraph/src-cli/internal/api"
11+
12+
"github.com/sourcegraph/sourcegraph/lib/errors"
1213
)
1314

1415
const McpURLPath = ".api/mcp/v1"
@@ -49,7 +50,32 @@ func DoToolRequest(ctx context.Context, client api.Client, tool *ToolDef, vars m
4950
return client.Do(req)
5051
}
5152

52-
func ParseToolResponse(ctx context.Context, resp *http.Response) ([]byte, error) {
53+
func ParseToolResponse(resp *http.Response) (map[string]json.RawMessage, error) {
54+
data, err := readSSEResponseData(resp)
55+
if err != nil {
56+
return nil, err
57+
}
58+
59+
if data == nil {
60+
return map[string]json.RawMessage{}, nil
61+
}
62+
63+
jsonRPCResp := struct {
64+
Version string `json:"jsonrpc"`
65+
ID int `json:"id"`
66+
Result struct {
67+
Content []json.RawMessage `json:"content"`
68+
StructuredContent map[string]json.RawMessage `json:"structuredContent"`
69+
} `json:"result"`
70+
}{}
71+
if err := json.Unmarshal(data, &jsonRPCResp); err != nil {
72+
return nil, errors.Wrapf(err, "failed to unmarshal MCP JSON-RPC response")
73+
}
74+
75+
return jsonRPCResp.Result.StructuredContent, nil
76+
}
77+
func readSSEResponseData(resp *http.Response) ([]byte, error) {
78+
defer resp.Body.Close()
5379
data, err := io.ReadAll(resp.Body)
5480
if err != nil {
5581
return nil, err

0 commit comments

Comments
 (0)