Skip to content

Commit a1a3303

Browse files
feat(publisher): copy version from package.json and prefer mcpName in init (#1145)
## Summary Addresses two related enhancements to `mcp-publisher init`: - **Copy `version` from `package.json`** (#736): When `package.json` is present and contains a `version` field, it is now used for both the top-level `version` and `packages[].version` in the generated `server.json`. Falls back to `"1.0.0"` when unavailable. This aligns with the recommended best practice of keeping server version in sync with package version. - **Prefer `mcpName` over `name`** (#737): The init command now checks for `mcpName` in `package.json` first, falling back to `name` if `mcpName` is not defined. This ensures the generated server name matches the required `mcpName` property. Closes #736 Closes #737 ## Changes - `cmd/publisher/commands/init.go`: - Added `getVersionFromPackageJSON()` helper to read version from `package.json` - Modified `InitCommand()` to use detected version with `"1.0.0"` fallback - Modified `getNameFromPackageJSON()` to prefer `mcpName` over `name` ## Test plan - [x] `go build ./cmd/publisher/...` compiles without errors - [x] `go test ./cmd/publisher/commands/...` passes all existing tests - [ ] Manual: run `mcp-publisher init` in a directory with `package.json` containing `version` and `mcpName` — verify both are copied to `server.json` - [ ] Manual: run `mcp-publisher init` in a directory with `package.json` without `mcpName` — verify `name` is used as fallback - [ ] Manual: run `mcp-publisher init` in a directory without `package.json` — verify defaults (`"1.0.0"`, directory name) are used ## AI Disclosure AI assistance (Claude) was used for initial code exploration and issue research. The implementation was written and reviewed by the author. Co-authored-by: Radoslav Dimitrov <radoslav@stacklok.com>
1 parent 35b1243 commit a1a3303

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

cmd/publisher/commands/init.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ func InitCommand() error {
2727
// Try to detect values from environment
2828
name := detectServerName(subfolder)
2929
description := detectDescription()
30-
version := "1.0.0"
30+
version := getVersionFromPackageJSON()
31+
if version == "" {
32+
version = "1.0.0"
33+
}
3134
repoURL := detectRepoURL()
3235
repoSource := MethodGitHub
3336
if repoURL != "" && !strings.Contains(repoURL, "github.com") {
@@ -140,9 +143,13 @@ func getNameFromPackageJSON() string {
140143
return ""
141144
}
142145

143-
name, ok := pkg["name"].(string)
146+
// Prefer mcpName over name, as the server name must match mcpName
147+
name, ok := pkg["mcpName"].(string)
144148
if !ok || name == "" {
145-
return ""
149+
name, ok = pkg["name"].(string)
150+
if !ok || name == "" {
151+
return ""
152+
}
146153
}
147154

148155
// Convert npm package name to MCP server name
@@ -156,6 +163,25 @@ func getNameFromPackageJSON() string {
156163
return fmt.Sprintf("io.github.<your-username>/%s", name)
157164
}
158165

166+
func getVersionFromPackageJSON() string {
167+
data, err := os.ReadFile("package.json")
168+
if err != nil {
169+
return ""
170+
}
171+
172+
var pkg map[string]any
173+
if err := json.Unmarshal(data, &pkg); err != nil {
174+
return ""
175+
}
176+
177+
version, ok := pkg["version"].(string)
178+
if !ok || version == "" {
179+
return ""
180+
}
181+
182+
return version
183+
}
184+
159185
func detectServerName(subfolder string) string {
160186
// Try to get from git remote
161187
repoURL := detectRepoURL()

0 commit comments

Comments
 (0)