Skip to content

Commit cf58f7c

Browse files
implements new helpers to support google/go-github v77 API
1 parent e4847a3 commit cf58f7c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

pkg/github/server.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ func RequiredInt(r mcp.CallToolRequest, p string) (int, error) {
9999
return int(v), nil
100100
}
101101

102+
// RequiredBigInt is a helper function that can be used to fetch a requested parameter from the request.
103+
// It does the following checks:
104+
// 1. Checks if the parameter is present in the request.
105+
// 2. Checks if the parameter is of the expected type.
106+
// 3. Checks if the parameter is not empty, i.e: non-zero value
107+
func RequiredBigInt(r mcp.CallToolRequest, p string) (int64, error) {
108+
v, err := RequiredParam[float64](r, p)
109+
if err != nil {
110+
return 0, err
111+
}
112+
return int64(v), nil
113+
}
114+
102115
// OptionalParam is a helper function that can be used to fetch a requested parameter from the request.
103116
// It does the following checks:
104117
// 1. Checks if the parameter is present in the request, if not, it returns its zero-value
@@ -189,6 +202,50 @@ func OptionalStringArrayParam(r mcp.CallToolRequest, p string) ([]string, error)
189202
}
190203
}
191204

205+
func convertStringSliceToInt64Slice(s []string) []int64 {
206+
int64Slice := make([]int64, len(s))
207+
for i, str := range s {
208+
int64Slice[i] = convertStringToInt64(str)
209+
}
210+
return int64Slice
211+
}
212+
213+
func convertStringToInt64(s string) int64 {
214+
var i int64
215+
fmt.Sscan(s, &i)
216+
return i
217+
}
218+
219+
// OptionalStringArrayParam is a helper function that can be used to fetch a requested parameter from the request.
220+
// It does the following checks:
221+
// 1. Checks if the parameter is present in the request, if not, it returns its zero-value
222+
// 2. If it is present, iterates the elements and checks each is a string
223+
func OptionalBigIntArrayParam(r mcp.CallToolRequest, p string) ([]int64, error) {
224+
// Check if the parameter is present in the request
225+
if _, ok := r.GetArguments()[p]; !ok {
226+
return []int64{}, nil
227+
}
228+
229+
switch v := r.GetArguments()[p].(type) {
230+
case nil:
231+
return []int64{}, nil
232+
case []string:
233+
return convertStringSliceToInt64Slice(v), nil
234+
case []any:
235+
int64Slice := make([]int64, len(v))
236+
for i, v := range v {
237+
s, ok := v.(string)
238+
if !ok {
239+
return []int64{}, fmt.Errorf("parameter %s is not of type string, is %T", p, v)
240+
}
241+
int64Slice[i] = convertStringToInt64(s)
242+
}
243+
return int64Slice, nil
244+
default:
245+
return []int64{}, fmt.Errorf("parameter %s could not be coerced to []int64, is %T", p, r.GetArguments()[p])
246+
}
247+
}
248+
192249
// WithPagination adds REST API pagination parameters to a tool.
193250
// https://docs.github.com/en/rest/using-the-rest-api/using-pagination-in-the-rest-api
194251
func WithPagination() mcp.ToolOption {

0 commit comments

Comments
 (0)