Skip to content

Commit bca2cda

Browse files
authored
fix(mcp): gracefully cast tool call params objects and return err instead of panic
1 parent a568ac1 commit bca2cda

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

pkg/mcp/resources.go

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,25 @@ func (s *Server) resourcesList(ctx context.Context, ctr mcp.CallToolRequest) (*m
110110
resourceListOptions := kubernetes.ResourceListOptions{
111111
AsTable: s.configuration.ListOutput.AsTable(),
112112
}
113+
113114
if labelSelector != nil {
114-
resourceListOptions.ListOptions.LabelSelector = labelSelector.(string)
115+
l, ok := labelSelector.(string)
116+
if !ok {
117+
return NewTextResult("", fmt.Errorf("labelSelector is not a string")), nil
118+
}
119+
resourceListOptions.ListOptions.LabelSelector = l
115120
}
116121
gvk, err := parseGroupVersionKind(ctr.GetArguments())
117122
if err != nil {
118123
return NewTextResult("", fmt.Errorf("failed to list resources, %s", err)), nil
119124
}
120-
ret, err := s.k.Derived(ctx).ResourcesList(ctx, gvk, namespace.(string), resourceListOptions)
125+
126+
ns, ok := namespace.(string)
127+
if !ok {
128+
return NewTextResult("", fmt.Errorf("namespace is not a string")), nil
129+
}
130+
131+
ret, err := s.k.Derived(ctx).ResourcesList(ctx, gvk, ns, resourceListOptions)
121132
if err != nil {
122133
return NewTextResult("", fmt.Errorf("failed to list resources: %v", err)), nil
123134
}
@@ -137,7 +148,18 @@ func (s *Server) resourcesGet(ctx context.Context, ctr mcp.CallToolRequest) (*mc
137148
if name == nil {
138149
return NewTextResult("", errors.New("failed to get resource, missing argument name")), nil
139150
}
140-
ret, err := s.k.Derived(ctx).ResourcesGet(ctx, gvk, namespace.(string), name.(string))
151+
152+
ns, ok := namespace.(string)
153+
if !ok {
154+
return NewTextResult("", fmt.Errorf("namespace is not a string")), nil
155+
}
156+
157+
n, ok := name.(string)
158+
if !ok {
159+
return NewTextResult("", fmt.Errorf("name is not a string")), nil
160+
}
161+
162+
ret, err := s.k.Derived(ctx).ResourcesGet(ctx, gvk, ns, n)
141163
if err != nil {
142164
return NewTextResult("", fmt.Errorf("failed to get resource: %v", err)), nil
143165
}
@@ -149,7 +171,13 @@ func (s *Server) resourcesCreateOrUpdate(ctx context.Context, ctr mcp.CallToolRe
149171
if resource == nil || resource == "" {
150172
return NewTextResult("", errors.New("failed to create or update resources, missing argument resource")), nil
151173
}
152-
resources, err := s.k.Derived(ctx).ResourcesCreateOrUpdate(ctx, resource.(string))
174+
175+
r, ok := resource.(string)
176+
if !ok {
177+
return NewTextResult("", fmt.Errorf("resource is not a string")), nil
178+
}
179+
180+
resources, err := s.k.Derived(ctx).ResourcesCreateOrUpdate(ctx, r)
153181
if err != nil {
154182
return NewTextResult("", fmt.Errorf("failed to create or update resources: %v", err)), nil
155183
}
@@ -173,7 +201,18 @@ func (s *Server) resourcesDelete(ctx context.Context, ctr mcp.CallToolRequest) (
173201
if name == nil {
174202
return NewTextResult("", errors.New("failed to delete resource, missing argument name")), nil
175203
}
176-
err = s.k.Derived(ctx).ResourcesDelete(ctx, gvk, namespace.(string), name.(string))
204+
205+
ns, ok := namespace.(string)
206+
if !ok {
207+
return NewTextResult("", fmt.Errorf("namespace is not a string")), nil
208+
}
209+
210+
n, ok := name.(string)
211+
if !ok {
212+
return NewTextResult("", fmt.Errorf("name is not a string")), nil
213+
}
214+
215+
err = s.k.Derived(ctx).ResourcesDelete(ctx, gvk, ns, n)
177216
if err != nil {
178217
return NewTextResult("", fmt.Errorf("failed to delete resource: %v", err)), nil
179218
}
@@ -189,7 +228,13 @@ func parseGroupVersionKind(arguments map[string]interface{}) (*schema.GroupVersi
189228
if kind == nil {
190229
return nil, errors.New("missing argument kind")
191230
}
192-
gv, err := schema.ParseGroupVersion(apiVersion.(string))
231+
232+
a, ok := apiVersion.(string)
233+
if !ok {
234+
return nil, fmt.Errorf("name is not a string")
235+
}
236+
237+
gv, err := schema.ParseGroupVersion(a)
193238
if err != nil {
194239
return nil, errors.New("invalid argument apiVersion")
195240
}

0 commit comments

Comments
 (0)