End-to-end publish test using examples from docs (#199)

This commit is contained in:
Charles Lowell 2025-07-25 16:09:17 -07:00 • committed by GitHub
commit 4f5e85b510
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
@ -65,15 +66,18 @@ func main() {
return
}
command := os.Args[1]
switch command {
var err error
switch os.Args[1] {
case "publish":
publishCommand()
err = publishCommand()
case "create":
createCommand()
err = createCommand()
default:
printUsage()
}
if err != nil {
log.Fatal(err)
}
}
func printUsage() {
@ -86,7 +90,7 @@ func printUsage() {
fmt.Fprint(os.Stdout, "Use 'mcp-publisher <command> --help' for more information about a command.\n")
}
func publishCommand() {
func publishCommand() error {
publishFlags := flag.NewFlagSet("publish", flag.ExitOnError)
var registryURL string
@ -119,14 +123,13 @@ func publishCommand() {
if registryURL == "" || mcpFilePath == "" {
publishFlags.Usage()
return
return errors.New("registry-url and mcp-file are required")
}
// Read MCP file
mcpData, err := os.ReadFile(mcpFilePath)
if err != nil {
log.Printf("Error reading MCP file: %s\n", err.Error())
return
return fmt.Errorf("error reading MCP file: %w", err)
}
var authProvider auth.Provider // Determine the authentication method
@ -135,8 +138,7 @@ func publishCommand() {
log.Println("Using GitHub OAuth for authentication")
authProvider = github.NewOAuthProvider(forceLogin, registryURL)
default:
log.Printf("Unsupported authentication method: %s\n", authMethod)
return
return fmt.Errorf("unsupported authentication method: %s", authMethod)
}
// Check if login is needed and perform authentication
@ -144,29 +146,27 @@ func publishCommand() {
if authProvider.NeedsLogin() {
err := authProvider.Login(ctx)
if err != nil {
log.Printf("Failed to authenticate with %s: %s\n", authProvider.Name(), err.Error())
return
return fmt.Errorf("failed to authenticate with %s: %w", authProvider.Name(), err)
}
}
// Get the token
token, err := authProvider.GetToken(ctx)
if err != nil {
log.Printf("Error getting token from %s: %s\n", authProvider.Name(), err.Error())
return
return fmt.Errorf("error getting token from %s: %w", authProvider.Name(), err)
}
// Publish to registry
err = publishToRegistry(registryURL, mcpData, token)
if err != nil {
log.Printf("Failed to publish to registry: %s\n", err.Error())
return
return fmt.Errorf("failed to publish to registry: %w", err)
}
log.Println("Successfully published to registry!")
return nil
}
func createCommand() {
func createCommand() error {
createFlags := flag.NewFlagSet("create", flag.ExitOnError)
// Basic server information flags
@ -246,13 +246,13 @@ func createCommand() {
// Validate required flags
if name == "" {
log.Fatal("Error: --name/-n is required")
return errors.New("--name/-n is required")
}
if description == "" {
log.Fatal("Error: --description/-d is required")
return errors.New("--description/-d is required")
}
if repoURL == "" {
log.Fatal("Error: --repo-url is required")
return errors.New("--repo-url is required")
}
// Set defaults
@ -270,13 +270,13 @@ func createCommand() {
// Convert to JSON
jsonData, err := json.MarshalIndent(server, "", " ")
if err != nil {
log.Fatalf("Error marshaling JSON: %v", err)
return fmt.Errorf("error marshaling JSON: %w", err)
}
// Write to file
err = os.WriteFile(output, jsonData, 0600)
if err != nil {
log.Fatalf("Error writing file: %v", err)
return fmt.Errorf("error writing file: %w", err)
}
log.Printf("Successfully created %s", output)
@ -285,6 +285,7 @@ func createCommand() {
log.Println(" - Set environment variable requirements")
log.Println(" - Add remote server configurations")
log.Println(" - Adjust runtime arguments")
return nil
}
// publishToRegistry sends the MCP server details to the registry with authentication