|
| 1 | +package harness |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + |
| 10 | + "github.com/harness/harness-mcp/client" |
| 11 | + "github.com/harness/harness-mcp/client/dto" |
| 12 | + "github.com/harness/harness-mcp/cmd/harness-mcp-server/config" |
| 13 | + "github.com/mark3labs/mcp-go/mcp" |
| 14 | + "github.com/mark3labs/mcp-go/server" |
| 15 | +) |
| 16 | + |
| 17 | +func AIDevOpsAgentTool(config *config.Config, client *client.GenaiService) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 18 | + return mcp.NewTool("ask_ai_devops_agent", |
| 19 | + mcp.WithDescription("Send a request to the Harness AI Devops agent to generate harness entities based on the provided action type and prompt."), |
| 20 | + mcp.WithString("prompt", |
| 21 | + mcp.Required(), |
| 22 | + mcp.Description("The prompt to send to the genai service"), |
| 23 | + ), |
| 24 | + mcp.WithString("action", |
| 25 | + mcp.Required(), |
| 26 | + mcp.Description("The action type to perform (CREATE_STEP, UPDATE_STEP, CREATE_STAGE, etc.)"), |
| 27 | + ), |
| 28 | + mcp.WithString("conversation_id", |
| 29 | + mcp.Description("Optional conversation ID to maintain conversation context (if not provided, a new ID will be generated)"), |
| 30 | + ), |
| 31 | + mcp.WithString("interaction_id", |
| 32 | + mcp.Description("Optional interaction ID for tracking purposes (if not provided, a new ID will be generated)"), |
| 33 | + ), |
| 34 | + mcp.WithArray("context", |
| 35 | + mcp.Description("Optional context information for the request"), |
| 36 | + mcp.Items(map[string]any{ |
| 37 | + "type": "object", |
| 38 | + "properties": map[string]any{ |
| 39 | + "type": map[string]any{ |
| 40 | + "type": "string", |
| 41 | + "description": "The type of context item", |
| 42 | + }, |
| 43 | + "payload": map[string]any{ |
| 44 | + "description": "The payload for this context item", |
| 45 | + }, |
| 46 | + }, |
| 47 | + "required": []string{"type", "payload"}, |
| 48 | + }), |
| 49 | + ), |
| 50 | + mcp.WithArray("conversation_raw", |
| 51 | + mcp.Description("Optional conversation history for context"), |
| 52 | + mcp.Items(map[string]any{ |
| 53 | + "type": "object", |
| 54 | + "properties": map[string]any{ |
| 55 | + "role": map[string]any{ |
| 56 | + "type": "string", |
| 57 | + "description": "The role of the message sender (e.g., 'user', 'assistant')", |
| 58 | + }, |
| 59 | + "content": map[string]any{ |
| 60 | + "type": "string", |
| 61 | + "description": "The content of the conversation message", |
| 62 | + }, |
| 63 | + }, |
| 64 | + "required": []string{"role", "content"}, |
| 65 | + }), |
| 66 | + ), |
| 67 | + WithScope(config, false), |
| 68 | + ), |
| 69 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 70 | + // Extract required parameters |
| 71 | + prompt, err := requiredParam[string](request, "prompt") |
| 72 | + if err != nil { |
| 73 | + return mcp.NewToolResultError(err.Error()), nil |
| 74 | + } |
| 75 | + |
| 76 | + action, err := requiredParam[string](request, "action") |
| 77 | + if err != nil { |
| 78 | + return mcp.NewToolResultError(err.Error()), nil |
| 79 | + } |
| 80 | + |
| 81 | + scope, err := fetchScope(config, request, false) |
| 82 | + if err != nil { |
| 83 | + return mcp.NewToolResultError(err.Error()), nil |
| 84 | + } |
| 85 | + |
| 86 | + // Extract optional parameters |
| 87 | + conversationID, _ := OptionalParam[string](request, "conversation_id") |
| 88 | + interactionID, _ := OptionalParam[string](request, "interaction_id") |
| 89 | + contextRaw, _ := OptionalParam[[]any](request, "context") |
| 90 | + conversationRaw, _ := OptionalParam[[]any](request, "conversation_raw") |
| 91 | + |
| 92 | + // Convert context items |
| 93 | + var contextItems []dto.ContextItem |
| 94 | + for _, ctxRaw := range contextRaw { |
| 95 | + if ctxMap, ok := ctxRaw.(map[string]interface{}); ok { |
| 96 | + ctxType, _ := ctxMap["type"].(string) |
| 97 | + ctxPayload := ctxMap["payload"] |
| 98 | + contextItems = append(contextItems, dto.ContextItem{ |
| 99 | + Type: ctxType, |
| 100 | + Payload: ctxPayload, |
| 101 | + }) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Create harness context from scope |
| 106 | + harnessContext := &dto.HarnessContext{ |
| 107 | + AccountID: scope.AccountID, |
| 108 | + OrgID: scope.OrgID, |
| 109 | + ProjectID: scope.ProjectID, |
| 110 | + } |
| 111 | + |
| 112 | + // Generate or use provided IDs |
| 113 | + var finalConversationID, finalInteractionID string |
| 114 | + |
| 115 | + if conversationID != "" { |
| 116 | + finalConversationID = conversationID |
| 117 | + } else { |
| 118 | + finalConversationID = uuid.New().String() |
| 119 | + } |
| 120 | + |
| 121 | + if interactionID != "" { |
| 122 | + finalInteractionID = interactionID |
| 123 | + } else { |
| 124 | + finalInteractionID = uuid.New().String() |
| 125 | + } |
| 126 | + |
| 127 | + // Create genai request |
| 128 | + genaiRequest := &dto.ServiceChatParameters{ |
| 129 | + Prompt: prompt, |
| 130 | + ConversationID: finalConversationID, |
| 131 | + InteractionID: finalInteractionID, |
| 132 | + ConversationRaw: conversationRaw, |
| 133 | + Context: contextItems, |
| 134 | + Action: dto.RequestAction(strings.ToUpper(action)), |
| 135 | + HarnessContext: harnessContext, |
| 136 | + Stream: false, |
| 137 | + } |
| 138 | + |
| 139 | + // Send the request to the genai service |
| 140 | + response, err := client.SendAIDevOpsChat(ctx, scope, genaiRequest) |
| 141 | + if err != nil { |
| 142 | + return nil, fmt.Errorf("failed to send request to genai service: %w", err) |
| 143 | + } |
| 144 | + |
| 145 | + // Check for errors in response |
| 146 | + if response.Error != "" { |
| 147 | + return mcp.NewToolResultError(response.Error), nil |
| 148 | + } |
| 149 | + |
| 150 | + // Format capabilities if present |
| 151 | + var capabilitiesText string |
| 152 | + if len(response.CapabilitiesToRun) > 0 { |
| 153 | + capabilitiesText = "\n\nCapabilities to run:\n" |
| 154 | + for _, cap := range response.CapabilitiesToRun { |
| 155 | + capabilitiesText += fmt.Sprintf("\n- Type: %s\n CallID: %s", cap.Type, cap.CallID) |
| 156 | + |
| 157 | + // Include input details if available |
| 158 | + if len(cap.Input) > 0 { |
| 159 | + capabilitiesText += "\n Input:" |
| 160 | + for k, v := range cap.Input { |
| 161 | + capabilitiesText += fmt.Sprintf("\n %s: %v", k, v) |
| 162 | + } |
| 163 | + } |
| 164 | + capabilitiesText += "\n" |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + // Combine any response text with capabilities and usage info |
| 169 | + resultText := "" |
| 170 | + if response.Response != "" { |
| 171 | + resultText = response.Response |
| 172 | + } else if response.ConversationRaw != "" { |
| 173 | + resultText = response.ConversationRaw |
| 174 | + } |
| 175 | + |
| 176 | + // Create the full result text |
| 177 | + fullResult := fmt.Sprintf("%s%s", resultText, capabilitiesText) |
| 178 | + |
| 179 | + return mcp.NewToolResultText(fullResult), nil |
| 180 | + } |
| 181 | +} |
0 commit comments