Skip to content

Commit f3b9ba4

Browse files
authored
Make Tool.inputSchema nonoptional (#123)
1 parent c95d0c0 commit f3b9ba4

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

Sources/MCP/Server/Tools.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct Tool: Hashable, Codable, Sendable {
1414
/// The tool description
1515
public let description: String
1616
/// The tool input schema
17-
public let inputSchema: Value?
17+
public let inputSchema: Value
1818

1919
/// Annotations that provide display-facing and operational information for a Tool.
2020
///
@@ -86,7 +86,7 @@ public struct Tool: Hashable, Codable, Sendable {
8686
public init(
8787
name: String,
8888
description: String,
89-
inputSchema: Value? = nil,
89+
inputSchema: Value,
9090
annotations: Annotations = nil
9191
) {
9292
self.name = name
@@ -183,7 +183,7 @@ public struct Tool: Hashable, Codable, Sendable {
183183
let container = try decoder.container(keyedBy: CodingKeys.self)
184184
name = try container.decode(String.self, forKey: .name)
185185
description = try container.decode(String.self, forKey: .description)
186-
inputSchema = try container.decodeIfPresent(Value.self, forKey: .inputSchema)
186+
inputSchema = try container.decode(Value.self, forKey: .inputSchema)
187187
annotations =
188188
try container.decodeIfPresent(Tool.Annotations.self, forKey: .annotations) ?? .init()
189189
}
@@ -192,9 +192,7 @@ public struct Tool: Hashable, Codable, Sendable {
192192
var container = encoder.container(keyedBy: CodingKeys.self)
193193
try container.encode(name, forKey: .name)
194194
try container.encode(description, forKey: .description)
195-
if let schema = inputSchema {
196-
try container.encode(schema, forKey: .inputSchema)
197-
}
195+
try container.encode(inputSchema, forKey: .inputSchema)
198196
if !annotations.isEmpty {
199197
try container.encode(annotations, forKey: .annotations)
200198
}

Tests/MCPTests/ToolTests.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ struct ToolTests {
130130
func testToolWithEmptyAnnotations() throws {
131131
var tool = Tool(
132132
name: "test_tool",
133-
description: "Test tool description"
133+
description: "Test tool description",
134+
inputSchema: [:]
134135
)
135136

136137
do {
@@ -163,7 +164,7 @@ struct ToolTests {
163164
let tool = Tool(
164165
name: "test_tool",
165166
description: "Test tool description",
166-
inputSchema: nil,
167+
inputSchema: [:],
167168
annotations: nil
168169
)
169170

@@ -324,8 +325,8 @@ struct ToolTests {
324325
@Test("ListTools result validation")
325326
func testListToolsResult() throws {
326327
let tools = [
327-
Tool(name: "tool1", description: "First tool", inputSchema: nil),
328-
Tool(name: "tool2", description: "Second tool", inputSchema: nil),
328+
Tool(name: "tool1", description: "First tool", inputSchema: [:]),
329+
Tool(name: "tool2", description: "Second tool", inputSchema: [:]),
329330
]
330331

331332
let result = ListTools.Result(tools: tools, nextCursor: "next_page")
@@ -399,7 +400,11 @@ struct ToolTests {
399400
#expect(request.id == 1)
400401
#expect(request.params.cursor == nil)
401402

402-
let testTool = Tool(name: "test_tool", description: "Test tool for verification")
403+
let testTool = Tool(
404+
name: "test_tool",
405+
description: "Test tool for verification",
406+
inputSchema: [:]
407+
)
403408
return ListTools.response(id: request.id, result: ListTools.Result(tools: [testTool]))
404409
}
405410

0 commit comments

Comments
 (0)