|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + categoriesv1 "buf.build/gen/go/xskydev/go-money-pb/protocolbuffers/go/gomoneypb/categories/v1" |
| 8 | + gomoneypbv1 "buf.build/gen/go/xskydev/go-money-pb/protocolbuffers/go/gomoneypb/v1" |
| 9 | + "github.com/ft-t/go-money/pkg/database" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | +) |
| 12 | + |
| 13 | +func (s *Server) handleCreateCategory(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 14 | + args := request.GetArguments() |
| 15 | + |
| 16 | + name, ok := args["name"].(string) |
| 17 | + if !ok || name == "" { |
| 18 | + return mcp.NewToolResultError("name parameter is required"), nil |
| 19 | + } |
| 20 | + |
| 21 | + queryCtx, cancel := context.WithTimeout(ctx, queryTimeout) |
| 22 | + defer cancel() |
| 23 | + |
| 24 | + queryCtx = database.WithContext(queryCtx, s.db) |
| 25 | + |
| 26 | + resp, err := s.cfg.CategorySvc.CreateCategory(queryCtx, &categoriesv1.CreateCategoryRequest{ |
| 27 | + Category: &gomoneypbv1.Category{ |
| 28 | + Name: name, |
| 29 | + }, |
| 30 | + }) |
| 31 | + if err != nil { |
| 32 | + return mcp.NewToolResultError(fmt.Sprintf("failed to create category: %v", err)), nil |
| 33 | + } |
| 34 | + |
| 35 | + return mcp.NewToolResultText(fmt.Sprintf("Category created with id %d", resp.Category.Id)), nil |
| 36 | +} |
| 37 | + |
| 38 | +func (s *Server) handleUpdateCategory(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 39 | + args := request.GetArguments() |
| 40 | + |
| 41 | + categoryID, ok := args["id"].(float64) |
| 42 | + if !ok { |
| 43 | + return mcp.NewToolResultError("id parameter is required"), nil |
| 44 | + } |
| 45 | + |
| 46 | + name, ok := args["name"].(string) |
| 47 | + if !ok || name == "" { |
| 48 | + return mcp.NewToolResultError("name parameter is required"), nil |
| 49 | + } |
| 50 | + |
| 51 | + queryCtx, cancel := context.WithTimeout(ctx, queryTimeout) |
| 52 | + defer cancel() |
| 53 | + |
| 54 | + queryCtx = database.WithContext(queryCtx, s.db) |
| 55 | + |
| 56 | + resp, err := s.cfg.CategorySvc.UpdateCategory(queryCtx, &categoriesv1.UpdateCategoryRequest{ |
| 57 | + Category: &gomoneypbv1.Category{ |
| 58 | + Id: int32(categoryID), |
| 59 | + Name: name, |
| 60 | + }, |
| 61 | + }) |
| 62 | + if err != nil { |
| 63 | + return mcp.NewToolResultError(fmt.Sprintf("failed to update category: %v", err)), nil |
| 64 | + } |
| 65 | + |
| 66 | + return mcp.NewToolResultText(fmt.Sprintf("Category %d updated to '%s'", resp.Category.Id, resp.Category.Name)), nil |
| 67 | +} |
0 commit comments