Skip to content

Commit 25ab420

Browse files
committed
print structuredContent from JSON RPC response
1 parent 17bc048 commit 25ab420

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
@@ -106,11 +106,15 @@ func handleMcpTool(ctx context.Context, client api.Client, tool *mcp.ToolDef, va
106106
return err
107107
}
108108

109-
data, err := mcp.ParseToolResponse(ctx, resp)
109+
result, err := mcp.ParseToolResponse(resp)
110110
if err != nil {
111111
return err
112112
}
113113

114-
fmt.Printf(string(data))
114+
output, err := json.MarshalIndent(result, "", " ")
115+
if err != nil {
116+
return err
117+
}
118+
fmt.Println(string(output))
115119
return nil
116120
}

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)