Skip to content

Commit dbf501b

Browse files
committed
Remove constraint parsing - too complicated
1 parent 4f61a2d commit dbf501b

File tree

3 files changed

+169
-164
lines changed

3 files changed

+169
-164
lines changed

pkg/github/pullrequests.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ func CreateAndSubmitPullRequestReview(getGQLClient GetGQLClientFn, t translation
10781078
return mcp.NewToolResultError(err.Error()), nil
10791079
}
10801080

1081-
pullNumber, err := requiredParam[constrainableInt32](request, "pullNumber")
1081+
pullNumber, err := RequiredInt32Param(request, "pullNumber")
10821082
if err != nil {
10831083
return mcp.NewToolResultError(err.Error()), nil
10841084
}
@@ -1189,7 +1189,7 @@ func CreatePendingPullRequestReview(getGQLClient GetGQLClientFn, t translations.
11891189
return mcp.NewToolResultError(err.Error()), nil
11901190
}
11911191

1192-
pullNumber, err := requiredParam[constrainableInt32](request, "pullNumber")
1192+
pullNumber, err := RequiredInt32Param(request, "pullNumber")
11931193
if err != nil {
11941194
return mcp.NewToolResultError(err.Error()), nil
11951195
}
@@ -1316,7 +1316,7 @@ func AddPullRequestReviewCommentToPendingReview(getGQLClient GetGQLClientFn, t t
13161316
return mcp.NewToolResultError(err.Error()), nil
13171317
}
13181318

1319-
pullNumber, err := requiredParam[constrainableInt32](request, "pullNumber")
1319+
pullNumber, err := RequiredInt32Param(request, "pullNumber")
13201320
if err != nil {
13211321
return mcp.NewToolResultError(err.Error()), nil
13221322
}
@@ -1336,7 +1336,7 @@ func AddPullRequestReviewCommentToPendingReview(getGQLClient GetGQLClientFn, t t
13361336
return mcp.NewToolResultError(err.Error()), nil
13371337
}
13381338

1339-
line, err := OptionalParam[constrainableInt32](request, "line")
1339+
line, err := OptionalInt32Param(request, "line")
13401340
if err != nil {
13411341
return mcp.NewToolResultError(err.Error()), nil
13421342
}
@@ -1346,7 +1346,7 @@ func AddPullRequestReviewCommentToPendingReview(getGQLClient GetGQLClientFn, t t
13461346
return mcp.NewToolResultError(err.Error()), nil
13471347
}
13481348

1349-
startLine, err := OptionalParam[constrainableInt32](request, "startLine")
1349+
startLine, err := OptionalInt32Param(request, "startLine")
13501350
if err != nil {
13511351
return mcp.NewToolResultError(err.Error()), nil
13521352
}
@@ -1487,7 +1487,7 @@ func SubmitPendingPullRequestReview(getGQLClient GetGQLClientFn, t translations.
14871487
return mcp.NewToolResultError(err.Error()), nil
14881488
}
14891489

1490-
pullNumber, err := requiredParam[constrainableInt32](request, "pullNumber")
1490+
pullNumber, err := RequiredInt32Param(request, "pullNumber")
14911491
if err != nil {
14921492
return mcp.NewToolResultError(err.Error()), nil
14931493
}
@@ -1628,7 +1628,7 @@ func DeletePendingPullRequestReview(getGQLClient GetGQLClientFn, t translations.
16281628
return mcp.NewToolResultError(err.Error()), nil
16291629
}
16301630

1631-
pullNumber, err := requiredParam[constrainableInt32](request, "pullNumber")
1631+
pullNumber, err := RequiredInt32Param(request, "pullNumber")
16321632
if err != nil {
16331633
return mcp.NewToolResultError(err.Error()), nil
16341634
}

pkg/github/server.go

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
5635
func 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.
180178
func OptionalIntParamWithDefault(r mcp.CallToolRequest, p string, d int) (int, error) {

0 commit comments

Comments
 (0)