|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + _ "embed" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "text/template" |
| 11 | +) |
| 12 | + |
| 13 | +//go:embed templates/basic/main.go |
| 14 | +var basicMainGoTemplate string |
| 15 | + |
| 16 | +//go:embed templates/basic/Dockerfile |
| 17 | +var basicDockerfileTemplate string |
| 18 | + |
| 19 | +//go:embed templates/basic/compose.yaml |
| 20 | +var basicComposeTemplate string |
| 21 | + |
| 22 | +//go:embed templates/basic/catalog.yaml |
| 23 | +var basicCatalogTemplate string |
| 24 | + |
| 25 | +//go:embed templates/basic/go.mod.template |
| 26 | +var basicGoModTemplate string |
| 27 | + |
| 28 | +//go:embed templates/basic/README.md |
| 29 | +var basicReadmeTemplate string |
| 30 | + |
| 31 | +//go:embed templates/chatgpt-app-basic/main.go |
| 32 | +var chatgptAppMainGoTemplate string |
| 33 | + |
| 34 | +//go:embed templates/chatgpt-app-basic/Dockerfile |
| 35 | +var chatgptAppDockerfileTemplate string |
| 36 | + |
| 37 | +//go:embed templates/chatgpt-app-basic/compose.yaml |
| 38 | +var chatgptAppComposeTemplate string |
| 39 | + |
| 40 | +//go:embed templates/chatgpt-app-basic/catalog.yaml |
| 41 | +var chatgptAppCatalogTemplate string |
| 42 | + |
| 43 | +//go:embed templates/chatgpt-app-basic/go.mod.template |
| 44 | +var chatgptAppGoModTemplate string |
| 45 | + |
| 46 | +//go:embed templates/chatgpt-app-basic/README.md |
| 47 | +var chatgptAppReadmeTemplate string |
| 48 | + |
| 49 | +//go:embed templates/chatgpt-app-basic/ui.html |
| 50 | +var chatgptAppUITemplate string |
| 51 | + |
| 52 | +type templateData struct { |
| 53 | + ServerName string |
| 54 | +} |
| 55 | + |
| 56 | +type templateSet struct { |
| 57 | + mainGo string |
| 58 | + dockerfile string |
| 59 | + compose string |
| 60 | + catalog string |
| 61 | + goMod string |
| 62 | + readme string |
| 63 | + uiHTML string // optional, only for chatgpt-app-basic |
| 64 | +} |
| 65 | + |
| 66 | +func getTemplateSet(templateName string) (*templateSet, error) { |
| 67 | + switch templateName { |
| 68 | + case "basic": |
| 69 | + return &templateSet{ |
| 70 | + mainGo: basicMainGoTemplate, |
| 71 | + dockerfile: basicDockerfileTemplate, |
| 72 | + compose: basicComposeTemplate, |
| 73 | + catalog: basicCatalogTemplate, |
| 74 | + goMod: basicGoModTemplate, |
| 75 | + readme: basicReadmeTemplate, |
| 76 | + }, nil |
| 77 | + case "chatgpt-app-basic": |
| 78 | + return &templateSet{ |
| 79 | + mainGo: chatgptAppMainGoTemplate, |
| 80 | + dockerfile: chatgptAppDockerfileTemplate, |
| 81 | + compose: chatgptAppComposeTemplate, |
| 82 | + catalog: chatgptAppCatalogTemplate, |
| 83 | + goMod: chatgptAppGoModTemplate, |
| 84 | + readme: chatgptAppReadmeTemplate, |
| 85 | + uiHTML: chatgptAppUITemplate, |
| 86 | + }, nil |
| 87 | + default: |
| 88 | + return nil, fmt.Errorf("unknown template: %s (available: basic, chatgpt-app-basic)", templateName) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Init initializes a new MCP server project in the specified directory |
| 93 | +func Init(_ context.Context, dir string, language string, templateName string) error { |
| 94 | + if language != "go" { |
| 95 | + return fmt.Errorf("unsupported language: %s (currently only 'go' is supported)", language) |
| 96 | + } |
| 97 | + |
| 98 | + // Get the template set |
| 99 | + templates, err := getTemplateSet(templateName) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + // Create directory if it doesn't exist |
| 105 | + if err := os.MkdirAll(dir, 0o755); err != nil { |
| 106 | + return fmt.Errorf("creating directory: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + // Check if directory is empty |
| 110 | + entries, err := os.ReadDir(dir) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("reading directory: %w", err) |
| 113 | + } |
| 114 | + if len(entries) > 0 { |
| 115 | + return fmt.Errorf("directory %s is not empty", dir) |
| 116 | + } |
| 117 | + |
| 118 | + // Extract server name from directory path |
| 119 | + serverName := filepath.Base(dir) |
| 120 | + data := templateData{ServerName: serverName} |
| 121 | + |
| 122 | + // Generate files from templates |
| 123 | + files := map[string]string{ |
| 124 | + "main.go": templates.mainGo, |
| 125 | + "Dockerfile": templates.dockerfile, |
| 126 | + "compose.yaml": templates.compose, |
| 127 | + "catalog.yaml": templates.catalog, |
| 128 | + "go.mod": templates.goMod, |
| 129 | + "README.md": templates.readme, |
| 130 | + } |
| 131 | + |
| 132 | + // Add ui.html for chatgpt-app-basic template |
| 133 | + if templates.uiHTML != "" { |
| 134 | + files["ui.html"] = templates.uiHTML |
| 135 | + } |
| 136 | + |
| 137 | + for filename, tmplContent := range files { |
| 138 | + // Parse and execute template |
| 139 | + tmpl, err := template.New(filename).Parse(tmplContent) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("parsing template %s: %w", filename, err) |
| 142 | + } |
| 143 | + |
| 144 | + var buf bytes.Buffer |
| 145 | + if err := tmpl.Execute(&buf, data); err != nil { |
| 146 | + return fmt.Errorf("executing template %s: %w", filename, err) |
| 147 | + } |
| 148 | + |
| 149 | + // Write file |
| 150 | + path := filepath.Join(dir, filename) |
| 151 | + if err := os.WriteFile(path, buf.Bytes(), 0o644); err != nil { |
| 152 | + return fmt.Errorf("writing %s: %w", filename, err) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return nil |
| 157 | +} |
0 commit comments