|
| 1 | +package io.modelcontextprotocol.kotlin.sdk.types |
| 2 | + |
| 3 | +import io.kotest.matchers.collections.shouldContainExactly |
| 4 | +import io.kotest.matchers.nulls.shouldNotBeNull |
| 5 | +import io.kotest.matchers.shouldBe |
| 6 | +import kotlinx.schema.json.ArrayPropertyDefinition |
| 7 | +import kotlinx.schema.json.FunctionCallingSchema |
| 8 | +import kotlinx.schema.json.ObjectPropertyDefinition |
| 9 | +import kotlinx.schema.json.StringPropertyDefinition |
| 10 | +import kotlin.test.Test |
| 11 | + |
| 12 | +class FunctionCallingSchemaAsToolSchemaTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + fun `should convert function parameters`() { |
| 16 | + val functionSchema = FunctionCallingSchema( |
| 17 | + name = "calculate", |
| 18 | + description = "Perform calculation", |
| 19 | + parameters = ObjectPropertyDefinition( |
| 20 | + properties = mapOf( |
| 21 | + "operation" to StringPropertyDefinition( |
| 22 | + description = "Math operation", |
| 23 | + ), |
| 24 | + "x" to StringPropertyDefinition(), |
| 25 | + "y" to StringPropertyDefinition(), |
| 26 | + ), |
| 27 | + required = listOf("operation", "x", "y"), |
| 28 | + ), |
| 29 | + ) |
| 30 | + |
| 31 | + val toolSchema = functionSchema.asToolSchema() |
| 32 | + |
| 33 | + toolSchema.type shouldBe "object" |
| 34 | + toolSchema.properties.shouldNotBeNull { |
| 35 | + keys shouldContainExactly setOf("operation", "x", "y") |
| 36 | + } |
| 37 | + toolSchema.required shouldContainExactly listOf("operation", "x", "y") |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun `should handle required and optional parameters`() { |
| 42 | + // Test with some required, some optional |
| 43 | + val mixedSchema = FunctionCallingSchema( |
| 44 | + name = "search", |
| 45 | + parameters = ObjectPropertyDefinition( |
| 46 | + properties = mapOf( |
| 47 | + "query" to StringPropertyDefinition(description = "Search query"), |
| 48 | + "limit" to StringPropertyDefinition(description = "Result limit"), |
| 49 | + ), |
| 50 | + required = listOf("query"), |
| 51 | + ), |
| 52 | + ) |
| 53 | + |
| 54 | + val mixedToolSchema = mixedSchema.asToolSchema() |
| 55 | + mixedToolSchema.type shouldBe "object" |
| 56 | + mixedToolSchema.properties.shouldNotBeNull { |
| 57 | + keys shouldContainExactly setOf("query", "limit") |
| 58 | + } |
| 59 | + mixedToolSchema.required shouldContainExactly listOf("query") |
| 60 | + |
| 61 | + // Test with all optional (empty required list) |
| 62 | + val allOptionalSchema = FunctionCallingSchema( |
| 63 | + name = "list", |
| 64 | + parameters = ObjectPropertyDefinition( |
| 65 | + properties = mapOf("filter" to StringPropertyDefinition()), |
| 66 | + required = emptyList(), |
| 67 | + ), |
| 68 | + ) |
| 69 | + |
| 70 | + val allOptionalToolSchema = allOptionalSchema.asToolSchema() |
| 71 | + allOptionalToolSchema.required.shouldNotBeNull { |
| 72 | + this shouldContainExactly emptyList() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `should handle complex nested parameter types`() { |
| 78 | + val functionSchema = FunctionCallingSchema( |
| 79 | + name = "process", |
| 80 | + parameters = ObjectPropertyDefinition( |
| 81 | + properties = mapOf( |
| 82 | + "items" to ArrayPropertyDefinition( |
| 83 | + items = StringPropertyDefinition(), |
| 84 | + description = "List of items to process", |
| 85 | + ), |
| 86 | + "config" to ObjectPropertyDefinition( |
| 87 | + properties = mapOf( |
| 88 | + "timeout" to StringPropertyDefinition(description = "Timeout in seconds"), |
| 89 | + "retries" to StringPropertyDefinition(description = "Number of retries"), |
| 90 | + ), |
| 91 | + required = listOf("timeout"), |
| 92 | + ), |
| 93 | + ), |
| 94 | + required = listOf("items", "config"), |
| 95 | + ), |
| 96 | + ) |
| 97 | + |
| 98 | + val toolSchema = functionSchema.asToolSchema() |
| 99 | + |
| 100 | + toolSchema.type shouldBe "object" |
| 101 | + toolSchema.properties.shouldNotBeNull { |
| 102 | + keys shouldContainExactly setOf("items", "config") |
| 103 | + } |
| 104 | + toolSchema.required shouldContainExactly listOf("items", "config") |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun `should handle parameters count variations`() { |
| 109 | + // Empty parameters |
| 110 | + val emptySchema = FunctionCallingSchema( |
| 111 | + name = "ping", |
| 112 | + parameters = ObjectPropertyDefinition( |
| 113 | + properties = emptyMap(), |
| 114 | + required = emptyList(), |
| 115 | + ), |
| 116 | + ) |
| 117 | + |
| 118 | + val emptyToolSchema = emptySchema.asToolSchema() |
| 119 | + emptyToolSchema.type shouldBe "object" |
| 120 | + emptyToolSchema.properties.shouldNotBeNull { |
| 121 | + keys shouldContainExactly emptySet() |
| 122 | + } |
| 123 | + |
| 124 | + // Single parameter |
| 125 | + val singleSchema = FunctionCallingSchema( |
| 126 | + name = "greet", |
| 127 | + parameters = ObjectPropertyDefinition( |
| 128 | + properties = mapOf( |
| 129 | + "name" to StringPropertyDefinition(description = "Name to greet"), |
| 130 | + ), |
| 131 | + required = listOf("name"), |
| 132 | + ), |
| 133 | + ) |
| 134 | + |
| 135 | + val singleToolSchema = singleSchema.asToolSchema() |
| 136 | + singleToolSchema.properties.shouldNotBeNull { |
| 137 | + keys shouldContainExactly setOf("name") |
| 138 | + } |
| 139 | + singleToolSchema.required shouldContainExactly listOf("name") |
| 140 | + } |
| 141 | +} |
0 commit comments