Skip to content

Commit 14c6f40

Browse files
authored
Support registry_url in mcpjungle config file (#121)
1 parent f314599 commit 14c6f40

File tree

6 files changed

+35
-19
lines changed

6 files changed

+35
-19
lines changed

client/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ func NewClient(baseURL string, accessToken string, httpClient *http.Client) *Cli
2424
}
2525
}
2626

27+
// BaseURL returns the base URL of the MCPJungle server
28+
func (c *Client) BaseURL() string {
29+
return c.baseURL
30+
}
31+
2732
// constructAPIEndpoint constructs the full API endpoint URL where a request must be sent
2833
func (c *Client) constructAPIEndpoint(suffixPath string) (string, error) {
2934
return url.JoinPath(c.baseURL, api.V0ApiPathPrefix, suffixPath)

cmd/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const ClientConfigFileName = ".mcpjungle.conf"
1313
// ClientConfig represents the MCPJungle client configuration stored in the user's home directory.
1414
// It can contain configuration for both a standard user and an admin user.
1515
type ClientConfig struct {
16+
// RegistryURL is the URL of the MCPJungle server.
17+
RegistryURL string `yaml:"registry_url"`
18+
// AccessToken is the access token used for authentication with the MCPJungle server.
1619
AccessToken string `yaml:"access_token"`
1720
}
1821

cmd/config/config_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@ import (
88
"github.com/mcpjungle/mcpjungle/pkg/testhelpers"
99
)
1010

11-
func TestClientConfigFileName(t *testing.T) {
12-
t.Run("ClientConfigFileName has expected value", func(t *testing.T) {
13-
expected := ".mcpjungle.conf"
14-
if ClientConfigFileName != expected {
15-
t.Errorf("Expected ClientConfigFileName to be '%s', got '%s'", expected, ClientConfigFileName)
16-
}
17-
})
18-
}
19-
2011
func TestClientConfig(t *testing.T) {
2112
t.Run("ClientConfig struct has AccessToken field", func(t *testing.T) {
2213
cfg := &ClientConfig{}

cmd/init_server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func runInitServer(cmd *cobra.Command, args []string) error {
3737

3838
// Create new client configuration
3939
cfg := &config.ClientConfig{
40+
RegistryURL: apiClient.BaseURL(),
4041
AccessToken: resp.AdminAccessToken,
4142
}
4243
if err := config.Save(cfg); err != nil {

cmd/root.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,32 @@ func Execute() error {
110110
// Initialize the API client with the registry server URL & client configuration (if any)
111111
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
112112
cfg := config.Load()
113-
apiClient = client.NewClient(registryServerURL, cfg.AccessToken, http.DefaultClient)
113+
114+
// determine the registry server URL to use
115+
// precedence: command line flag explicitly set by user > config file > flag default value
116+
var u string
117+
if cmd.Flags().Changed("registry") {
118+
u = registryServerURL
119+
120+
// if the user explicitly set the --registry flag, but the config file doesn't have
121+
// a registry_url entry, print a tip to let them know they can set it in the config file
122+
if cfg.RegistryURL == "" {
123+
if cfgFilePath, err := config.AbsPath(); err == nil {
124+
cmd.Printf(
125+
"TIP: You can set `registry_url: %s` in %s to avoid setting the --registry flag every time.\n\n",
126+
registryServerURL,
127+
cfgFilePath,
128+
)
129+
}
130+
}
131+
132+
} else if cfg.RegistryURL != "" {
133+
u = cfg.RegistryURL
134+
} else {
135+
u = registryServerURL
136+
}
137+
138+
apiClient = client.NewClient(u, cfg.AccessToken, http.DefaultClient)
114139
}
115140

116141
return rootCmd.Execute()

cmd/root_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,3 @@ func TestRootCommandStructure(t *testing.T) {
1212
t.Errorf("Expected root command Short to be 'MCP Gateway for AI Agents', got %s", rootCmd.Short)
1313
}
1414
}
15-
16-
func TestSubCommandGroups(t *testing.T) {
17-
if subCommandGroupBasic != "basic" {
18-
t.Errorf("Expected subCommandGroupBasic to be 'basic', got %s", subCommandGroupBasic)
19-
}
20-
if subCommandGroupAdvanced != "advanced" {
21-
t.Errorf("Expected subCommandGroupAdvanced to be 'advanced', got %s", subCommandGroupAdvanced)
22-
}
23-
}

0 commit comments

Comments
 (0)