forked from tbxark/mcp-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
227 lines (198 loc) · 6.17 KB
/
config.go
File metadata and controls
227 lines (198 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"crypto/tls"
"errors"
nethttp "net/http"
"strings"
"time"
"github.com/go-sphere/confstore"
"github.com/go-sphere/confstore/codec"
"github.com/go-sphere/confstore/provider"
"github.com/go-sphere/confstore/provider/file"
"github.com/go-sphere/confstore/provider/http"
"github.com/tbxark/optional-go"
)
type StdioMCPClientConfig struct {
Command string `json:"command"`
Env map[string]string `json:"env"`
Args []string `json:"args"`
}
type SSEMCPClientConfig struct {
URL string `json:"url"`
Headers map[string]string `json:"headers"`
}
type StreamableMCPClientConfig struct {
URL string `json:"url"`
Headers map[string]string `json:"headers"`
Timeout time.Duration `json:"timeout"`
}
type MCPClientType string
const (
MCPClientTypeStdio MCPClientType = "stdio"
MCPClientTypeSSE MCPClientType = "sse"
MCPClientTypeStreamable MCPClientType = "streamable-http"
)
type MCPServerType string
const (
MCPServerTypeSSE MCPServerType = "sse"
MCPServerTypeStreamable MCPServerType = "streamable-http"
)
// ---- V2 ----
type ToolFilterMode string
const (
ToolFilterModeAllow ToolFilterMode = "allow"
ToolFilterModeBlock ToolFilterMode = "block"
)
type ToolFilterConfig struct {
Mode ToolFilterMode `json:"mode,omitempty"`
List []string `json:"list,omitempty"`
}
type OptionsV2 struct {
PanicIfInvalid optional.Field[bool] `json:"panicIfInvalid,omitempty"`
LogEnabled optional.Field[bool] `json:"logEnabled,omitempty"`
AuthTokens []string `json:"authTokens,omitempty"`
ToolFilter *ToolFilterConfig `json:"toolFilter,omitempty"`
Disabled bool `json:"disabled,omitempty"`
}
type MCPProxyConfigV2 struct {
BaseURL string `json:"baseURL"`
Addr string `json:"addr"`
Name string `json:"name"`
Version string `json:"version"`
Type MCPServerType `json:"type,omitempty"`
Options *OptionsV2 `json:"options,omitempty"`
}
type MCPClientConfigV2 struct {
TransportType MCPClientType `json:"transportType,omitempty"`
// Stdio
Command string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
Env map[string]string `json:"env,omitempty"`
// SSE or Streamable HTTP
URL string `json:"url,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Timeout time.Duration `json:"timeout,omitempty"`
Options *OptionsV2 `json:"options,omitempty"`
}
func parseMCPClientConfigV2(conf *MCPClientConfigV2) (any, error) {
if conf.Command != "" || conf.TransportType == MCPClientTypeStdio {
if conf.Command == "" {
return nil, errors.New("command is required for stdio transport")
}
return &StdioMCPClientConfig{
Command: conf.Command,
Env: conf.Env,
Args: conf.Args,
}, nil
}
if conf.URL != "" {
if conf.TransportType == MCPClientTypeStreamable {
return &StreamableMCPClientConfig{
URL: conf.URL,
Headers: conf.Headers,
Timeout: conf.Timeout,
}, nil
} else {
return &SSEMCPClientConfig{
URL: conf.URL,
Headers: conf.Headers,
}, nil
}
}
return nil, errors.New("invalid server type")
}
// ---- Config ----
type Config struct {
McpProxy *MCPProxyConfigV2 `json:"mcpProxy"`
McpServers map[string]*MCPClientConfigV2 `json:"mcpServers"`
}
type FullConfig struct {
DeprecatedServerV1 *MCPProxyConfigV1 `json:"server"`
DeprecatedClientsV1 map[string]*MCPClientConfigV1 `json:"clients"`
McpProxy *MCPProxyConfigV2 `json:"mcpProxy"`
McpServers map[string]*MCPClientConfigV2 `json:"mcpServers"`
}
func newConfProvider(path string, insecure, expandEnv bool, httpHeaders string, httpTimeout int) (provider.Provider, error) {
if http.IsRemoteURL(path) {
var opts []http.Option
httpClient := nethttp.DefaultClient
if insecure {
transport := nethttp.DefaultTransport.(*nethttp.Transport).Clone()
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
httpClient = &nethttp.Client{Transport: transport}
}
if httpTimeout > 0 {
httpClient.Timeout = time.Duration(httpTimeout) * time.Second
}
opts = append(opts, http.WithClient(httpClient))
if httpHeaders != "" {
// format: 'Key1:Value1;Key2:Value2'
headers := make(nethttp.Header)
for _, kv := range strings.Split(httpHeaders, ";") {
parts := strings.SplitN(kv, ":", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
if key != "" && value != "" {
headers.Add(key, value)
}
}
}
if len(headers) > 0 {
opts = append(opts, http.WithHeaders(headers))
}
}
pro := http.New(path, opts...)
if expandEnv {
return provider.NewExpandEnv(pro), nil
} else {
return pro, nil
}
}
if file.IsLocalPath(path) {
if expandEnv {
return provider.NewExpandEnv(file.New(path, file.WithExpandEnv())), nil
} else {
return file.New(path), nil
}
}
return nil, errors.New("unsupported config path")
}
func load(path string, insecure, expandEnv bool, httpHeaders string, httpTimeout int) (*Config, error) {
pro, err := newConfProvider(path, insecure, expandEnv, httpHeaders, httpTimeout)
if err != nil {
return nil, err
}
conf, err := confstore.Load[FullConfig](pro, codec.JsonCodec())
if err != nil {
return nil, err
}
adaptMCPClientConfigV1ToV2(conf)
if conf.McpProxy == nil {
return nil, errors.New("mcpProxy is required")
}
if conf.McpProxy.Options == nil {
conf.McpProxy.Options = &OptionsV2{}
}
for _, clientConfig := range conf.McpServers {
if clientConfig.Options == nil {
clientConfig.Options = &OptionsV2{}
}
if clientConfig.Options.AuthTokens == nil {
clientConfig.Options.AuthTokens = conf.McpProxy.Options.AuthTokens
}
if !clientConfig.Options.PanicIfInvalid.Present() {
clientConfig.Options.PanicIfInvalid = conf.McpProxy.Options.PanicIfInvalid
}
if !clientConfig.Options.LogEnabled.Present() {
clientConfig.Options.LogEnabled = conf.McpProxy.Options.LogEnabled
}
}
if conf.McpProxy.Type == "" {
conf.McpProxy.Type = MCPServerTypeSSE // default to SSE
}
return &Config{
McpProxy: conf.McpProxy,
McpServers: conf.McpServers,
}, nil
}