googleapis--mcp-toolbox
99 行
2.7 KiB
Go
99 行
2.7 KiB
Go
// Copyright 2025 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package custom
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
yaml "github.com/goccy/go-yaml"
|
|
"github.com/googleapis/mcp-toolbox/internal/prompts"
|
|
"github.com/googleapis/mcp-toolbox/internal/util/parameters"
|
|
)
|
|
|
|
type Message = prompts.Message
|
|
|
|
const resourceType = "custom"
|
|
|
|
// init registers this prompt type with the prompt framework.
|
|
func init() {
|
|
if !prompts.Register(resourceType, newConfig) {
|
|
panic(fmt.Sprintf("prompt type %q already registered", resourceType))
|
|
}
|
|
}
|
|
|
|
// newConfig is the factory function for creating a custom prompt configuration.
|
|
func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (prompts.PromptConfig, error) {
|
|
cfg := &Config{Name: name}
|
|
if err := decoder.DecodeContext(ctx, cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
// Config is the configuration for a custom prompt.
|
|
// It implements both the prompts.PromptConfig and prompts.Prompt interfaces.
|
|
type Config struct {
|
|
Name string `yaml:"name"`
|
|
Description string `yaml:"description,omitempty"`
|
|
Messages []Message `yaml:"messages"`
|
|
Arguments prompts.Arguments `yaml:"arguments,omitempty"`
|
|
}
|
|
|
|
// Interface compliance checks.
|
|
var _ prompts.PromptConfig = Config{}
|
|
var _ prompts.Prompt = Prompt{}
|
|
|
|
func (c Config) PromptConfigType() string {
|
|
return resourceType
|
|
}
|
|
|
|
func (c Config) Initialize() (prompts.Prompt, error) {
|
|
p := Prompt{
|
|
Config: c,
|
|
manifest: prompts.GetManifest(c.Description, c.Arguments),
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
type Prompt struct {
|
|
Config
|
|
manifest prompts.Manifest
|
|
}
|
|
|
|
func (p Prompt) GetDesc() string {
|
|
return p.Description
|
|
}
|
|
|
|
func (p Prompt) GetArguments() prompts.Arguments {
|
|
return p.Arguments
|
|
}
|
|
|
|
func (p Prompt) ToConfig() prompts.PromptConfig {
|
|
return p.Config
|
|
}
|
|
|
|
func (p Prompt) Manifest() prompts.Manifest {
|
|
return p.manifest
|
|
}
|
|
|
|
func (p Prompt) SubstituteParams(argValues parameters.ParamValues) (any, error) {
|
|
return prompts.SubstituteMessages(p.Messages, p.Arguments, argValues)
|
|
}
|
|
|
|
func (p Prompt) ParseArgs(args map[string]any, data map[string]map[string]any) (parameters.ParamValues, error) {
|
|
return prompts.ParseArguments(p.Arguments, args, data)
|
|
}
|