Skip to content

Improve MchSchema JSON consistency #26

@tzolov

Description

@tzolov

Bug description

Redundant type handling in McpSchema Content implementations causes unnecessary code complexity and potential maintenance issues.
The Content interface and its implementations (TextContent, ImageContent, EmbeddedResource) currently maintain explicit type fields and methods, despite Jackson already handling type information through @JsonSubTypes annotation.
This redundancy increases code complexity and creates potential for inconsistencies if the explicit type values don't match the expected polymorphic types.

Environment

Java SDK version: 17
MCP Java SDK version: 0.8.0-SNAPSHOT

Steps to reproduce

	@Test
	void testTextContent() throws Exception {
		McpSchema.TextContent test = new McpSchema.TextContent("XXX");
		String value = mapper.writeValueAsString(test);
		assertEquals("""
				{"type":"text","text":"XXX"}""", value);
	}

Solution:

Lets Jackson handle the type information entirely and removes the redundant field and method:

  • Remove the type() method from Content interface
  • Removing the type field, constructor initialization, and getter method from all implementations
  • Adjusting constructors to no longer require the type parameter
  • Ensuring tests pass, confirming that JSON serialization still includes the correct type information

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions