|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/containers/kubernetes-mcp-server/pkg/api" |
| 10 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 11 | + "k8s.io/utils/ptr" |
| 12 | +) |
| 13 | + |
| 14 | +func ServerToolToGoSdkTool(s *Server, tool api.ServerTool) (*mcp.Tool, mcp.ToolHandler, error) { |
| 15 | + goSdkTool := &mcp.Tool{ |
| 16 | + Name: tool.Tool.Name, |
| 17 | + Description: tool.Tool.Description, |
| 18 | + Title: tool.Tool.Annotations.Title, |
| 19 | + Annotations: &mcp.ToolAnnotations{ |
| 20 | + Title: tool.Tool.Annotations.Title, |
| 21 | + ReadOnlyHint: ptr.Deref(tool.Tool.Annotations.ReadOnlyHint, false), |
| 22 | + DestructiveHint: tool.Tool.Annotations.DestructiveHint, |
| 23 | + IdempotentHint: ptr.Deref(tool.Tool.Annotations.IdempotentHint, false), |
| 24 | + OpenWorldHint: tool.Tool.Annotations.OpenWorldHint, |
| 25 | + }, |
| 26 | + } |
| 27 | + if tool.Tool.InputSchema != nil { |
| 28 | + schema, err := json.Marshal(tool.Tool.InputSchema) |
| 29 | + if err != nil { |
| 30 | + return nil, nil, fmt.Errorf("failed to marshal tool input schema for tool %s: %v", tool.Tool.Name, err) |
| 31 | + } |
| 32 | + // TODO: temporary fix to append an empty properties object (some client have trouble parsing a schema without properties) |
| 33 | + // As opposed, Gemini had trouble for a while when properties was present but empty. |
| 34 | + // https://github.com/containers/kubernetes-mcp-server/issues/340 |
| 35 | + if string(schema) == `{"type":"object"}` { |
| 36 | + schema = []byte(`{"type":"object","properties":{}}`) |
| 37 | + } |
| 38 | + |
| 39 | + var fixedSchema map[string]interface{} |
| 40 | + if err := json.Unmarshal(schema, &fixedSchema); err != nil { |
| 41 | + return nil, nil, fmt.Errorf("failed to unmarshal tool input schema for tool %s: %v", tool.Tool.Name, err) |
| 42 | + } |
| 43 | + |
| 44 | + goSdkTool.InputSchema = fixedSchema |
| 45 | + } |
| 46 | + goSdkHandler := func(ctx context.Context, request *mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 47 | + toolCallRequest, err := GoSdkToolCallRequestToToolCallRequest(request) |
| 48 | + if err != nil { |
| 49 | + return nil, fmt.Errorf("%v for tool %s", err, tool.Tool.Name) |
| 50 | + } |
| 51 | + // get the correct derived Kubernetes client for the target specified in the request |
| 52 | + cluster := toolCallRequest.GetString(s.p.GetTargetParameterName(), s.p.GetDefaultTarget()) |
| 53 | + k, err := s.p.GetDerivedKubernetes(ctx, cluster) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + result, err := tool.Handler(api.ToolHandlerParams{ |
| 59 | + Context: ctx, |
| 60 | + Kubernetes: k, |
| 61 | + ToolCallRequest: toolCallRequest, |
| 62 | + ListOutput: s.configuration.ListOutput(), |
| 63 | + }) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + return NewTextResult(result.Content, result.Error), nil |
| 68 | + } |
| 69 | + return goSdkTool, goSdkHandler, nil |
| 70 | +} |
| 71 | + |
| 72 | +type ToolCallRequest struct { |
| 73 | + Name string |
| 74 | + arguments map[string]any |
| 75 | +} |
| 76 | + |
| 77 | +var _ api.ToolCallRequest = (*ToolCallRequest)(nil) |
| 78 | + |
| 79 | +func GoSdkToolCallRequestToToolCallRequest(request *mcp.CallToolRequest) (*ToolCallRequest, error) { |
| 80 | + toolCallParams, ok := request.GetParams().(*mcp.CallToolParamsRaw) |
| 81 | + if !ok { |
| 82 | + return nil, errors.New("invalid tool call parameters for tool call request") |
| 83 | + } |
| 84 | + return GoSdkToolCallParamsToToolCallRequest(toolCallParams) |
| 85 | +} |
| 86 | + |
| 87 | +func GoSdkToolCallParamsToToolCallRequest(toolCallParams *mcp.CallToolParamsRaw) (*ToolCallRequest, error) { |
| 88 | + var arguments map[string]any |
| 89 | + if err := json.Unmarshal(toolCallParams.Arguments, &arguments); err != nil { |
| 90 | + return nil, fmt.Errorf("failed to unmarshal tool call arguments: %v", err) |
| 91 | + } |
| 92 | + return &ToolCallRequest{ |
| 93 | + Name: toolCallParams.Name, |
| 94 | + arguments: arguments, |
| 95 | + }, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (ToolCallRequest *ToolCallRequest) GetArguments() map[string]any { |
| 99 | + return ToolCallRequest.arguments |
| 100 | +} |
| 101 | + |
| 102 | +func (ToolCallRequest *ToolCallRequest) GetString(key, defaultValue string) string { |
| 103 | + if value, ok := ToolCallRequest.arguments[key]; ok { |
| 104 | + if strValue, ok := value.(string); ok { |
| 105 | + return strValue |
| 106 | + } |
| 107 | + } |
| 108 | + return defaultValue |
| 109 | +} |
0 commit comments