Skip to content

Commit ca3da90

Browse files
Output new ServerID after publishing (#359)
When using `mcp-publisher publish ...`, output the new server `ID` if publishing is successful. ## Motivation and Context It's useful to know this during testing. It means that you don't have to go and search for the new server ID afterwards. Once we have the `v0/servers/{name}/{version}`, I think we could consider removing this again. ## How Has This Been Tested? I've been using this while I test publishing to locally running registries. ## Breaking Changes none ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [ ] New and existing tests pass locally - [x] I have added appropriate error handling - [x] I have added or updated documentation as needed
1 parent 35ab2bd commit ca3da90

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

cmd/publisher/commands/publish.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,31 @@ func PublishCommand(args []string) error {
6565

6666
// Publish to registry
6767
_, _ = fmt.Fprintf(os.Stdout, "Publishing to %s...\n", registryURL)
68-
if err := publishToRegistry(registryURL, serverData, token); err != nil {
68+
response, err := publishToRegistry(registryURL, serverData, token)
69+
if err != nil {
6970
return fmt.Errorf("publish failed: %w", err)
7071
}
7172

7273
_, _ = fmt.Fprintln(os.Stdout, "✓ Successfully published")
74+
if serverID := response.GetID(); serverID != "" {
75+
_, _ = fmt.Fprintf(os.Stdout, "✓ Server Id %s", serverID)
76+
}
77+
7378
return nil
7479
}
7580

76-
func publishToRegistry(registryURL string, serverData []byte, token string) error {
81+
func publishToRegistry(registryURL string, serverData []byte, token string) (*apiv0.ServerJSON, error) {
7782
// Parse the server JSON data
7883
var serverJSON apiv0.ServerJSON
7984
err := json.Unmarshal(serverData, &serverJSON)
8085
if err != nil {
81-
return fmt.Errorf("error parsing server.json file: %w", err)
86+
return nil, fmt.Errorf("error parsing server.json file: %w", err)
8287
}
8388

8489
// Convert to JSON
8590
jsonData, err := json.Marshal(serverJSON)
8691
if err != nil {
87-
return fmt.Errorf("error serializing request: %w", err)
92+
return nil, fmt.Errorf("error serializing request: %w", err)
8893
}
8994

9095
// Ensure URL ends with the publish endpoint
@@ -96,27 +101,31 @@ func publishToRegistry(registryURL string, serverData []byte, token string) erro
96101
// Create and send request
97102
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, publishURL, bytes.NewBuffer(jsonData))
98103
if err != nil {
99-
return fmt.Errorf("error creating request: %w", err)
104+
return nil, fmt.Errorf("error creating request: %w", err)
100105
}
101106
req.Header.Set("Content-Type", "application/json")
102107
req.Header.Set("Authorization", "Bearer "+token)
103108

104109
client := &http.Client{}
105110
resp, err := client.Do(req)
106111
if err != nil {
107-
return fmt.Errorf("error sending request: %w", err)
112+
return nil, fmt.Errorf("error sending request: %w", err)
108113
}
109114
defer resp.Body.Close()
110115

111116
// Read response
112117
body, err := io.ReadAll(resp.Body)
113118
if err != nil {
114-
return fmt.Errorf("error reading response: %w", err)
119+
return nil, fmt.Errorf("error reading response: %w", err)
115120
}
116121

117122
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
118-
return fmt.Errorf("server returned status %d: %s", resp.StatusCode, body)
123+
return nil, fmt.Errorf("server returned status %d: %s", resp.StatusCode, body)
119124
}
120125

121-
return nil
126+
if err := json.Unmarshal(body, &serverJSON); err != nil {
127+
return nil, err
128+
}
129+
130+
return &serverJSON, nil
122131
}

pkg/api/v0/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ type Metadata struct {
4444
NextCursor string `json:"next_cursor,omitempty"`
4545
Count int `json:"count"`
4646
}
47+
48+
func (s *ServerJSON) GetID() string {
49+
if s.Meta != nil && s.Meta.Official != nil {
50+
return s.Meta.Official.ID
51+
}
52+
return ""
53+
}

0 commit comments

Comments
 (0)