-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathexport.go
More file actions
125 lines (104 loc) · 3.58 KB
/
export.go
File metadata and controls
125 lines (104 loc) · 3.58 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
package client
import (
_ "embed"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/cloudposse/atmos/cmd/mcp/mcpcmd"
errUtils "github.com/cloudposse/atmos/errors"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui"
)
//go:embed markdown/atmos_mcp_export.md
var exportLongMarkdown string
const configFilePermissions = 0o600
var exportCmd = &cobra.Command{
Use: "export",
Short: "Export .mcp.json from atmos.yaml MCP server configuration",
Long: exportLongMarkdown,
Args: cobra.NoArgs,
RunE: executeMCPExport,
}
func init() {
exportCmd.Flags().StringP("output", "o", ".mcp.json", "Output file path")
mcpcmd.McpCmd.AddCommand(exportCmd)
}
// mcpJSONConfig represents the .mcp.json file format used by Claude Code and other IDEs.
type mcpJSONConfig struct {
MCPServers map[string]mcpJSONServer `json:"mcpServers"`
}
// mcpJSONServer represents a single MCP server entry in .mcp.json.
type mcpJSONServer struct {
Command string `json:"command"`
Args []string `json:"args"`
Env map[string]string `json:"env,omitempty"`
}
func executeMCPExport(cmd *cobra.Command, _ []string) error {
defer perf.Track(nil, "cmd.mcpExport")()
atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, false)
if err != nil {
return err
}
if len(atmosConfig.MCP.Servers) == 0 {
ui.Info("No MCP servers configured. Add servers under `mcp.servers` in `atmos.yaml`.")
return nil
}
outputFile, _ := cmd.Flags().GetString("output")
config := mcpJSONConfig{
MCPServers: make(map[string]mcpJSONServer),
}
for name, serverCfg := range atmosConfig.MCP.Servers {
config.MCPServers[name] = buildMCPJSONEntry(name, &serverCfg)
}
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return fmt.Errorf("%w: %w", errUtils.ErrMCPConfigMarshalFailed, err)
}
if err := os.WriteFile(outputFile, append(data, '\n'), configFilePermissions); err != nil {
return fmt.Errorf("%w: %s: %w", errUtils.ErrMCPConfigWriteFailed, outputFile, err)
}
// Enforce permissions on existing files (WriteFile only sets perms on new files).
if err := os.Chmod(outputFile, configFilePermissions); err != nil {
return fmt.Errorf("%w: %s: %w", errUtils.ErrMCPConfigPermsFailed, outputFile, err)
}
ui.Success(fmt.Sprintf("Generated %s with %d server(s)", outputFile, len(config.MCPServers)))
return nil
}
// buildMCPJSONEntry creates a .mcp.json entry for a server.
// Servers with identity are wrapped with 'atmos auth exec' for credential injection.
// Env keys are uppercased because Viper lowercases all YAML map keys.
func buildMCPJSONEntry(_ string, serverCfg *schema.MCPServerConfig) mcpJSONServer {
env := uppercaseEnvKeys(serverCfg.Env)
if serverCfg.Identity != "" {
// Wrap with atmos auth exec for credential injection.
args := []string{"auth", "exec", "-i", serverCfg.Identity, "--", serverCfg.Command}
args = append(args, serverCfg.Args...)
return mcpJSONServer{
Command: "atmos",
Args: args,
Env: env,
}
}
// No auth — use command directly.
return mcpJSONServer{
Command: serverCfg.Command,
Args: serverCfg.Args,
Env: env,
}
}
// uppercaseEnvKeys returns a copy of the env map with all keys uppercased.
// Viper lowercases all YAML map keys, but env vars are conventionally UPPERCASE.
func uppercaseEnvKeys(env map[string]string) map[string]string {
if env == nil {
return nil
}
result := make(map[string]string, len(env))
for k, v := range env {
result[strings.ToUpper(k)] = v
}
return result
}