|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.mcp.server.common.autoconfigure; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 20 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 23 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 24 | +import io.modelcontextprotocol.spec.McpSchema; |
| 25 | + |
| 26 | +import org.springframework.ai.mcp.server.common.autoconfigure.properties.McpServerProperties; |
| 27 | +import org.springframework.ai.util.JacksonUtils; |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 31 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 32 | +import org.springframework.context.annotation.Bean; |
| 33 | + |
| 34 | +@AutoConfiguration |
| 35 | +@ConditionalOnClass(McpSchema.class) |
| 36 | +@ConditionalOnProperty(prefix = McpServerProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true", |
| 37 | + matchIfMissing = true) |
| 38 | +@ConditionalOnMissingBean(name = "mcpServerObjectMapper") |
| 39 | +public class McpServerObjectMapperAutoConfiguration { |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a configured ObjectMapper for MCP server JSON serialization. |
| 43 | + * <p> |
| 44 | + * This ObjectMapper is specifically configured for MCP protocol compliance with: |
| 45 | + * <ul> |
| 46 | + * <li>Lenient deserialization that doesn't fail on unknown properties</li> |
| 47 | + * <li>Proper handling of empty beans during serialization</li> |
| 48 | + * <li>Exclusion of null values from JSON output</li> |
| 49 | + * <li>Standard Jackson modules for Java 8, JSR-310, and Kotlin support</li> |
| 50 | + * </ul> |
| 51 | + * <p> |
| 52 | + * This bean can be overridden by providing a custom ObjectMapper bean with the name |
| 53 | + * "mcpServerObjectMapper". |
| 54 | + * @return configured ObjectMapper instance for MCP server operations |
| 55 | + */ |
| 56 | + // NOTE: defaultCandidate=false prevents this MCP specific mapper from being injected |
| 57 | + // in code that doesn't explicitly qualify injection point by name. |
| 58 | + @Bean(name = "mcpServerObjectMapper", defaultCandidate = false) |
| 59 | + public ObjectMapper mcpServerObjectMapper() { |
| 60 | + return JsonMapper.builder() |
| 61 | + // Deserialization configuration |
| 62 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 63 | + .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT) |
| 64 | + // Serialization configuration |
| 65 | + .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) |
| 66 | + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 67 | + .serializationInclusion(JsonInclude.Include.NON_NULL) |
| 68 | + // Register standard Jackson modules (Jdk8, JavaTime, ParameterNames, Kotlin) |
| 69 | + .addModules(JacksonUtils.instantiateAvailableModules()) |
| 70 | + .build(); |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments