@@ -3,6 +3,7 @@ package mcp
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
+ "github.com/spf13/cast"
6
7
)
7
8
8
9
// ClientRequest types
@@ -612,3 +613,105 @@ func ParseReadResourceResult(rawMessage *json.RawMessage) (*ReadResourceResult,
612
613
613
614
return & result , nil
614
615
}
616
+
617
+ func ParseArgument (request CallToolRequest , key string , defaultVal any ) any {
618
+ if _ , ok := request .Params .Arguments [key ]; ! ok {
619
+ return defaultVal
620
+ } else {
621
+ return request .Params .Arguments [key ]
622
+ }
623
+ }
624
+
625
+ // ParseBoolean extracts and converts a boolean parameter from a CallToolRequest.
626
+ // If the key is not found in the Arguments map, the defaultValue is returned.
627
+ // The function uses cast.ToBool for conversion which handles various string representations
628
+ // such as "true", "yes", "1", etc.
629
+ func ParseBoolean (request CallToolRequest , key string , defaultValue bool ) bool {
630
+ v := ParseArgument (request , key , defaultValue )
631
+ return cast .ToBool (v )
632
+ }
633
+
634
+ // ParseInt64 extracts and converts an int64 parameter from a CallToolRequest.
635
+ // If the key is not found in the Arguments map, the defaultValue is returned.
636
+ func ParseInt64 (request CallToolRequest , key string , defaultValue int64 ) int64 {
637
+ v := ParseArgument (request , key , defaultValue )
638
+ return cast .ToInt64 (v )
639
+ }
640
+
641
+ // ParseInt32 extracts and converts an int32 parameter from a CallToolRequest.
642
+ func ParseInt32 (request CallToolRequest , key string , defaultValue int32 ) int32 {
643
+ v := ParseArgument (request , key , defaultValue )
644
+ return cast .ToInt32 (v )
645
+ }
646
+
647
+ // ParseInt16 extracts and converts an int16 parameter from a CallToolRequest.
648
+ func ParseInt16 (request CallToolRequest , key string , defaultValue int16 ) int16 {
649
+ v := ParseArgument (request , key , defaultValue )
650
+ return cast .ToInt16 (v )
651
+ }
652
+
653
+ // ParseInt8 extracts and converts an int8 parameter from a CallToolRequest.
654
+ func ParseInt8 (request CallToolRequest , key string , defaultValue int8 ) int8 {
655
+ v := ParseArgument (request , key , defaultValue )
656
+ return cast .ToInt8 (v )
657
+ }
658
+
659
+ // ParseInt extracts and converts an int parameter from a CallToolRequest.
660
+ func ParseInt (request CallToolRequest , key string , defaultValue int ) int {
661
+ v := ParseArgument (request , key , defaultValue )
662
+ return cast .ToInt (v )
663
+ }
664
+
665
+ // ParseUInt extracts and converts an uint parameter from a CallToolRequest.
666
+ func ParseUInt (request CallToolRequest , key string , defaultValue uint ) uint {
667
+ v := ParseArgument (request , key , defaultValue )
668
+ return cast .ToUint (v )
669
+ }
670
+
671
+ // ParseUInt64 extracts and converts an uint64 parameter from a CallToolRequest.
672
+ func ParseUInt64 (request CallToolRequest , key string , defaultValue uint64 ) uint64 {
673
+ v := ParseArgument (request , key , defaultValue )
674
+ return cast .ToUint64 (v )
675
+ }
676
+
677
+ // ParseUInt32 extracts and converts an uint32 parameter from a CallToolRequest.
678
+ func ParseUInt32 (request CallToolRequest , key string , defaultValue uint32 ) uint32 {
679
+ v := ParseArgument (request , key , defaultValue )
680
+ return cast .ToUint32 (v )
681
+ }
682
+
683
+ // ParseUInt16 extracts and converts an uint16 parameter from a CallToolRequest.
684
+ func ParseUInt16 (request CallToolRequest , key string , defaultValue uint16 ) uint16 {
685
+ v := ParseArgument (request , key , defaultValue )
686
+ return cast .ToUint16 (v )
687
+ }
688
+
689
+ // ParseUInt8 extracts and converts an uint8 parameter from a CallToolRequest.
690
+ func ParseUInt8 (request CallToolRequest , key string , defaultValue uint8 ) uint8 {
691
+ v := ParseArgument (request , key , defaultValue )
692
+ return cast .ToUint8 (v )
693
+ }
694
+
695
+ // ParseFloat32 extracts and converts a float32 parameter from a CallToolRequest.
696
+ func ParseFloat32 (request CallToolRequest , key string , defaultValue float32 ) float32 {
697
+ v := ParseArgument (request , key , defaultValue )
698
+ return cast .ToFloat32 (v )
699
+ }
700
+
701
+ // ParseFloat64 extracts and converts a float64 parameter from a CallToolRequest.
702
+ func ParseFloat64 (request CallToolRequest , key string , defaultValue float64 ) float64 {
703
+ v := ParseArgument (request , key , defaultValue )
704
+ return cast .ToFloat64 (v )
705
+ }
706
+
707
+ // ParseString extracts and converts a string parameter from a CallToolRequest.
708
+ func ParseString (request CallToolRequest , key string , defaultValue string ) string {
709
+ v := ParseArgument (request , key , defaultValue )
710
+ return cast .ToString (v )
711
+ }
712
+
713
+ // ParseStringMap extracts and converts a string map parameter from a CallToolRequest.
714
+ func ParseStringMap (request CallToolRequest , key string , defaultValue map [string ]any ) map [string ]any {
715
+ v := ParseArgument (request , key , defaultValue )
716
+ return cast .ToStringMap (v )
717
+ }
0 commit comments