Skip to content

Commit

Permalink
✨ Make all commands glazed commands
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed Jan 24, 2025
1 parent 2c38ad4 commit c53e70c
Show file tree
Hide file tree
Showing 4 changed files with 711 additions and 95 deletions.
187 changes: 145 additions & 42 deletions cmd/mcp-client/cmds/resources.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package cmds

import (
"context"
"fmt"
"io"

"github.com/go-go-golems/glazed/pkg/cli"
"github.com/go-go-golems/glazed/pkg/cmds"
glazed_layers "github.com/go-go-golems/glazed/pkg/cmds/layers"
"github.com/go-go-golems/glazed/pkg/cmds/parameters"
"github.com/go-go-golems/glazed/pkg/middlewares"
"github.com/go-go-golems/glazed/pkg/settings"
"github.com/go-go-golems/glazed/pkg/types"
"github.com/go-go-golems/go-go-mcp/cmd/mcp-client/cmds/helpers"
"github.com/go-go-golems/go-go-mcp/cmd/mcp-client/cmds/layers"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -14,61 +25,153 @@ var ResourcesCmd = &cobra.Command{
Long: `List available resources and read specific resources.`,
}

var listResourcesCmd = &cobra.Command{
Use: "list",
Short: "List available resources",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := helpers.CreateClient(cmd)
if err != nil {
return err
}
defer client.Close(cmd.Context())
type ListResourcesCommand struct {
*cmds.CommandDescription
}

resources, cursor, err := client.ListResources(cmd.Context(), "")
if err != nil {
return err
}
type ListResourcesSettings struct {
}

for _, resource := range resources {
fmt.Printf("URI: %s\n", resource.URI)
fmt.Printf("Name: %s\n", resource.Name)
fmt.Printf("Description: %s\n", resource.Description)
fmt.Printf("MimeType: %s\n", resource.MimeType)
fmt.Println()
}
type ReadResourceCommand struct {
*cmds.CommandDescription
}

if cursor != "" {
fmt.Printf("Next cursor: %s\n", cursor)
}
type ReadResourceSettings struct {
URI string `glazed.parameter:"uri"`
}

func NewListResourcesCommand() (*ListResourcesCommand, error) {
glazedParameterLayer, err := settings.NewGlazedParameterLayers()
if err != nil {
return nil, errors.Wrap(err, "could not create Glazed parameter layer")
}

return nil
},
clientLayer, err := layers.NewClientParameterLayer()
if err != nil {
return nil, errors.Wrap(err, "could not create client parameter layer")
}

return &ListResourcesCommand{
CommandDescription: cmds.NewCommandDescription(
"list",
cmds.WithShort("List available resources"),
cmds.WithLayersList(
glazedParameterLayer,
clientLayer,
),
),
}, nil
}

var readResourceCmd = &cobra.Command{
Use: "read [resource-uri]",
Short: "Read a specific resource",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := helpers.CreateClient(cmd)
if err != nil {
func NewReadResourceCommand() (*ReadResourceCommand, error) {
clientLayer, err := layers.NewClientParameterLayer()
if err != nil {
return nil, errors.Wrap(err, "could not create client parameter layer")
}

return &ReadResourceCommand{
CommandDescription: cmds.NewCommandDescription(
"read",
cmds.WithShort("Read a specific resource"),
cmds.WithArguments(
parameters.NewParameterDefinition(
"uri",
parameters.ParameterTypeString,
parameters.WithHelp("URI of the resource to read"),
parameters.WithRequired(true),
),
),
cmds.WithLayersList(clientLayer),
),
}, nil
}

func (c *ListResourcesCommand) RunIntoGlazeProcessor(
ctx context.Context,
parsedLayers *glazed_layers.ParsedLayers,
gp middlewares.Processor,
) error {
s := &ListResourcesSettings{}
if err := parsedLayers.InitializeStruct(glazed_layers.DefaultSlug, s); err != nil {
return err
}

client, err := helpers.CreateClientFromSettings(parsedLayers)
if err != nil {
return err
}
defer client.Close(ctx)

resources, cursor, err := client.ListResources(ctx, "")
if err != nil {
return err
}

for _, resource := range resources {
row := types.NewRow(
types.MRP("uri", resource.URI),
types.MRP("name", resource.Name),
types.MRP("description", resource.Description),
types.MRP("mime_type", resource.MimeType),
)
if err := gp.AddRow(ctx, row); err != nil {
return err
}
defer client.Close(cmd.Context())
}

content, err := client.ReadResource(cmd.Context(), args[0])
if err != nil {
if cursor != "" {
// Add cursor as a separate row with a special type
row := types.NewRow(
types.MRP("cursor", cursor),
types.MRP("type", "cursor"),
)
if err := gp.AddRow(ctx, row); err != nil {
return err
}
}

fmt.Printf("URI: %s\n", content.URI)
fmt.Printf("MimeType: %s\n", content.MimeType)
fmt.Printf("Content:\n%s\n", content.Text)
return nil
},
return nil
}

func (c *ReadResourceCommand) RunIntoWriter(
ctx context.Context,
parsedLayers *glazed_layers.ParsedLayers,
w io.Writer,
) error {
s := &ReadResourceSettings{}
if err := parsedLayers.InitializeStruct(glazed_layers.DefaultSlug, s); err != nil {
return err
}

client, err := helpers.CreateClientFromSettings(parsedLayers)
if err != nil {
return err
}
defer client.Close(ctx)

content, err := client.ReadResource(ctx, s.URI)
if err != nil {
return err
}

_, err = fmt.Fprintf(w, "URI: %s\nMimeType: %s\nContent:\n%s\n",
content.URI, content.MimeType, content.Text)
return err
}

func init() {
ResourcesCmd.AddCommand(listResourcesCmd)
ResourcesCmd.AddCommand(readResourceCmd)
listCmd, err := NewListResourcesCommand()
cobra.CheckErr(err)

cobraListCmd, err := cli.BuildCobraCommandFromGlazeCommand(listCmd)
cobra.CheckErr(err)

readCmd, err := NewReadResourceCommand()
cobra.CheckErr(err)

cobraReadCmd, err := cli.BuildCobraCommandFromWriterCommand(readCmd)
cobra.CheckErr(err)

ResourcesCmd.AddCommand(cobraListCmd)
ResourcesCmd.AddCommand(cobraReadCmd)
}
Loading

0 comments on commit c53e70c

Please sign in to comment.