Files
Go-Web-Template/server/cmd/mcp/config.go

128 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"git.echol.cn/loser/Go-Web-Template/server/config"
"git.echol.cn/loser/Go-Web-Template/server/global"
"gopkg.in/yaml.v3"
)
type standaloneConfig struct {
MCP config.MCP `yaml:"mcp"`
}
func loadStandaloneConfig() (string, error) {
configPath, err := resolveConfigPath()
if err != nil {
return "", err
}
content, err := os.ReadFile(configPath)
if err != nil {
return "", fmt.Errorf("读取 MCP 配置失败: %w", err)
}
var cfg standaloneConfig
if err := yaml.Unmarshal(content, &cfg); err != nil {
return "", fmt.Errorf("解析 MCP 配置失败: %w", err)
}
applyStandaloneDefaults(configPath, &cfg)
global.GVA_CONFIG.MCP = cfg.MCP
return configPath, nil
}
func resolveConfigPath() (string, error) {
explicit, err := parseConfigFlag(os.Args[1:])
if err != nil {
return "", err
}
if explicit != "" {
return filepath.Abs(explicit)
}
if envPath := strings.TrimSpace(os.Getenv("GVA_MCP_CONFIG")); envPath != "" {
return filepath.Abs(envPath)
}
wd, _ := os.Getwd()
exe, _ := os.Executable()
exeDir := filepath.Dir(exe)
candidates := []string{
filepath.Join(wd, "config.yaml"),
filepath.Join(wd, "mcp.yaml"),
filepath.Join(wd, "cmd", "mcp", "config.yaml"),
filepath.Join(wd, "server", "cmd", "mcp", "config.yaml"),
filepath.Join(exeDir, "config.yaml"),
filepath.Join(exeDir, "mcp.yaml"),
}
for _, candidate := range candidates {
if candidate == "" {
continue
}
if info, err := os.Stat(candidate); err == nil && !info.IsDir() {
return filepath.Abs(candidate)
}
}
return "", errors.New("未找到 MCP 独立配置文件请在当前目录、cmd/mcp 目录或通过 -config / GVA_MCP_CONFIG 指定 config.yaml")
}
func parseConfigFlag(args []string) (string, error) {
fs := flag.NewFlagSet("gva-mcp", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var configPath string
fs.StringVar(&configPath, "c", "", "MCP config file path")
fs.StringVar(&configPath, "config", "", "MCP config file path")
if err := fs.Parse(args); err != nil {
return "", err
}
return strings.TrimSpace(configPath), nil
}
func applyStandaloneDefaults(configPath string, cfg *standaloneConfig) {
if cfg.MCP.Name == "" {
cfg.MCP.Name = "GVA_MCP"
}
if cfg.MCP.Version == "" {
cfg.MCP.Version = "v1.0.0"
}
if cfg.MCP.Path == "" {
cfg.MCP.Path = "/mcp"
}
if cfg.MCP.Addr == 0 {
cfg.MCP.Addr = 8889
}
if cfg.MCP.AuthHeader == "" {
cfg.MCP.AuthHeader = "x-token"
}
if cfg.MCP.RequestTimeout <= 0 {
cfg.MCP.RequestTimeout = 15
}
if cfg.MCP.UpstreamBaseURL == "" {
cfg.MCP.UpstreamBaseURL = "http://127.0.0.1:8888"
}
if cfg.MCP.BaseURL == "" {
cfg.MCP.BaseURL = fmt.Sprintf("http://127.0.0.1:%d%s", cfg.MCP.Addr, cfg.MCP.Path)
}
}
func isDir(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}