@@ -30,27 +30,6 @@ func NewServer(version string, opts ...server.ServerOption) *server.MCPServer {
3030 return s
3131}
3232
33- type constrainableValue interface {
34- Constrain (any ) error
35- }
36-
37- type constrainableInt32 int32
38-
39- func (ci * constrainableInt32 ) Constrain (param any ) error {
40- i , ok := param .(float64 )
41- if ! ok {
42- return fmt .Errorf ("parameter is not of type float, is %T" , param )
43- }
44-
45- // Check if the parameter is within the int32 range
46- if i < math .MinInt32 || i > math .MaxInt32 {
47- return fmt .Errorf ("parameter is out of int32 range" )
48- }
49-
50- * ci = constrainableInt32 (i )
51- return nil
52- }
53-
5433// OptionalParamOK is a helper function that can be used to fetch a requested parameter from the request.
5534// It returns the value, a boolean indicating if the parameter was present, and an error if the type is wrong.
5635func OptionalParamOK [T any ](r mcp.CallToolRequest , p string ) (value T , ok bool , err error ) {
@@ -95,23 +74,13 @@ func requiredParam[T comparable](r mcp.CallToolRequest, p string) (T, error) {
9574 return zero , fmt .Errorf ("missing required parameter: %s" , p )
9675 }
9776
98- // Check whether our parameter is something that can be parsed. It is expected that the ParseParam method
99- // sets the receiver's value to the parsed value.
100- var parsableParamResult T
101- if constrainableValue , ok := any (& parsableParamResult ).(constrainableValue ); ok {
102- if err := constrainableValue .Constrain (param ); err != nil {
103- return zero , fmt .Errorf ("failed to parse parameter %s: %w" , p , err )
104- }
105- return parsableParamResult , nil
106- }
107-
10877 // Check if the parameter is of the expected type
109- typedParam , ok := r . Params . Arguments [ p ] .(T )
78+ typedParam , ok := param .(T )
11079 if ! ok {
11180 return zero , fmt .Errorf ("parameter %s is not of type %T" , p , zero )
11281 }
11382
114- if r . Params . Arguments [ p ].( T ) == zero {
83+ if typedParam == zero {
11584 return zero , fmt .Errorf ("missing required parameter: %s" , p )
11685 }
11786
@@ -131,6 +100,26 @@ func RequiredInt(r mcp.CallToolRequest, p string) (int, error) {
131100 return int (v ), nil
132101}
133102
103+ // RequiredInt is a helper function that can be used to fetch a requested parameter from the request.
104+ // It does the following checks:
105+ // 1. Checks if the parameter is present in the request.
106+ // 2. Checks if the parameter is of the expected type.
107+ // 3. Checks if the parameter is not empty, i.e: non-zero value
108+ // 4. Checks if the parameter is within the int32 range
109+ func RequiredInt32Param (r mcp.CallToolRequest , p string ) (int32 , error ) {
110+ v , err := RequiredInt (r , p )
111+ if err != nil {
112+ return 0 , err
113+ }
114+
115+ // Check if the parameter is within the int32 range
116+ if v < math .MinInt32 || v > math .MaxInt32 {
117+ return 0 , fmt .Errorf ("parameter %s is out of int32 range" , p )
118+ }
119+
120+ return int32 (v ), nil
121+ }
122+
134123// OptionalParam is a helper function that can be used to fetch a requested parameter from the request.
135124// It does the following checks:
136125// 1. Checks if the parameter is present in the request, if not, it returns its zero-value
@@ -144,20 +133,10 @@ func OptionalParam[T any](r mcp.CallToolRequest, p string) (T, error) {
144133 return zero , nil
145134 }
146135
147- // Check whether our parameter is something that can be parsed. It is expected that the ParseParam method
148- // sets the receiver's value to the parsed value.
149- var parsableParamResult T
150- if parseableParam , ok := any (& parsableParamResult ).(constrainableValue ); ok {
151- if err := parseableParam .Constrain (param ); err != nil {
152- return zero , fmt .Errorf ("failed to parse parameter %s: %w" , p , err )
153- }
154- return parsableParamResult , nil
155- }
156-
157136 // Check if the parameter is of the expected type
158137 typedParam , ok := param .(T )
159138 if ! ok {
160- return zero , fmt .Errorf ("parameter %s is not of type %T, is %T" , p , parsableParamResult , r .Params .Arguments [p ])
139+ return zero , fmt .Errorf ("parameter %s is not of type %T, is %T" , p , zero , r .Params .Arguments [p ])
161140 }
162141
163142 return typedParam , nil
@@ -175,6 +154,25 @@ func OptionalIntParam(r mcp.CallToolRequest, p string) (int, error) {
175154 return int (v ), nil
176155}
177156
157+ // OptionalInt32Param is a helper function that can be used to fetch a requested parameter from the request.
158+ // It does the following checks:
159+ // 1. Checks if the parameter is present in the request, if not, it returns its zero-value
160+ // 2. If it is present, it checks if the parameter is of the expected type
161+ // 3. Checks if the parameter is within the int32 range
162+ func OptionalInt32Param (r mcp.CallToolRequest , p string ) (int32 , error ) {
163+ v , err := OptionalIntParam (r , p )
164+ if err != nil {
165+ return 0 , err
166+ }
167+
168+ // Check if the parameter is within the int32 range
169+ if v < math .MinInt32 || v > math .MaxInt32 {
170+ return 0 , fmt .Errorf ("parameter %s is out of int32 range" , p )
171+ }
172+
173+ return int32 (v ), nil
174+ }
175+
178176// OptionalIntParamWithDefault is a helper function that can be used to fetch a requested parameter from the request
179177// similar to optionalIntParam, but it also takes a default value.
180178func OptionalIntParamWithDefault (r mcp.CallToolRequest , p string , d int ) (int , error ) {
0 commit comments