@@ -45,6 +45,11 @@ public extension Dictionary where Key == String, Value == Sendable {
4545 // return the actual enum case value that matches the string label
4646 return allCases [ index]
4747 }
48+ else if T . self == Date . self {
49+ // Handle Date type using the new extractDate method
50+ let date = try extractDate ( named: name)
51+ return date as! T
52+ }
4853 else {
4954 throw MCPToolError . invalidArgumentType (
5055 parameterName: name,
@@ -165,4 +170,48 @@ public extension Dictionary where Key == String, Value == Sendable {
165170 )
166171 }
167172 }
173+
174+ /// Extracts a Date parameter from the dictionary, attempting multiple parsing strategies
175+ /// - Parameter name: The name of the parameter
176+ /// - Returns: The extracted Date value
177+ /// - Throws: MCPToolError.invalidArgumentType if the parameter cannot be converted to a Date
178+ func extractDate( named name: String ) throws -> Date {
179+ guard let anyValue = self [ name] else {
180+ preconditionFailure ( " Failed to retrieve value for parameter \( name) " )
181+ }
182+
183+ // If it's already a Date, return it
184+ if let date = anyValue as? Date {
185+ return date
186+ }
187+
188+ // Try parsing as string
189+ if let stringValue = anyValue as? String {
190+ // Try ISO 8601 date format
191+ let isoFormatter = ISO8601DateFormatter ( )
192+ if let date = isoFormatter. date ( from: stringValue) {
193+ return date
194+ }
195+
196+ // Try Unix timestamp (both integer and decimal)
197+ if let timestampDouble = Double ( stringValue) {
198+ return Date ( timeIntervalSince1970: timestampDouble)
199+ }
200+ }
201+
202+ // Try direct conversion from number
203+ if let timestampDouble = anyValue as? Double {
204+ return Date ( timeIntervalSince1970: timestampDouble)
205+ }
206+
207+ if let timestampInt = anyValue as? Int {
208+ return Date ( timeIntervalSince1970: TimeInterval ( timestampInt) )
209+ }
210+
211+ throw MCPToolError . invalidArgumentType (
212+ parameterName: name,
213+ expectedType: " ISO 8601 Date " ,
214+ actualType: String ( describing: Swift . type ( of: anyValue) )
215+ )
216+ }
168217}
0 commit comments