Skip to content

bug: IsError field in CallToolResult is missing from serialized output when false (java mcp client) #566

@win-cmd

Description

@win-cmd

Description

When using a Java MCP client to call a Go MCP server, the client needs to read the IsError field from the returned CallToolResult.

In the latest version of the code, when IsError is false, this field is not included in the serialized object. It only appears when IsError is true.

Code Sample

// MarshalJSON implements custom JSON marshaling for CallToolResult
func (r CallToolResult) MarshalJSON() ([]byte, error) {
	m := make(map[string]any)

	// Marshal Meta if present
	if r.Meta != nil {
		m["_meta"] = r.Meta
	}

	// Marshal Content array
	content := make([]any, len(r.Content))
	for i, c := range r.Content {
		content[i] = c
	}
	m["content"] = content

	// Marshal IsError if true
	if r.IsError {
		m["isError"] = r.IsError
	}

	return json.Marshal(m)
}

Logs or Error Messages

Cannot invoke "java.lang.Boolean.booleanValue()" because the return value of "io.modelcontextprotocol.spec.McpSchema$CallToolResult.isError()" is null

Environment

Additional Context

Add any other context about the problem here.

Possible Solution

// MarshalJSON implements custom JSON marshaling for CallToolResult
func (r CallToolResult) MarshalJSON() ([]byte, error) {
	m := make(map[string]any)

	// Marshal Meta if present
	if r.Meta != nil {
		m["_meta"] = r.Meta
	}

	// Marshal Content array
	content := make([]any, len(r.Content))
	for i, c := range r.Content {
		content[i] = c
	}
	m["content"] = content

	// *** fix this ***
	m["isError"] = r.IsError

	return json.Marshal(m)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions