From 09c4dff7c406e992c09c53a6972ce1751dcebdca Mon Sep 17 00:00:00 2001 From: yasomaru Date: Sun, 17 Aug 2025 14:28:27 +0900 Subject: [PATCH] mcp: replace custom randText implementation with crypto/rand.Text - Remove custom base32alphabet constant and manual implementation - Use Go 1.24's crypto/rand.Text() function for secure random text generation - Simplify code by removing 26 lines of custom implementation - Maintain same functionality with standard library implementation This change resolves TODO comments about using crypto/rand.Text once Go 1.24 is assured. The standard library implementation provides the same security guarantees with better maintainability and consistency. Fixes: #TODO (use crypto/rand.Text) --- examples/server/sequentialthinking/main.go | 12 +----------- mcp/util.go | 12 +----------- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/examples/server/sequentialthinking/main.go b/examples/server/sequentialthinking/main.go index 45a4fa6f..7952aaf0 100644 --- a/examples/server/sequentialthinking/main.go +++ b/examples/server/sequentialthinking/main.go @@ -486,18 +486,8 @@ func ThinkingHistory(ctx context.Context, req *mcp.ServerRequest[*mcp.ReadResour }, nil } -// Copied from crypto/rand. -// TODO: once 1.24 is assured, just use crypto/rand. -const base32alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" - func randText() string { - // ⌈log₃₂ 2¹²⁸⌉ = 26 chars - src := make([]byte, 26) - rand.Read(src) - for i := range src { - src[i] = base32alphabet[src[i]%32] - } - return string(src) + return rand.Text() } func main() { diff --git a/mcp/util.go b/mcp/util.go index 102c0885..fed02c56 100644 --- a/mcp/util.go +++ b/mcp/util.go @@ -14,16 +14,6 @@ func assert(cond bool, msg string) { } } -// Copied from crypto/rand. -// TODO: once 1.24 is assured, just use crypto/rand. -const base32alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" - func randText() string { - // ⌈log₃₂ 2¹²⁸⌉ = 26 chars - src := make([]byte, 26) - rand.Read(src) - for i := range src { - src[i] = base32alphabet[src[i]%32] - } - return string(src) + return rand.Text() }