175 lines
5.0 KiB
Go
175 lines
5.0 KiB
Go
// Copyright 2025 eat-pray-ai & OpenWaygate
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package activity
|
|
|
|
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/activity"
|
|
"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 = "activity-list"
|
|
)
|
|
|
|
type listIn struct {
|
|
ChannelId string `json:"channelId"`
|
|
Home *string `json:"home,omitempty"`
|
|
MaxResults int64 `json:"maxResults"`
|
|
Mine *string `json:"mine,omitempty"`
|
|
PublishedAfter string `json:"publishedAfter"`
|
|
PublishedBefore string `json:"publishedBefore"`
|
|
RegionCode string `json:"regionCode"`
|
|
Parts []string `json:"parts"`
|
|
Output string `json:"output"`
|
|
Jsonpath string `json:"jsonpath"`
|
|
}
|
|
|
|
var listInSchema = &jsonschema.Schema{
|
|
Type: "object",
|
|
// Required: []string{
|
|
// "channelId", "home", "maxResults", "mine", "publishedAfter",
|
|
// "publishedBefore", "regionCode", "parts", "output", "jsonpath",
|
|
// },
|
|
Properties: map[string]*jsonschema.Schema{
|
|
"channelId": {
|
|
Type: "string", Description: ciUsage, Default: json.RawMessage(`""`),
|
|
},
|
|
"home": {
|
|
Type: "string", Enum: []any{"true", "false", ""},
|
|
Description: homeUsage, Default: json.RawMessage(`""`),
|
|
},
|
|
"maxResults": {
|
|
Type: "number", Description: pkg.MRUsage,
|
|
Default: json.RawMessage("5"), Minimum: jsonschema.Ptr(float64(0)),
|
|
},
|
|
"mine": {
|
|
Type: "string", Enum: []any{"true", "false", ""},
|
|
Description: mineUsage, Default: json.RawMessage(`""`),
|
|
},
|
|
"publishedAfter": {
|
|
Type: "string", Description: paUsage, Default: json.RawMessage(`""`),
|
|
},
|
|
"publishedBefore": {
|
|
Type: "string", Description: pbUsage, Default: json.RawMessage(`""`),
|
|
},
|
|
"regionCode": {
|
|
Type: "string", Description: rcUsage, Default: json.RawMessage(`""`),
|
|
},
|
|
"parts": {
|
|
Type: "array", Description: pkg.PartsUsage,
|
|
Items: &jsonschema.Schema{Type: "string"},
|
|
Default: json.RawMessage(`["id","snippet","contentDetails"]`),
|
|
},
|
|
"output": {
|
|
Type: "string", Enum: []any{"json", "yaml", "table"},
|
|
Description: pkg.TableUsage, Default: json.RawMessage(`"yaml"`),
|
|
},
|
|
"jsonpath": {
|
|
Type: "string", Description: pkg.JPUsage, Default: json.RawMessage(`""`),
|
|
},
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
mcp.AddTool(
|
|
cmd.Server, &mcp.Tool{
|
|
Name: listTool, Title: short, Description: long,
|
|
InputSchema: listInSchema, Annotations: &mcp.ToolAnnotations{
|
|
DestructiveHint: jsonschema.Ptr(false),
|
|
IdempotentHint: true,
|
|
OpenWorldHint: jsonschema.Ptr(true),
|
|
ReadOnlyHint: true,
|
|
},
|
|
}, listHandler,
|
|
)
|
|
activityCmd.AddCommand(listCmd)
|
|
listCmd.Flags().StringVarP(&channelId, "channelId", "c", "", ciUsage)
|
|
listCmd.Flags().BoolVarP(home, "home", "H", true, homeUsage)
|
|
listCmd.Flags().Int64VarP(&maxResults, "maxResults", "n", 5, pkg.MRUsage)
|
|
listCmd.Flags().BoolVarP(mine, "mine", "M", true, mineUsage)
|
|
listCmd.Flags().StringVarP(
|
|
&publishedAfter, "publishedAfter", "a", "", paUsage,
|
|
)
|
|
listCmd.Flags().StringVarP(
|
|
&publishedBefore, "publishedBefore", "b", "", pbUsage,
|
|
)
|
|
listCmd.Flags().StringVarP(®ionCode, "regionCode", "r", "", rcUsage)
|
|
listCmd.Flags().StringSliceVarP(
|
|
&parts, "parts", "p", []string{"id", "snippet", "contentDetails"},
|
|
pkg.PartsUsage,
|
|
)
|
|
listCmd.Flags().StringVarP(&output, "output", "o", "table", pkg.TableUsage)
|
|
listCmd.Flags().StringVarP(&jpath, "jsonpath", "j", "", pkg.JPUsage)
|
|
}
|
|
|
|
var listCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: short,
|
|
Long: long,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
err := list(cmd.OutOrStdout())
|
|
if err != nil {
|
|
_ = cmd.Help()
|
|
cmd.PrintErrf("Error: %v\n", err)
|
|
}
|
|
},
|
|
}
|
|
|
|
func listHandler(
|
|
ctx context.Context, req *mcp.CallToolRequest, input listIn,
|
|
) (*mcp.CallToolResult, any, error) {
|
|
logger := slog.New(
|
|
mcp.NewLoggingHandler(
|
|
req.Session,
|
|
&mcp.LoggingHandlerOptions{LoggerName: listTool, MinInterval: time.Second},
|
|
),
|
|
)
|
|
|
|
channelId = input.ChannelId
|
|
home = utils.BoolPtr(*input.Home)
|
|
maxResults = input.MaxResults
|
|
mine = utils.BoolPtr(*input.Mine)
|
|
publishedAfter = input.PublishedAfter
|
|
publishedBefore = input.PublishedBefore
|
|
regionCode = input.RegionCode
|
|
parts = input.Parts
|
|
output = input.Output
|
|
jpath = input.Jsonpath
|
|
|
|
var writer bytes.Buffer
|
|
err := list(&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 list(writer io.Writer) error {
|
|
a := activity.NewActivity(
|
|
activity.WithChannelId(channelId),
|
|
activity.WithHome(home),
|
|
activity.WithMaxResults(maxResults),
|
|
activity.WithMine(mine),
|
|
activity.WithPublishedAfter(publishedAfter),
|
|
activity.WithPublishedBefore(publishedBefore),
|
|
activity.WithRegionCode(regionCode),
|
|
activity.WithService(nil),
|
|
)
|
|
|
|
return a.List(parts, output, jpath, writer)
|
|
}
|