server_save_draft.go

 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 := strings.TrimSpace(arguments.RecipeID)
43	if recipeID != "" {
44		if err := validateRecipeID(recipeID); err != nil {
45			return nil, SaveRecipeOutput{}, err
46		}
47		if err := s.backend.UpdateRecipeContent(ctx, recipeID, markdown, portions); err != nil {
48			return nil, SaveRecipeOutput{}, err
49		}
50
51		return newRecipeOverwriteResult(recipeID)
52	}
53
54	savedRecipeID, err := s.backend.SaveRecipeDraft(ctx, draftID, markdown, portions)
55	if err != nil {
56		return nil, SaveRecipeOutput{}, err
57	}
58	if strings.TrimSpace(savedRecipeID) == "" {
59		return nil, SaveRecipeOutput{}, fmt.Errorf("save recipe draft response missing recipe ID")
60	}
61
62	return newRecipeSaveResult(savedRecipeID)
63}