Skip to content

Commit b1ccf31

Browse files
committed
Allow decoding Decodable type directly from Value
Summary: I noticed we are not converting value associated with response to the strong types requests recorded This change will help to decode the value type later once we support the rest of plumbing (next commit)
1 parent 9ebe125 commit b1ccf31

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Sources/MCP/Base/Value.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,16 @@ extension String {
400400
}
401401
}
402402
}
403+
404+
405+
406+
// MARK: - Decode specific type from Value
407+
408+
public extension JSONDecoder {
409+
func decode<T: Decodable>(_ type: T.Type, from value: Value) throws -> T {
410+
guard let data = try? JSONEncoder().encode(value) else {
411+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Failed to encode value"))
412+
}
413+
return try self.decode(T.self, from: data)
414+
}
415+
}

0 commit comments

Comments
 (0)