-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathconfig.go
More file actions
400 lines (344 loc) · 13.3 KB
/
Copy pathconfig.go
File metadata and controls
400 lines (344 loc) · 13.3 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package mcp_server //nolint:revive,stylecheck,mnd // fine for now
import (
"encoding/json"
"fmt"
"time"
"gopkg.in/yaml.v2"
"github.com/stackql/stackql/pkg/mcp_server/audit"
"github.com/stackql/stackql/pkg/mcp_server/policy"
"github.com/stackql/stackql/pkg/mcp_server/render"
"github.com/stackql/stackql/pkg/sink"
)
// Config represents the complete configuration for the MCP server.
type Config struct {
// Server contains server-specific configuration.
Server ServerConfig `json:"server" yaml:"server"`
// Backend contains backend-specific configuration.
Backend BackendConfig `json:"backend" yaml:"backend"`
// EnabledTools restricts which MCP tools the server publishes.
// When nil or empty, every built-in tool is published (default behavior).
// When populated, only the named tools are registered.
EnabledTools []string `json:"enabled_tools,omitempty" yaml:"enabled_tools,omitempty"`
// EnabledPrompts restricts which MCP prompts the server publishes.
// Same semantics as EnabledTools: nil or empty means all registered prompts are published.
EnabledPrompts []string `json:"enabled_prompts,omitempty" yaml:"enabled_prompts,omitempty"`
}
// IsToolEnabled reports whether the named tool should be published.
// Empty/nil EnabledTools means all tools are enabled.
func (c *Config) IsToolEnabled(name string) bool {
if len(c.EnabledTools) == 0 {
return true
}
for _, n := range c.EnabledTools {
if n == name {
return true
}
}
return false
}
// IsPromptEnabled reports whether the named prompt should be published.
// Empty/nil EnabledPrompts means all prompts are enabled.
func (c *Config) IsPromptEnabled(name string) bool {
if len(c.EnabledPrompts) == 0 {
return true
}
for _, n := range c.EnabledPrompts {
if n == name {
return true
}
}
return false
}
func (c *Config) GetServerTransport() string {
if c.Server.Transport == "" {
return DefaultConfig().Server.Transport
}
return c.Server.Transport
}
func (c *Config) GetServerAddress() string {
if c.Server.Address == "" {
return DefaultConfig().Server.Address
}
return c.Server.Address
}
func (c *Config) IsTcpBackend() bool {
return c.Backend.Type == "tcp"
}
func (c *Config) GetBackendConnectionString() string {
if c.Backend.ConnectionString == "" {
return DefaultConfig().Backend.ConnectionString
}
return c.Backend.ConnectionString
}
// GetMode returns the server's effective mode. Empty string is mapped to
// the safe default.
func (c *Config) GetMode() string {
if c == nil || c.Server.Mode == "" {
return policy.ModeSafe
}
return c.Server.Mode
}
// GetRender returns the server-level default render format for tool result
// text content. Empty string is mapped to the markdown default.
func (c *Config) GetRender() string {
if c == nil || c.Server.Render == "" {
return render.FormatMarkdown
}
return c.Server.Render
}
// IsAuditEnabled reports whether audit logging should run. Audit is on by
// default unless explicitly disabled.
func (c *Config) IsAuditEnabled() bool {
if c == nil || c.Server.Audit.Disabled {
return false
}
return true
}
// ServerConfig contains configuration for the MCP server itself.
type ServerConfig struct {
// Name is the server name advertised to clients.
Name string `json:"name" yaml:"name"`
// Transport specifies the transport configuration for the server.
Transport string `json:"transport" yaml:"transport"`
// Address is the server Address advertised to clients.
Address string `json:"address" yaml:"address"`
// Scheme is the protocol scheme used by the server.
Scheme string `json:"scheme" yaml:"scheme"`
// Version is the server version advertised to clients.
Version string `json:"version" yaml:"version"`
TLSCertFile string `json:"tls_cert_file,omitempty" yaml:"tls_cert_file,omitempty"`
TLSKeyFile string `json:"tls_key_file,omitempty" yaml:"tls_key_file,omitempty"`
TransportCfg map[string]any `json:"transport_cfg,omitempty" yaml:"transport_cfg,omitempty"`
// Description is a human-readable description of the server.
Description string `json:"description" yaml:"description"`
// MaxConcurrentRequests limits the number of concurrent client requests.
MaxConcurrentRequests int `json:"max_concurrent_requests" yaml:"max_concurrent_requests"`
// RequestTimeout specifies the timeout for individual requests.
RequestTimeout Duration `json:"request_timeout" yaml:"request_timeout"`
// Mode controls the safety contract for query / mutation / lifecycle tools.
// Legal values: "read_only", "safe", "delete_safe", "full_access".
// Empty string is treated as "safe".
//
// For back-compat with PR1, the JSON/YAML key `read_only: true` is also
// accepted and is equivalent to Mode = "read_only". When both `mode`
// and `read_only` are set, `mode` wins.
Mode string `json:"mode,omitempty" yaml:"mode,omitempty"`
// Render sets the server-level default for tool result text content.
// Legal values: "markdown" (default), "json". A per-call `format`
// argument on a tool overrides this default (issue #669).
Render string `json:"render,omitempty" yaml:"render,omitempty"`
// Audit configures the audit subsystem. Audit is enabled by default
// (Disabled is false) and writes to a file sink.
Audit AuditConfig `json:"audit,omitempty" yaml:"audit,omitempty"`
}
// serverConfigWire mirrors ServerConfig with the legacy `read_only` flag
// included as a transient field. Used only for unmarshalling.
type serverConfigWire struct {
Name string `json:"name" yaml:"name"`
Transport string `json:"transport" yaml:"transport"`
Address string `json:"address" yaml:"address"`
Scheme string `json:"scheme" yaml:"scheme"`
Version string `json:"version" yaml:"version"`
TLSCertFile string `json:"tls_cert_file,omitempty" yaml:"tls_cert_file,omitempty"`
TLSKeyFile string `json:"tls_key_file,omitempty" yaml:"tls_key_file,omitempty"`
TransportCfg map[string]any `json:"transport_cfg,omitempty" yaml:"transport_cfg,omitempty"`
Description string `json:"description" yaml:"description"`
MaxConcurrentRequests int `json:"max_concurrent_requests" yaml:"max_concurrent_requests"`
RequestTimeout Duration `json:"request_timeout" yaml:"request_timeout"`
Mode string `json:"mode,omitempty" yaml:"mode,omitempty"`
Render string `json:"render,omitempty" yaml:"render,omitempty"`
Audit AuditConfig `json:"audit,omitempty" yaml:"audit,omitempty"`
// LegacyReadOnly preserves the PR1 `read_only: true` wire form.
LegacyReadOnly *bool `json:"read_only,omitempty" yaml:"read_only,omitempty"`
}
func (s *ServerConfig) fromWire(w serverConfigWire) {
s.Name = w.Name
s.Transport = w.Transport
s.Address = w.Address
s.Scheme = w.Scheme
s.Version = w.Version
s.TLSCertFile = w.TLSCertFile
s.TLSKeyFile = w.TLSKeyFile
s.TransportCfg = w.TransportCfg
s.Description = w.Description
s.MaxConcurrentRequests = w.MaxConcurrentRequests
s.RequestTimeout = w.RequestTimeout
s.Mode = w.Mode
s.Render = w.Render
s.Audit = w.Audit
// Legacy: `read_only: true` with no `mode` -> Mode = "read_only".
// `mode` always wins.
if s.Mode == "" && w.LegacyReadOnly != nil && *w.LegacyReadOnly {
s.Mode = policy.ModeReadOnly
}
}
// UnmarshalJSON honours the legacy `read_only: true` shim.
func (s *ServerConfig) UnmarshalJSON(data []byte) error {
var w serverConfigWire
if err := json.Unmarshal(data, &w); err != nil {
return err
}
s.fromWire(w)
return nil
}
// UnmarshalYAML honours the legacy `read_only: true` shim.
func (s *ServerConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
var w serverConfigWire
if err := unmarshal(&w); err != nil {
return err
}
s.fromWire(w)
return nil
}
// AuditConfig configures the audit subsystem.
type AuditConfig struct {
// Disabled turns the audit subsystem off entirely. Default false
// (audit is on by default).
Disabled bool `json:"disabled,omitempty" yaml:"disabled,omitempty"`
// FailureMode controls what happens when the sink returns an error.
// Legal values: "strict" (default), "strict_mutations", "best_effort".
FailureMode string `json:"failure_mode,omitempty" yaml:"failure_mode,omitempty"`
// Sink selects the destination kind. Currently only "file" is
// implemented; other values are reserved. Empty defaults to "file".
Sink string `json:"sink,omitempty" yaml:"sink,omitempty"`
// File holds file-sink-specific options. Only consulted when Sink is
// "file" (the default).
File sink.FileConfig `json:"file,omitempty" yaml:"file,omitempty"`
}
// GetFailureMode returns the effective failure-mode string with the default
// substituted for empty input.
func (a AuditConfig) GetFailureMode() string {
if a.FailureMode == "" {
return audit.FailureModeStrict
}
return a.FailureMode
}
// BackendConfig contains configuration for the backend connection.
type BackendConfig struct {
// Type specifies the backend type ("tcp", "memory").
Type string `json:"type" yaml:"type"`
// AppName is an optional application name describing the backend.
// In the first instance, this is stackql.
// **Possible** future use case for the backing db (e.g., "postgres", "mysql", etc).
AppName string `json:"app_name" yaml:"app_name"`
// ConnectionString contains the connection details for the backend.
// Format depends on the backend type.
ConnectionString string `json:"dsn" yaml:"dsn"`
// MaxConnections limits the number of backend connections.
MaxConnections int `json:"max_connections" yaml:"max_connections"`
// ConnectionTimeout specifies the timeout for backend connections.
ConnectionTimeout Duration `json:"connection_timeout" yaml:"connection_timeout"`
// QueryTimeout specifies the timeout for individual queries.
QueryTimeout Duration `json:"query_timeout" yaml:"query_timeout"`
}
// Duration is a wrapper around time.Duration that can be marshaled to/from JSON and YAML.
type Duration time.Duration
// MarshalJSON implements json.Marshaler.
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
// UnmarshalJSON implements json.Unmarshaler.
func (d *Duration) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
duration, err := time.ParseDuration(s)
if err != nil {
return err
}
*d = Duration(duration)
return nil
}
// MarshalYAML implements yaml.Marshaler.
func (d Duration) MarshalYAML() (interface{}, error) {
return time.Duration(d).String(), nil
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
duration, err := time.ParseDuration(s)
if err != nil {
return err
}
*d = Duration(duration)
return nil
}
// DefaultConfig returns a configuration with sensible defaults.
func defaultConfig() *Config {
return &Config{
Server: ServerConfig{
Name: "StackQL MCP Server",
Version: "0.1.0",
Description: "Model Context Protocol server for StackQL",
MaxConcurrentRequests: 100,
Transport: serverTransportStdIO,
Address: DefaultHTTPServerAddress,
RequestTimeout: Duration(30 * time.Second),
Mode: policy.ModeSafe,
},
Backend: BackendConfig{
Type: "stackql",
ConnectionString: "stackql://localhost",
MaxConnections: 10,
ConnectionTimeout: Duration(10 * time.Second),
QueryTimeout: Duration(30 * time.Second),
},
}
}
// DefaultConfig returns a configuration with sensible defaults.
func DefaultConfig() *Config {
rv := defaultConfig()
return rv
}
func DefaultHTTPConfig() *Config {
rv := defaultConfig()
rv.Server.Transport = serverTransportHTTP
return rv
}
func DefaultSSEConfig() *Config {
rv := defaultConfig()
rv.Server.Transport = serverTransportSSE
return rv
}
// Validate validates the configuration and returns an error if invalid.
func (c *Config) Validate() error {
if !policy.IsLegalMode(c.Server.Mode) {
return fmt.Errorf("invalid server.mode %q (legal: read_only, safe, delete_safe, full_access)", c.Server.Mode)
}
if !render.IsLegalFormat(c.Server.Render) {
return fmt.Errorf("invalid server.render %q (legal: markdown, json)", c.Server.Render)
}
switch c.Server.Audit.GetFailureMode() {
case audit.FailureModeStrict, audit.FailureModeStrictMutations, audit.FailureModeBestEffort:
default:
return fmt.Errorf("invalid server.audit.failure_mode %q (legal: strict, strict_mutations, best_effort)",
c.Server.Audit.FailureMode)
}
return nil
}
// LoadFromJSON loads configuration from JSON data.
func LoadFromJSON(data []byte) (*Config, error) {
config := &Config{}
if err := json.Unmarshal(data, config); err != nil {
return nil, fmt.Errorf("failed to parse JSON config: %w", err)
}
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
return config, nil
}
// LoadFromYAML loads configuration from YAML data.
func LoadFromYAML(data []byte) (*Config, error) {
config := &Config{}
if err := yaml.Unmarshal(data, config); err != nil {
return nil, fmt.Errorf("failed to parse YAML config: %w", err)
}
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
return config, nil
}