// Copyright 2025 eat-pray-ai & OpenWaygate
// SPDX-License-Identifier: Apache-2.0
package channel
import (
"bytes"
"context"
"encoding/json"
"io"
"log/slog"
"time"
"github.com/eat-pray-ai/yutu/cmd"
"github.com/eat-pray-ai/yutu/pkg"
"github.com/eat-pray-ai/yutu/pkg/channel"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/spf13/cobra"
)
const (
updateTool = "channel-update"
updateShort = "Update channel's info"
updateLong = "Update channel's info, such as title, description, etc"
updateIdUsage = "ID of the channel to update"
)
type updateIn struct {
Ids []string `json:"ids"`
Country string `json:"country"`
CustomUrl string `json:"customUrl"`
DefaultLanguage string `json:"defaultLanguage"`
Description string `json:"description"`
Title string `json:"title"`
Output string `json:"output"`
Jsonpath string `json:"jsonpath"`
}
var updateInSchema = &jsonschema.Schema{
Type: "object",
Required: []string{"ids"},
Properties: map[string]*jsonschema.Schema{
"ids": {
Type: "array", Items: &jsonschema.Schema{
Type: "string",
},
Description: updateIdUsage,
Default: json.RawMessage(`[]`),
},
"country": {
Type: "string", Description: countryUsage,
Default: json.RawMessage(`""`),
},
"customUrl": {
Type: "string", Description: curlUsage,
Default: json.RawMessage(`""`),
},
"defaultLanguage": {
Type: "string", Description: dlUsage,
Default: json.RawMessage(`""`),
},
"description": {
Type: "string", Description: descUsage,
Default: json.RawMessage(`""`),
},
"title": {
Type: "string", Description: titleUsage,
Default: json.RawMessage(`""`),
},
"output": {
Type: "string", Enum: []any{"json", "yaml", "silent", ""},
Description: pkg.SilentUsage, Default: json.RawMessage(`"yaml"`),
},
"jsonpath": {
Type: "string", Description: pkg.JPUsage,
Default: json.RawMessage(`""`),
},
},
}
func init() {
mcp.AddTool(
cmd.Server, &mcp.Tool{
Name: updateTool, Title: updateShort, Description: updateLong,
InputSchema: updateInSchema, Annotations: &mcp.ToolAnnotations{
DestructiveHint: jsonschema.Ptr(false),
IdempotentHint: true,
OpenWorldHint: jsonschema.Ptr(true),
ReadOnlyHint: false,
},
}, updateHandler,
)
channelCmd.AddCommand(updateCmd)
updateCmd.Flags().StringSliceVarP(&ids, "id", "i", []string{}, updateIdUsage)
updateCmd.Flags().StringVarP(&country, "country", "c", "", countryUsage)
updateCmd.Flags().StringVarP(&customUrl, "customUrl", "u", "", curlUsage)
updateCmd.Flags().StringVarP(
&defaultLanguage, "defaultLanguage", "l", "", dlUsage,
)
updateCmd.Flags().StringVarP(
&description, "description", "d", "", descUsage,
)
updateCmd.Flags().StringVarP(&title, "title", "t", "", titleUsage)
updateCmd.Flags().StringVarP(&output, "output", "o", "", pkg.SilentUsage)
updateCmd.Flags().StringVarP(&jpath, "jsonpath", "j", "", pkg.JPUsage)
_ = updateCmd.MarkFlagRequired("id")
}
var updateCmd = &cobra.Command{
Use: "update",
Short: updateShort,
Long: updateLong,
Run: func(cmd *cobra.Command, args []string) {
err := update(cmd.OutOrStdout())
if err != nil {
_ = cmd.Help()
cmd.PrintErrf("Error: %v\n", err)
}
},
}
func updateHandler(
ctx context.Context, req *mcp.CallToolRequest, input updateIn,
) (*mcp.CallToolResult, any, error) {
logger := slog.New(
mcp.NewLoggingHandler(
req.Session,
&mcp.LoggingHandlerOptions{
LoggerName: updateTool, MinInterval: time.Second,
},
),
)
ids = input.Ids
country = input.Country
customUrl = input.CustomUrl
defaultLanguage = input.DefaultLanguage
description = input.Description
title = input.Title
output = input.Output
jpath = input.Jsonpath
var writer bytes.Buffer
err := update(&writer)
if err != nil {
logger.ErrorContext(ctx, err.Error(), "input", input)
return nil, nil, err
}
return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: writer.String()}}}, nil, nil
}
func update(writer io.Writer) error {
c := channel.NewChannel(
channel.WithIDs(ids),
channel.WithCountry(country),
channel.WithCustomUrl(customUrl),
channel.WithDefaultLanguage(defaultLanguage),
channel.WithDescription(description),
channel.WithTitle(title),
channel.WithMaxResults(1),
channel.WithService(nil),
)
return c.Update(output, jpath, writer)
}