From 187c7d79a6d793e7326d34d8b7a133c9f156c841 Mon Sep 17 00:00:00 2001 From: abcsnowwolf Date: Sun, 7 Sep 2025 00:44:59 +0800 Subject: [PATCH 1/3] fix: allow description field to be absent from `Tool` * description field is not a requried field of Tool --- Sources/MCP/Server/Tools.swift | 2 +- Tests/MCPTests/ToolTests.swift | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Sources/MCP/Server/Tools.swift b/Sources/MCP/Server/Tools.swift index d5ee9bb1..5f90e798 100644 --- a/Sources/MCP/Server/Tools.swift +++ b/Sources/MCP/Server/Tools.swift @@ -182,7 +182,7 @@ public struct Tool: Hashable, Codable, Sendable { public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) name = try container.decode(String.self, forKey: .name) - description = try container.decode(String.self, forKey: .description) + description = try container.decodeIfPresent(String.self, forKey: .description) ?? "" inputSchema = try container.decode(Value.self, forKey: .inputSchema) annotations = try container.decodeIfPresent(Tool.Annotations.self, forKey: .annotations) ?? .init() diff --git a/Tests/MCPTests/ToolTests.swift b/Tests/MCPTests/ToolTests.swift index af48f3e1..4cadd794 100644 --- a/Tests/MCPTests/ToolTests.swift +++ b/Tests/MCPTests/ToolTests.swift @@ -423,3 +423,20 @@ struct ToolTests { } } } + + @Test("Tool with missing description defaults to empty string") + func testToolWithMissingDescription() throws { + let jsonString = """ + { + "name": "test_tool", + "inputSchema": {} + } + """ + let jsonData = jsonString.data(using: .utf8)! + + let tool = try JSONDecoder().decode(Tool.self, from: jsonData) + + #expect(tool.name == "test_tool") + #expect(tool.description == "") + #expect(tool.inputSchema == [:]) + } \ No newline at end of file From 57010a004c313549202b9f3dfa0885133a6d337d Mon Sep 17 00:00:00 2001 From: Mattt Date: Sun, 7 Sep 2025 04:26:06 -0700 Subject: [PATCH 2/3] Apply suggestions from code review --- Sources/MCP/Server/Tools.swift | 2 +- Tests/MCPTests/ToolTests.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/MCP/Server/Tools.swift b/Sources/MCP/Server/Tools.swift index 5f90e798..fd10d934 100644 --- a/Sources/MCP/Server/Tools.swift +++ b/Sources/MCP/Server/Tools.swift @@ -182,7 +182,7 @@ public struct Tool: Hashable, Codable, Sendable { public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) name = try container.decode(String.self, forKey: .name) - description = try container.decodeIfPresent(String.self, forKey: .description) ?? "" + description = try container.decodeIfPresent(String.self, forKey: .description) inputSchema = try container.decode(Value.self, forKey: .inputSchema) annotations = try container.decodeIfPresent(Tool.Annotations.self, forKey: .annotations) ?? .init() diff --git a/Tests/MCPTests/ToolTests.swift b/Tests/MCPTests/ToolTests.swift index 4cadd794..9fb09948 100644 --- a/Tests/MCPTests/ToolTests.swift +++ b/Tests/MCPTests/ToolTests.swift @@ -437,6 +437,6 @@ struct ToolTests { let tool = try JSONDecoder().decode(Tool.self, from: jsonData) #expect(tool.name == "test_tool") - #expect(tool.description == "") + #expect(tool.description == nil) #expect(tool.inputSchema == [:]) } \ No newline at end of file From 03b9d6326d4b21e1f33292ca7eabeb28a3576320 Mon Sep 17 00:00:00 2001 From: Mattt Date: Sun, 7 Sep 2025 04:26:41 -0700 Subject: [PATCH 3/3] Update Tests/MCPTests/ToolTests.swift --- Tests/MCPTests/ToolTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/MCPTests/ToolTests.swift b/Tests/MCPTests/ToolTests.swift index 9fb09948..b08963b3 100644 --- a/Tests/MCPTests/ToolTests.swift +++ b/Tests/MCPTests/ToolTests.swift @@ -424,7 +424,7 @@ struct ToolTests { } } - @Test("Tool with missing description defaults to empty string") + @Test("Tool with missing description") func testToolWithMissingDescription() throws { let jsonString = """ {