1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: LicenseRef-MutuaL-1.2
4
5package mcp
6
7import (
8 "context"
9 "fmt"
10 "strings"
11
12 sdk "github.com/modelcontextprotocol/go-sdk/mcp"
13)
14
15func (s *Server) callDraftSaveRecipeTool(
16 ctx context.Context,
17 arguments SaveRecipeArguments,
18) (*sdk.CallToolResult, SaveRecipeOutput, error) {
19 draftID := strings.TrimSpace(arguments.DraftID)
20 if draftID == "" {
21 return nil, SaveRecipeOutput{}, fmt.Errorf("draft_id is required when source is draft")
22 }
23
24 markdown := arguments.Markdown
25 portions := arguments.Portions
26 hasMarkdown := strings.TrimSpace(markdown) != ""
27 hasPortions := portions >= 1
28 if hasMarkdown != hasPortions {
29 return nil, SaveRecipeOutput{}, fmt.Errorf(
30 "markdown and portions must be provided together when source is draft",
31 )
32 }
33 if !hasMarkdown {
34 content, err := s.backend.ReadRecipeContent(ctx, draftID)
35 if err != nil {
36 return nil, SaveRecipeOutput{}, err
37 }
38 markdown = content.Content
39 portions = content.Portions
40 }
41
42 recipeID, err := s.backend.SaveRecipeDraft(ctx, draftID, markdown, portions)
43 if err != nil {
44 return nil, SaveRecipeOutput{}, err
45 }
46 if strings.TrimSpace(recipeID) == "" {
47 return nil, SaveRecipeOutput{}, fmt.Errorf("save recipe draft response missing recipe ID")
48 }
49
50 return newRecipeSaveResult(recipeID)
51}