Skip to content

Commit 1ddb5f0

Browse files
committed
Make Tool.inputSchema nonoptional
1 parent 14fddc4 commit 1ddb5f0

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
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: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,28 +126,29 @@ struct ToolTests {
126126
func testToolWithEmptyAnnotations() throws {
127127
var tool = Tool(
128128
name: "test_tool",
129-
description: "Test tool description"
129+
description: "Test tool description",
130+
inputSchema: [:],
130131
)
131132

132133
do {
133134
#expect(tool.annotations.isEmpty)
134-
135+
135136
let encoder = JSONEncoder()
136137
let data = try encoder.encode(tool)
137-
138+
138139
// Verify that empty annotations are not included in the JSON
139140
let jsonString = String(data: data, encoding: .utf8)!
140141
#expect(!jsonString.contains("\"annotations\""))
141142
}
142-
143+
143144
do {
144145
tool.annotations.title = "Test"
145146

146147
#expect(!tool.annotations.isEmpty)
147-
148+
148149
let encoder = JSONEncoder()
149150
let data = try encoder.encode(tool)
150-
151+
151152
// Verify that empty annotations are not included in the JSON
152153
let jsonString = String(data: data, encoding: .utf8)!
153154
#expect(jsonString.contains("\"annotations\""))
@@ -159,7 +160,7 @@ struct ToolTests {
159160
let tool = Tool(
160161
name: "test_tool",
161162
description: "Test tool description",
162-
inputSchema: nil,
163+
inputSchema: [:],
163164
annotations: nil
164165
)
165166

@@ -318,8 +319,8 @@ struct ToolTests {
318319
@Test("ListTools result validation")
319320
func testListToolsResult() throws {
320321
let tools = [
321-
Tool(name: "tool1", description: "First tool", inputSchema: nil),
322-
Tool(name: "tool2", description: "Second tool", inputSchema: nil),
322+
Tool(name: "tool1", description: "First tool", inputSchema: [:]),
323+
Tool(name: "tool2", description: "Second tool", inputSchema: [:]),
323324
]
324325

325326
let result = ListTools.Result(tools: tools, nextCursor: "next_page")
@@ -393,7 +394,11 @@ struct ToolTests {
393394
#expect(request.id == 1)
394395
#expect(request.params.cursor == nil)
395396

396-
let testTool = Tool(name: "test_tool", description: "Test tool for verification")
397+
let testTool = Tool(
398+
name: "test_tool",
399+
description: "Test tool for verification",
400+
inputSchema: [:]
401+
)
397402
return ListTools.response(id: request.id, result: ListTools.Result(tools: [testTool]))
398403
}
399404

0 commit comments

Comments
 (0)