From 5ff0ef56dfc4b7a39e50d3c869d05e840e204c7d Mon Sep 17 00:00:00 2001 From: Max Kless Date: Mon, 29 Sep 2025 12:22:52 +0200 Subject: [PATCH 1/4] fix: detect server subfolder in init --- cmd/publisher/commands/init.go | 85 ++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 10 deletions(-) diff --git a/cmd/publisher/commands/init.go b/cmd/publisher/commands/init.go index 6b2ac27a..d2263552 100644 --- a/cmd/publisher/commands/init.go +++ b/cmd/publisher/commands/init.go @@ -21,8 +21,11 @@ func InitCommand() error { return errors.New("server.json already exists") } + // Detect if we're in a subdirectory of the git repository + subfolder := detectSubfolder() + // Try to detect values from environment - name := detectServerName() + name := detectServerName(subfolder) description := detectDescription() version := "1.0.0" repoURL := detectRepoURL() @@ -55,7 +58,7 @@ func InitCommand() error { // Create the server structure server := createServerJSON( - name, description, version, repoURL, repoSource, + name, description, version, repoURL, repoSource, subfolder, packageType, packageIdentifier, version, envVars, ) @@ -82,6 +85,50 @@ func InitCommand() error { return nil } +func detectSubfolder() string { + // Get current working directory + cwd, err := os.Getwd() + if err != nil { + return "" + } + + // Find git repository root + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel") + cmd.Dir = cwd + output, err := cmd.Output() + if err != nil { + // Not in a git repository + return "" + } + + gitRoot := strings.TrimSpace(string(output)) + + // Clean the paths to ensure proper comparison + gitRoot = filepath.Clean(gitRoot) + cwd = filepath.Clean(cwd) + + // If we're in the root, no subfolder + if gitRoot == cwd { + return "" + } + + // Check if cwd is actually within gitRoot + if !strings.HasPrefix(cwd, gitRoot) { + return "" + } + + // Calculate relative path from git root to current directory + relPath, err := filepath.Rel(gitRoot, cwd) + if err != nil { + return "" + } + + // Convert to forward slashes for consistency (important for cross-platform) + return filepath.ToSlash(relPath) +} + func getNameFromPackageJSON() string { data, err := os.ReadFile("package.json") if err != nil { @@ -109,7 +156,7 @@ func getNameFromPackageJSON() string { return fmt.Sprintf("io.github./%s", name) } -func detectServerName() string { +func detectServerName(subfolder string) string { // Try to get from git remote repoURL := detectRepoURL() if repoURL != "" { @@ -119,6 +166,15 @@ func detectServerName() string { if len(parts) >= 5 { owner := parts[3] repo := strings.TrimSuffix(parts[4], ".git") + + // If we're in a subdirectory, use the current folder name + if subfolder != "" { + if cwd, err := os.Getwd(); err == nil { + folderName := filepath.Base(cwd) + return fmt.Sprintf("io.github.%s/%s", owner, folderName) + } + } + return fmt.Sprintf("io.github.%s/%s", owner, repo) } } @@ -263,7 +319,7 @@ func detectPackageIdentifier(serverName string, packageType string) string { } func createServerJSON( - name, description, version, repoURL, repoSource, + name, description, version, repoURL, repoSource, subfolder, packageType, packageIdentifier, packageVersion string, envVars []model.KeyValueInput, ) apiv0.ServerJSON { @@ -299,16 +355,25 @@ func createServerJSON( }, } + // Create repository with optional subfolder + repo := model.Repository{ + URL: repoURL, + Source: repoSource, + } + + // Only set subfolder if we're actually in a subdirectory + if subfolder != "" { + repo.Subfolder = subfolder + } + // Create server structure return apiv0.ServerJSON{ Schema: model.CurrentSchemaURL, Name: name, Description: description, - Repository: model.Repository{ - URL: repoURL, - Source: repoSource, - }, - Version: version, - Packages: []model.Package{pkg}, + Status: model.StatusActive, + Repository: repo, + Version: version, + Packages: []model.Package{pkg}, } } From 514c92d49e688a91644fdd8e95cfeff87267d7e0 Mon Sep 17 00:00:00 2001 From: Max Kless Date: Mon, 29 Sep 2025 12:42:32 +0200 Subject: [PATCH 2/4] reduce function complexity --- cmd/publisher/commands/init.go | 42 +++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/cmd/publisher/commands/init.go b/cmd/publisher/commands/init.go index d2263552..b4630b38 100644 --- a/cmd/publisher/commands/init.go +++ b/cmd/publisher/commands/init.go @@ -159,24 +159,10 @@ func getNameFromPackageJSON() string { func detectServerName(subfolder string) string { // Try to get from git remote repoURL := detectRepoURL() - if repoURL != "" { - // Extract owner/repo from GitHub URL - if strings.Contains(repoURL, "github.com") { - parts := strings.Split(repoURL, "/") - if len(parts) >= 5 { - owner := parts[3] - repo := strings.TrimSuffix(parts[4], ".git") - - // If we're in a subdirectory, use the current folder name - if subfolder != "" { - if cwd, err := os.Getwd(); err == nil { - folderName := filepath.Base(cwd) - return fmt.Sprintf("io.github.%s/%s", owner, folderName) - } - } - - return fmt.Sprintf("io.github.%s/%s", owner, repo) - } + if repoURL != "" && strings.Contains(repoURL, "github.com") { + name := buildGitHubServerName(repoURL, subfolder) + if name != "" { + return name } } @@ -194,6 +180,26 @@ func detectServerName(subfolder string) string { return "com.example/my-mcp-server" } +func buildGitHubServerName(repoURL, subfolder string) string { + parts := strings.Split(repoURL, "/") + if len(parts) < 5 { + return "" + } + + owner := parts[3] + repo := strings.TrimSuffix(parts[4], ".git") + + // If we're in a subdirectory, use the current folder name + if subfolder != "" { + if cwd, err := os.Getwd(); err == nil { + folderName := filepath.Base(cwd) + return fmt.Sprintf("io.github.%s/%s", owner, folderName) + } + } + + return fmt.Sprintf("io.github.%s/%s", owner, repo) +} + func detectDescription() string { // Try to get from package.json if data, err := os.ReadFile("package.json"); err == nil { From 0bc91bf33b56675b9793dcba207844aff1be383e Mon Sep 17 00:00:00 2001 From: Max Kless Date: Mon, 6 Oct 2025 14:04:59 +0200 Subject: [PATCH 3/4] fixes after rebase --- cmd/publisher/commands/init.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/publisher/commands/init.go b/cmd/publisher/commands/init.go index b4630b38..5ceb89e4 100644 --- a/cmd/publisher/commands/init.go +++ b/cmd/publisher/commands/init.go @@ -377,7 +377,6 @@ func createServerJSON( Schema: model.CurrentSchemaURL, Name: name, Description: description, - Status: model.StatusActive, Repository: repo, Version: version, Packages: []model.Package{pkg}, From d0c5123019f1ee9afcbb4d47ca8ddbf0a7182609 Mon Sep 17 00:00:00 2001 From: Max Kless Date: Tue, 7 Oct 2025 10:44:24 +0200 Subject: [PATCH 4/4] reuse subfolder string --- cmd/publisher/commands/init.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/publisher/commands/init.go b/cmd/publisher/commands/init.go index 5ceb89e4..c9a2eedc 100644 --- a/cmd/publisher/commands/init.go +++ b/cmd/publisher/commands/init.go @@ -191,10 +191,8 @@ func buildGitHubServerName(repoURL, subfolder string) string { // If we're in a subdirectory, use the current folder name if subfolder != "" { - if cwd, err := os.Getwd(); err == nil { - folderName := filepath.Base(cwd) - return fmt.Sprintf("io.github.%s/%s", owner, folderName) - } + folderName := filepath.Base(subfolder) + return fmt.Sprintf("io.github.%s/%s", owner, folderName) } return fmt.Sprintf("io.github.%s/%s", owner, repo)