client: Honor IsError and extract JSON from tool text
CI / Test and build (pull_request) Successful in 14s
CI / Test and build (pull_request) Successful in 14s
Fold Jerry dump into toolJSON: TrimSpace IsError text, concat JSON via rawJSON, and unit tests mirroring IsError=true / StructuredContent=null / Text=`end must be after start` / GetError=nil.
This commit is contained in:
+52
-16
@@ -46,7 +46,7 @@ func TestToolJSON(t *testing.T) {
|
||||
{
|
||||
name: "wrapped JSON in prose",
|
||||
res: &mcp.CallToolResult{
|
||||
Content: []mcp.Content{&mcp.TextContent{Text: `here is data: {"symbol":"MU"} thanks`}},
|
||||
Content: []mcp.Content{&mcp.TextContent{Text: `here: {"symbol":"MU"} ok`}},
|
||||
},
|
||||
want: `{"symbol":"MU"}`,
|
||||
},
|
||||
@@ -60,6 +60,16 @@ func TestToolJSON(t *testing.T) {
|
||||
},
|
||||
want: `{"symbol":"MU"}`,
|
||||
},
|
||||
{
|
||||
name: "concatenated text JSON",
|
||||
res: &mcp.CallToolResult{
|
||||
Content: []mcp.Content{
|
||||
&mcp.TextContent{Text: `{"symbol":`},
|
||||
&mcp.TextContent{Text: `"MU"}`},
|
||||
},
|
||||
},
|
||||
want: `{"symbol":"MU"}`,
|
||||
},
|
||||
{
|
||||
name: "wrapped JSON array",
|
||||
res: &mcp.CallToolResult{
|
||||
@@ -110,18 +120,38 @@ func TestToolJSON_getError(t *testing.T) {
|
||||
|
||||
func TestToolJSON_isErrorText(t *testing.T) {
|
||||
t.Parallel()
|
||||
// Jerry dump: IsError=true, StructuredContent=null, Text=`end must be after start`, GetError=nil.
|
||||
res := &mcp.CallToolResult{
|
||||
IsError: true,
|
||||
Content: []mcp.Content{&mcp.TextContent{Text: "historicals unavailable"}},
|
||||
IsError: true,
|
||||
StructuredContent: nil,
|
||||
Content: []mcp.Content{&mcp.TextContent{Text: "end must be after start"}},
|
||||
}
|
||||
if res.GetError() != nil {
|
||||
t.Fatal("GetError should be nil when IsError is set via field")
|
||||
t.Fatal("GetError should be nil on a client-shaped result")
|
||||
}
|
||||
_, err := toolJSON(res)
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
if err.Error() != "historicals unavailable" {
|
||||
if err.Error() != "end must be after start" {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), "non-json tool result") {
|
||||
t.Fatalf("got non-json wrapping: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolJSON_isErrorOverStructured(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := toolJSON(&mcp.CallToolResult{
|
||||
IsError: true,
|
||||
StructuredContent: map[string]any{"symbol": "MU"},
|
||||
Content: []mcp.Content{&mcp.TextContent{Text: "end must be after start"}},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
if err.Error() != "end must be after start" {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), "non-json tool result") {
|
||||
@@ -131,15 +161,21 @@ func TestToolJSON_isErrorText(t *testing.T) {
|
||||
|
||||
func TestToolJSON_isErrorEmpty(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := toolJSON(&mcp.CallToolResult{IsError: true})
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
tests := []*mcp.CallToolResult{
|
||||
{IsError: true},
|
||||
{IsError: true, Content: []mcp.Content{&mcp.TextContent{Text: " \n"}}},
|
||||
}
|
||||
if err.Error() != "tool error" {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), "non-json tool result") {
|
||||
t.Fatalf("got non-json wrapping: %v", err)
|
||||
for _, res := range tests {
|
||||
_, err := toolJSON(res)
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
if err.Error() != "tool error" {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), "non-json tool result") {
|
||||
t.Fatalf("got non-json wrapping: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,10 +188,10 @@ func TestToolJSON_nonJSONText(t *testing.T) {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
msg := err.Error()
|
||||
if !strings.Contains(msg, "non-json") {
|
||||
t.Fatalf("missing non-json: %v", err)
|
||||
if !strings.Contains(msg, "non-json tool result") {
|
||||
t.Fatalf("missing non-json tool result: %v", err)
|
||||
}
|
||||
for _, want := range []string{`isError=false`, `types=[text]`, `structured=nil`, `prefix="not json"`} {
|
||||
for _, want := range []string{`isError=false`, `types=[text]`, `structured=nil`, `textLen=8`, `prefix="not json"`} {
|
||||
if !strings.Contains(msg, want) {
|
||||
t.Fatalf("missing %q in %v", want, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user