Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mcp/mcp-annotations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<scope>test</scope>
</dependency>

</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
import org.springframework.ai.mcp.annotation.McpToolParam;
import org.springframework.ai.mcp.annotation.context.McpAsyncRequestContext;
import org.springframework.ai.mcp.annotation.context.McpSyncRequestContext;
import org.springframework.ai.model.KotlinModule;
import org.springframework.ai.util.json.JsonParser;
import org.springframework.ai.util.json.schema.JsonSchemaGenerator.SchemaOption;
import org.springframework.core.KotlinDetector;
import org.springframework.core.Nullness;
import org.springframework.util.ClassUtils;
import org.springframework.util.ConcurrentReferenceHashMap;
Expand Down Expand Up @@ -85,16 +87,21 @@ public final class McpJsonSchemaGenerator {
Module springAiSchemaModule = PROPERTY_REQUIRED_BY_DEFAULT ? new McpSpringAiSchemaModule()
: new McpSpringAiSchemaModule(McpSpringAiSchemaModule.Option.PROPERTY_REQUIRED_FALSE_BY_DEFAULT);

SchemaGeneratorConfig subtypeConfig = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12,
OptionPreset.PLAIN_JSON)
SchemaGeneratorConfigBuilder subtypeConfigBuilder = new SchemaGeneratorConfigBuilder(
SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON)
.with(jacksonModule)
.with(openApiModule)
.with(springAiSchemaModule)
.with(Option.EXTRA_OPEN_API_FORMAT_VALUES)
.with(Option.STANDARD_FORMATS)
.with(Option.PLAIN_DEFINITION_KEYS)
.without(Option.SCHEMA_VERSION_INDICATOR)
.build();
.without(Option.SCHEMA_VERSION_INDICATOR);

if (KotlinDetector.isKotlinReflectPresent()) {
subtypeConfigBuilder.with(new KotlinModule());
}

SchemaGeneratorConfig subtypeConfig = subtypeConfigBuilder.build();

SUBTYPE_SCHEMA_GENERATOR = new SchemaGenerator(subtypeConfig);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2023-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.mcp.annotation.method.tool.utils

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.ai.mcp.annotation.McpTool
import org.springframework.ai.mcp.annotation.McpToolParam
import tools.jackson.databind.JsonNode
import tools.jackson.databind.json.JsonMapper

class McpJsonSchemaGeneratorKotlinTests {

private val jsonMapper = JsonMapper()

@Test
fun `nullable Kotlin properties are not required in MCP tool input schemas`() {
val method = SearchTools::class.java.getMethod("search", Filter::class.java)

val schema = McpJsonSchemaGenerator.generateForMethodInput(method)
val schemaNode = jsonMapper.readTree(schema)
val filterNode = schemaNode["properties"]["filter"]
val topLevelRequired = schemaNode["required"]
val filterRequired = filterNode["required"]

assertThat(requiredNames(topLevelRequired)).doesNotContain("filter")
assertThat(requiredNames(filterRequired)).doesNotContain("name", "ids")
}

@Test
fun `non-null Kotlin constructor properties without defaults remain required`() {
val method = SearchTools::class.java.getMethod("mixed", SearchRequest::class.java)

val schema = McpJsonSchemaGenerator.generateForMethodInput(method)
val schemaNode = jsonMapper.readTree(schema)
val requestNode = schemaNode["properties"]["request"]
val requestRequired = requiredNames(requestNode["required"])

assertThat(requestRequired).contains("query")
assertThat(requestRequired).doesNotContain("filter")
}

private fun requiredNames(required: JsonNode?): List<String> {
if (required == null || required.isNull) {
return emptyList()
}
return required.iterator().asSequence().map { it.asString() }.toList()
}

private data class Filter(val name: String? = null, val ids: List<String>? = null)

private data class SearchRequest(val query: String, val filter: String? = null)

private class SearchTools {

@McpTool(description = "Search")
fun search(@McpToolParam(required = false) filter: Filter? = null): String {
return "ok"
}

@McpTool(description = "Mixed")
fun mixed(@McpToolParam(required = false) request: SearchRequest? = null): String {
return "ok"
}

}

}
Loading