mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-02-27 09:05:12 +00:00
## Summary - MCP clients may send numbers as strings. This adds `ToInt64` and `GetOptionalInt` helpers to `pkg/params` and replaces all raw `.(float64)` type assertions across operation handlers to accept both `float64` and string inputs. ## Test plan - [x] Verify `go test ./...` passes - [x] Test with an MCP client that sends numeric parameters as strings *Created by Claude on behalf of @silverwind* Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/138 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-committed-by: silverwind <me@silverwind.io>
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package params
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
)
|
|
|
|
// ToInt64 converts a value to int64, accepting both float64 (JSON number) and
|
|
// string representations. Returns false if the value cannot be converted.
|
|
func ToInt64(val any) (int64, bool) {
|
|
switch v := val.(type) {
|
|
case float64:
|
|
return int64(v), true
|
|
case string:
|
|
i, err := strconv.ParseInt(v, 10, 64)
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
return i, true
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|
|
|
|
// GetIndex extracts a required integer parameter from MCP tool arguments.
|
|
// It accepts both numeric (float64 from JSON) and string representations.
|
|
// This provides better UX for LLM callers that may naturally use strings
|
|
// for identifiers like issue/PR numbers.
|
|
func GetIndex(args map[string]any, key string) (int64, error) {
|
|
val, exists := args[key]
|
|
if !exists {
|
|
return 0, fmt.Errorf("%s is required", key)
|
|
}
|
|
|
|
if i, ok := ToInt64(val); ok {
|
|
return i, nil
|
|
}
|
|
|
|
if s, ok := val.(string); ok {
|
|
return 0, fmt.Errorf("%s must be a valid integer (got %q)", key, s)
|
|
}
|
|
|
|
return 0, fmt.Errorf("%s must be a number or numeric string", key)
|
|
}
|
|
|
|
// GetOptionalInt extracts an optional integer parameter from MCP tool arguments.
|
|
// Returns defaultVal if the key is missing or the value cannot be parsed.
|
|
// Accepts both float64 (JSON number) and string representations.
|
|
func GetOptionalInt(args map[string]any, key string, defaultVal int64) int64 {
|
|
val, exists := args[key]
|
|
if !exists {
|
|
return defaultVal
|
|
}
|
|
if i, ok := ToInt64(val); ok {
|
|
return i
|
|
}
|
|
return defaultVal
|
|
}
|