68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"text/template"
|
|
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"kra/internal/conf"
|
|
"kra/pkg/utils"
|
|
"kra/pkg/utils/autocode"
|
|
)
|
|
|
|
// AutoMcpTool MCP工具请求
|
|
type AutoMcpTool struct {
|
|
Name string `json:"name"` // 工具名称
|
|
Description string `json:"description"` // 工具描述
|
|
Parameters string `json:"parameters"` // 参数定义
|
|
}
|
|
|
|
// AutoCodeMCPUsecase MCP用例
|
|
type AutoCodeMCPUsecase struct {
|
|
config *conf.AutoCodeMCPConfig
|
|
log *log.Helper
|
|
}
|
|
|
|
// NewAutoCodeMCPUsecase 创建MCP用例
|
|
func NewAutoCodeMCPUsecase(config *conf.AutoCodeMCPConfig, logger log.Logger) *AutoCodeMCPUsecase {
|
|
return &AutoCodeMCPUsecase{
|
|
config: config,
|
|
log: log.NewHelper(logger),
|
|
}
|
|
}
|
|
|
|
// CreateMcp 创建MCP工具文件
|
|
func (uc *AutoCodeMCPUsecase) CreateMcp(ctx context.Context, info AutoMcpTool) (toolFilePath string, err error) {
|
|
mcpTemplatePath := filepath.Join(uc.config.Root, uc.config.Server, "resource", "mcp", "tools.tpl")
|
|
mcpToolPath := filepath.Join(uc.config.Root, uc.config.Server, "mcp")
|
|
|
|
var files *template.Template
|
|
|
|
templateName := filepath.Base(mcpTemplatePath)
|
|
|
|
files, err = template.New(templateName).Funcs(autocode.GetTemplateFuncMap()).ParseFiles(mcpTemplatePath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
fileName := utils.HumpToUnderscore(info.Name)
|
|
|
|
toolFilePath = filepath.Join(mcpToolPath, fileName+".go")
|
|
|
|
f, err := os.Create(toolFilePath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer f.Close()
|
|
|
|
// 执行模板,将内容写入文件
|
|
err = files.Execute(f, info)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return toolFilePath, nil
|
|
}
|