0908
This commit is contained in:
45
aio-mcp/internal/config/config.go
Normal file
45
aio-mcp/internal/config/config.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// Package config 解析 aio-mcp 的启动配置(命令行参数)。
|
||||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// 支持的传输方式。
|
||||
const (
|
||||
TransportStdio = "stdio"
|
||||
TransportHTTP = "http"
|
||||
)
|
||||
|
||||
// Config 是 aio-mcp 的运行时配置。
|
||||
type Config struct {
|
||||
Transport string // MCP 传输方式:stdio 或 http
|
||||
Addr string // http 模式下的监听地址
|
||||
LogLevel string // 日志级别:debug / info / warn / error
|
||||
DataDir string // 数据目录:SQLite 库文件与工具产物的存放位置
|
||||
}
|
||||
|
||||
// Parse 解析命令行参数。args 不含程序名(通常传 os.Args[1:]),
|
||||
// 用法与错误信息写到 out。
|
||||
func Parse(args []string, out io.Writer) (*Config, error) {
|
||||
cfg := &Config{}
|
||||
|
||||
fs := flag.NewFlagSet("aio-mcp", flag.ContinueOnError)
|
||||
fs.SetOutput(out)
|
||||
fs.StringVar(&cfg.Transport, "transport", TransportHTTP, "传输方式:http(默认,streamable HTTP)或 stdio(本地 Agent 拉起)")
|
||||
fs.StringVar(&cfg.Addr, "addr", "localhost:8000", "http 模式下的监听地址")
|
||||
fs.StringVar(&cfg.LogLevel, "log-level", "info", "日志级别:debug / info / warn / error")
|
||||
fs.StringVar(&cfg.DataDir, "data-dir", "data", "数据目录:SQLite 库文件与工具产物的存放位置")
|
||||
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cfg.Transport != TransportStdio && cfg.Transport != TransportHTTP {
|
||||
return nil, fmt.Errorf("未知 transport: %q(支持: %s, %s)", cfg.Transport, TransportStdio, TransportHTTP)
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user