Skip to content

Commit 45e15d0

Browse files
committed
fix: detect server subfolder in init
1 parent 747ad31 commit 45e15d0

File tree

1 file changed

+74
-10
lines changed

1 file changed

+74
-10
lines changed

cmd/publisher/commands/init.go

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ func InitCommand() error {
2121
return errors.New("server.json already exists")
2222
}
2323

24+
// Detect if we're in a subdirectory of the git repository
25+
subfolder := detectSubfolder()
26+
2427
// Try to detect values from environment
25-
name := detectServerName()
28+
name := detectServerName(subfolder)
2629
description := detectDescription()
2730
version := "1.0.0"
2831
repoURL := detectRepoURL()
@@ -55,7 +58,7 @@ func InitCommand() error {
5558

5659
// Create the server structure
5760
server := createServerJSON(
58-
name, description, version, repoURL, repoSource,
61+
name, description, version, repoURL, repoSource, subfolder,
5962
packageType, packageIdentifier, version, envVars,
6063
)
6164

@@ -82,6 +85,50 @@ func InitCommand() error {
8285
return nil
8386
}
8487

88+
func detectSubfolder() string {
89+
// Get current working directory
90+
cwd, err := os.Getwd()
91+
if err != nil {
92+
return ""
93+
}
94+
95+
// Find git repository root
96+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
97+
defer cancel()
98+
cmd := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel")
99+
cmd.Dir = cwd
100+
output, err := cmd.Output()
101+
if err != nil {
102+
// Not in a git repository
103+
return ""
104+
}
105+
106+
gitRoot := strings.TrimSpace(string(output))
107+
108+
// Clean the paths to ensure proper comparison
109+
gitRoot = filepath.Clean(gitRoot)
110+
cwd = filepath.Clean(cwd)
111+
112+
// If we're in the root, no subfolder
113+
if gitRoot == cwd {
114+
return ""
115+
}
116+
117+
// Check if cwd is actually within gitRoot
118+
if !strings.HasPrefix(cwd, gitRoot) {
119+
return ""
120+
}
121+
122+
// Calculate relative path from git root to current directory
123+
relPath, err := filepath.Rel(gitRoot, cwd)
124+
if err != nil {
125+
return ""
126+
}
127+
128+
// Convert to forward slashes for consistency (important for cross-platform)
129+
return filepath.ToSlash(relPath)
130+
}
131+
85132
func getNameFromPackageJSON() string {
86133
data, err := os.ReadFile("package.json")
87134
if err != nil {
@@ -109,7 +156,7 @@ func getNameFromPackageJSON() string {
109156
return fmt.Sprintf("io.github.<your-username>/%s", name)
110157
}
111158

112-
func detectServerName() string {
159+
func detectServerName(subfolder string) string {
113160
// Try to get from git remote
114161
repoURL := detectRepoURL()
115162
if repoURL != "" {
@@ -119,6 +166,15 @@ func detectServerName() string {
119166
if len(parts) >= 5 {
120167
owner := parts[3]
121168
repo := strings.TrimSuffix(parts[4], ".git")
169+
170+
// If we're in a subdirectory, use the current folder name
171+
if subfolder != "" {
172+
if cwd, err := os.Getwd(); err == nil {
173+
folderName := filepath.Base(cwd)
174+
return fmt.Sprintf("io.github.%s/%s", owner, folderName)
175+
}
176+
}
177+
122178
return fmt.Sprintf("io.github.%s/%s", owner, repo)
123179
}
124180
}
@@ -263,7 +319,7 @@ func detectPackageIdentifier(serverName string, packageType string) string {
263319
}
264320

265321
func createServerJSON(
266-
name, description, version, repoURL, repoSource,
322+
name, description, version, repoURL, repoSource, subfolder,
267323
packageType, packageIdentifier, packageVersion string,
268324
envVars []model.KeyValueInput,
269325
) apiv0.ServerJSON {
@@ -299,17 +355,25 @@ func createServerJSON(
299355
},
300356
}
301357

358+
// Create repository with optional subfolder
359+
repo := model.Repository{
360+
URL: repoURL,
361+
Source: repoSource,
362+
}
363+
364+
// Only set subfolder if we're actually in a subdirectory
365+
if subfolder != "" {
366+
repo.Subfolder = subfolder
367+
}
368+
302369
// Create server structure
303370
return apiv0.ServerJSON{
304371
Schema: "https://static.modelcontextprotocol.io/schemas/2025-09-16/server.schema.json",
305372
Name: name,
306373
Description: description,
307374
Status: model.StatusActive,
308-
Repository: model.Repository{
309-
URL: repoURL,
310-
Source: repoSource,
311-
},
312-
Version: version,
313-
Packages: []model.Package{pkg},
375+
Repository: repo,
376+
Version: version,
377+
Packages: []model.Package{pkg},
314378
}
315379
}

0 commit comments

Comments
 (0)