141 lines
4.6 KiB
Go
141 lines
4.6 KiB
Go
// Copyright 2025 eat-pray-ai & OpenWaygate
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package subscription
|
|
|
|
import (
|
|
"encoding/json/jsontext"
|
|
"io"
|
|
|
|
cobramcp "github.com/eat-pray-ai/cobra-mcp"
|
|
"github.com/eat-pray-ai/yutu/cmd"
|
|
"github.com/eat-pray-ai/yutu/pkg"
|
|
"github.com/eat-pray-ai/yutu/pkg/subscription"
|
|
"github.com/eat-pray-ai/yutu/pkg/utils"
|
|
"github.com/google/jsonschema-go/jsonschema"
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
const (
|
|
listTool = "subscription-list"
|
|
listIdsUsage = "Return the subscriptions with the given ids for Stubby or Apiary"
|
|
listCidUsage = "Return the subscriptions of the given channel owner"
|
|
listShort = "List subscriptions"
|
|
listLong = "List subscriptions. Use this tool to list subscriptions."
|
|
listExample = `# List my subscriptions
|
|
yutu subscription list --for mine
|
|
# List subscriptions by channel ID with limit
|
|
yutu subscription list --channelId UC_x5XG1OV2P6uZZ5FSM9Ttw --maxResults 10
|
|
# List specific subscriptions in JSON format
|
|
yutu subscription list --ids abc123,def456 --output json
|
|
# List subscriptions for a channel alphabetically
|
|
yutu subscription list --forChannelId UC_x5XG1OV2P6uZZ5FSM9Ttw --order alphabetical`
|
|
)
|
|
|
|
var listInSchema = &jsonschema.Schema{
|
|
Type: "object",
|
|
Required: []string{},
|
|
Properties: map[string]*jsonschema.Schema{
|
|
"ids": {
|
|
Type: "array", Description: listIdsUsage,
|
|
Items: &jsonschema.Schema{Type: "string"},
|
|
},
|
|
"channel_id": {Type: "string", Description: listCidUsage},
|
|
"for_channel_id": {Type: "string", Description: fcidUsage},
|
|
"max_results": {
|
|
Type: "number", Description: pkg.MRUsage,
|
|
Default: jsontext.Value("5"),
|
|
Minimum: new(float64(0)),
|
|
},
|
|
"for": {
|
|
Type: "string", Description: forUsage,
|
|
Enum: []any{"mine", "myRecentSubscribers", "mySubscribers"},
|
|
},
|
|
"on_behalf_of_content_owner": {
|
|
Type: "string",
|
|
Description: pkg.OBOCOUsage,
|
|
},
|
|
"on_behalf_of_content_owner_channel": {
|
|
Type: "string",
|
|
Description: pkg.OBOCOCUsage,
|
|
},
|
|
"order": {
|
|
Type: "string", Description: orderUsage,
|
|
Enum: []any{
|
|
"subscriptionOrderUnspecified", "relevance", "unread", "alphabetical",
|
|
},
|
|
Default: jsontext.Value(`"relevance"`),
|
|
},
|
|
"parts": {
|
|
Type: "array", Description: pkg.PartsUsage,
|
|
Items: &jsonschema.Schema{Type: "string"},
|
|
Default: jsontext.Value(`["id","snippet"]`),
|
|
},
|
|
"output": {
|
|
Type: "string", Enum: []any{"json", "yaml", "table"},
|
|
Description: pkg.TableUsage, Default: jsontext.Value(`"json"`),
|
|
},
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
mcp.AddTool(
|
|
cmd.Server, &mcp.Tool{
|
|
Name: listTool, Title: listShort, Description: listLong,
|
|
InputSchema: listInSchema, Annotations: &mcp.ToolAnnotations{
|
|
DestructiveHint: new(false),
|
|
IdempotentHint: true,
|
|
OpenWorldHint: new(true),
|
|
ReadOnlyHint: true,
|
|
},
|
|
}, cobramcp.GenToolHandler(
|
|
listTool, func(input subscription.Subscription, writer io.Writer) error {
|
|
return input.List(writer)
|
|
},
|
|
),
|
|
)
|
|
subscriptionCmd.AddCommand(listCmd)
|
|
|
|
listCmd.Flags().StringSliceVarP(&ids, "ids", "i", []string{}, listIdsUsage)
|
|
listCmd.Flags().StringVarP(&channelId, "channelId", "c", "", listCidUsage)
|
|
listCmd.Flags().StringVarP(&forChannelId, "forChannelId", "C", "", fcidUsage)
|
|
listCmd.Flags().Int64VarP(&maxResults, "maxResults", "n", 5, pkg.MRUsage)
|
|
listCmd.Flags().StringVarP(&subscriptionFor, "for", "F", "", forUsage)
|
|
listCmd.Flags().StringVarP(
|
|
&onBehalfOfContentOwner, "onBehalfOfContentOwner", "b", "", pkg.OBOCOUsage,
|
|
)
|
|
listCmd.Flags().StringVarP(
|
|
&onBehalfOfContentOwnerChannel, "onBehalfOfContentOwnerChannel", "B", "",
|
|
pkg.OBOCOCUsage,
|
|
)
|
|
listCmd.Flags().StringVarP(&order, "order", "O", "relevance", orderUsage)
|
|
listCmd.Flags().StringSliceVarP(
|
|
&parts, "parts", "p", []string{"id", "snippet"}, pkg.PartsUsage,
|
|
)
|
|
listCmd.Flags().StringP("output", "o", "table", pkg.TableUsage)
|
|
}
|
|
|
|
var listCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: listShort,
|
|
Long: listLong,
|
|
Example: listExample,
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
|
output, _ := cmd.Flags().GetString("output")
|
|
input := subscription.NewSubscription(
|
|
subscription.WithIds(ids),
|
|
subscription.WithChannelId(channelId),
|
|
subscription.WithForChannelId(forChannelId),
|
|
subscription.WithMaxResults(maxResults),
|
|
subscription.WithFor(subscriptionFor),
|
|
subscription.WithOnBehalfOfContentOwner(onBehalfOfContentOwner),
|
|
subscription.WithOnBehalfOfContentOwnerChannel(onBehalfOfContentOwnerChannel),
|
|
subscription.WithOrder(order),
|
|
subscription.WithParts(parts),
|
|
subscription.WithOutput(output),
|
|
)
|
|
utils.HandleCmdError(input.List(cmd.OutOrStdout()), cmd)
|
|
},
|
|
}
|