MCP server starters SHOULD use spring.jackson properties.
Bug description
When using spring.jackson.default-property-inclusion=non_null and spring-ai-starter-mcp-server-webmvc
tool calls are returning null values.
For example "end_date": null,
I want to avoid this noise in the context window.
Expected behavior The starter should create a ToolCallResultConverter based on the autoconfigured jackson json mapper and use it.
Workaround Of course I can use this annotation :
@Tool(description = "Retrieve customer information", resultConverter = MyNonNullJacksonConverter.class)
But this does not fit well in the spring boot autoconfiguration philosophy.
Another solution could be to allow injecting a ToolCallResultConverter using MethodToolCallbackProvider.build().toolCallResultConverter( ... ), but this method does not exists.
My feeling is that the root problem is that DefaultToolCallResultConverter and JsonParser are not IoC friendly. Hardcoding a static JsonParser is imho a bad practice.
It may works by registering McpSyncServerCustomizer :
@Bean
McpSyncServerCustomizer jacksonMcpSyncServerCustomizer(JsonMaper jsonMaper) {
return serverBuilder -> serverBuilder.jsonMapper(new JacksonMcpJsonMapper(jsonMaper));
}
It may works by registering
McpSyncServerCustomizer:@Bean McpSyncServerCustomizer jacksonMcpSyncServerCustomizer(JsonMaper jsonMaper) { return serverBuilder -> serverBuilder.jsonMapper(new JacksonMcpJsonMapper(jsonMaper)); }
Nice suggestion, but didn't work. The message is serialized by DefaultToolCallResultConverter instead: MethodToolCallbackProvider.java#L98 -> https://github.com/spring-projects/spring-ai/blob/9bceb110e6bc34798c27ad203e9e005b85a4e9fd/spring-ai-model/src/main/java/org/springframework/ai/tool/support/ToolUtils.java#L92-L97
You are right, it's handled by JsonParser:
https://github.com/spring-projects/spring-ai/blob/9cb7fbe726ae7671680ccd385a399aacc6b18e59/spring-ai-model/src/main/java/org/springframework/ai/util/json/JsonParser.java#L41-L45
I am not sure using spring.jackson is the right path as it could also break some use cases and have unexpected side effects. But there should be a way to customize Jackson here I agree.
what if replace the static JsonParser usage with an injectable JsonMapper inside of DefaultToolCallResultConverter? then expose it inside of MethodToolCallbackProvider.java#L98 as @fbus mentioned.