fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

[BUG] 对mcp java-sdk中的TextContent进行序列化为json过程中丢失type字段

Open bwangll opened this issue 4 months ago • 1 comments

问题描述

对mcp的java-sdk中的TextContent序列化时,丢失字段

环境信息

  • OS信息: macOS m1
  • JDK信息: Openjdk 17
  • 版本信息:Fastjson2 2.0.49

重现步骤

McpSchema.TextContent content = new McpSchema.TextContent("{\"a\":\"b\"}");
        List<McpSchema.Content> contents = List.of(content);
        McpSchema.CallToolResult result = new McpSchema.CallToolResult.Builder().content(contents).isError(false).build();
        McpSchema.JSONRPCResponse jsonrpcResponse = new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, 1, result, null);

        JSON.toJSONString(jsonrpcResponse)

期待的正确结果

期望结果

{
    "jsonrpc": "2.0",
    "id": 2,
    "result": {
        "content": [
            {
                "type": "text",
                "text": "{\"a\":\"b\"}"
            }
        ],
        "isError": false
    }
}

conotent应该有"type": "text" 使用jackson有( mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);) 使用fastjson该字段丢失 mcp java-sdk为最新版

bwangll avatar Sep 03 '25 06:09 bwangll

JSON.toJSONString(jsonrpcResponse, JSONWriter.Feature.FieldBased) 这样试试看

wenshao avatar Sep 20 '25 03:09 wenshao