Skip to content

Commit ae9bcc7

Browse files
committed
Update: handle JSON decoding errors
1 parent 9203a71 commit ae9bcc7

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

internal/agent/tools/mcp_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ func newTestMCPServer(t *testing.T) (*httptest.Server, *mcp.Client) {
2020
Method string `json:"method"`
2121
Params json.RawMessage `json:"params,omitempty"`
2222
}
23-
json.NewDecoder(r.Body).Decode(&req)
23+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
24+
http.Error(w, err.Error(), http.StatusBadRequest)
25+
return
26+
}
2427
w.Header().Set("Content-Type", "application/json")
2528

2629
type rpcResp struct {
@@ -31,11 +34,11 @@ func newTestMCPServer(t *testing.T) (*httptest.Server, *mcp.Client) {
3134

3235
switch req.Method {
3336
case "initialize":
34-
json.NewEncoder(w).Encode(rpcResp{JSONRPC: "2.0", ID: req.ID, Result: json.RawMessage(`{"capabilities":{}}`)})
37+
_ = json.NewEncoder(w).Encode(rpcResp{JSONRPC: "2.0", ID: req.ID, Result: json.RawMessage(`{"capabilities":{}}`)})
3538
case "notifications/initialized":
3639
w.WriteHeader(http.StatusAccepted)
3740
case "tools/list":
38-
json.NewEncoder(w).Encode(rpcResp{
41+
_ = json.NewEncoder(w).Encode(rpcResp{
3942
JSONRPC: "2.0", ID: req.ID,
4043
Result: json.RawMessage(`{"tools":[{"name":"upper","description":"uppercases text","inputSchema":{"type":"object","properties":{"text":{"type":"string"}},"required":["text"]}}]}`),
4144
})
@@ -46,7 +49,7 @@ func newTestMCPServer(t *testing.T) (*httptest.Server, *mcp.Client) {
4649
}
4750
_ = json.Unmarshal(req.Params, &params)
4851
text := strings.ToUpper(params.Arguments["text"].(string))
49-
json.NewEncoder(w).Encode(rpcResp{
52+
_ = json.NewEncoder(w).Encode(rpcResp{
5053
JSONRPC: "2.0", ID: req.ID,
5154
Result: json.RawMessage(`{"content":[{"type":"text","text":"` + text + `"}]}`),
5255
})
@@ -64,7 +67,7 @@ func newTestMCPServer(t *testing.T) (*httptest.Server, *mcp.Client) {
6467
func TestMCPToolNameAndDescription(t *testing.T) {
6568
srv, client := newTestMCPServer(t)
6669
defer srv.Close()
67-
defer client.Close()
70+
defer func() { _ = client.Close() }()
6871

6972
tools := client.Tools()
7073
if len(tools) != 1 {
@@ -88,7 +91,7 @@ func TestMCPToolNameAndDescription(t *testing.T) {
8891
func TestMCPToolExecute(t *testing.T) {
8992
srv, client := newTestMCPServer(t)
9093
defer srv.Close()
91-
defer client.Close()
94+
defer func() { _ = client.Close() }()
9295

9396
tools := client.Tools()
9497
mcpTool := NewMCPTool(client, "testsvr", tools[0])
@@ -105,7 +108,7 @@ func TestMCPToolExecute(t *testing.T) {
105108
func TestMCPToolRegistration(t *testing.T) {
106109
srv, client := newTestMCPServer(t)
107110
defer srv.Close()
108-
defer client.Close()
111+
defer func() { _ = client.Close() }()
109112

110113
reg := NewRegistry()
111114
for _, tool := range client.Tools() {

0 commit comments

Comments
 (0)