Skip to content

Commit e6ce958

Browse files
authored
Fix Registry selection, inject Registry into the command (#705)
* Fix Registry selection, inject Registry into the command
1 parent 2085597 commit e6ce958

File tree

7 files changed

+79
-40
lines changed

7 files changed

+79
-40
lines changed

mcp/mcp-cli-api/src/main/java/software/amazon/smithy/java/mcp/cli/AbstractAddBundle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package software.amazon.smithy.java.mcp.cli;
77

88
import java.util.Set;
9-
import software.amazon.smithy.java.mcp.cli.model.Config;
109
import software.amazon.smithy.java.mcp.cli.model.Location;
1110

1211
/**
@@ -19,7 +18,8 @@
1918
public abstract class AbstractAddBundle extends SmithyMcpCommand implements ConfigurationCommand {
2019

2120
@Override
22-
public final void execute(Config config) throws Exception {
21+
public final void execute(ExecutionContext context) throws Exception {
22+
var config = context.config();
2323
if (!canOverwrite() && config.getToolBundles().containsKey(getToolBundleName())) {
2424
throw new IllegalArgumentException("Tool bundle " + getToolBundleName()
2525
+ " already exists. Either choose a new name or pass --overwrite to overwrite the existing tool bundle");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.java.mcp.cli;
7+
8+
import software.amazon.smithy.java.mcp.cli.model.Config;
9+
import software.amazon.smithy.mcp.bundle.api.Registry;
10+
11+
public record ExecutionContext(Config config, Registry registry) {}

mcp/mcp-cli-api/src/main/java/software/amazon/smithy/java/mcp/cli/RegistryUtils.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
package software.amazon.smithy.java.mcp.cli;
77

88
import java.util.Map;
9+
import java.util.Objects;
910
import java.util.ServiceLoader;
1011
import java.util.ServiceLoader.Provider;
1112
import java.util.function.Function;
1213
import java.util.stream.Collectors;
14+
import software.amazon.smithy.java.mcp.cli.model.Config;
1315
import software.amazon.smithy.mcp.bundle.api.Registry;
1416

15-
public class RegistryUtils {
17+
class RegistryUtils {
1618

1719
private RegistryUtils() {}
1820

@@ -25,17 +27,20 @@ private RegistryUtils() {}
2527
.collect(Collectors.toMap(Registry::name, Function.identity()));
2628
}
2729

28-
public static Registry getRegistry(String name) {
30+
static Registry getRegistry(String name, Config config) {
31+
2932
if (name == null) {
30-
return getRegistry();
33+
name = Objects.requireNonNull(config.getDefaultRegistry(),
34+
"Either configure a default registry or the registry name is required.");
35+
}
36+
37+
if (!config.getRegistries().containsKey(name)) {
38+
throw new IllegalArgumentException("The registry '" + name + "' does not exist.");
3139
}
40+
3241
if (JAVA_REGISTRIES.containsKey(name)) {
3342
return JAVA_REGISTRIES.get(name);
3443
}
3544
throw new IllegalStateException("No such registry: " + name);
3645
}
37-
38-
public static Registry getRegistry() {
39-
return JAVA_REGISTRIES.values().iterator().next();
40-
}
4146
}

mcp/mcp-cli-api/src/main/java/software/amazon/smithy/java/mcp/cli/SmithyMcpCommand.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public abstract class SmithyMcpCommand implements Callable<Integer> {
2626
public final Integer call() throws Exception {
2727
try {
2828
var config = loadOrCreateConfig();
29-
execute(config);
29+
execute(new ExecutionContext(config, RegistryUtils.getRegistry(registryToUse(config), config)));
3030
return 0;
3131
} catch (IllegalArgumentException e) {
3232
System.out.println("Invalid input : [" + e.getMessage() + "]");
@@ -42,8 +42,12 @@ public final Integer call() throws Exception {
4242
* <p>
4343
* Subclasses must implement this method to provide command-specific functionality.
4444
*
45-
* @param config The MCP configuration
45+
* @param context {@link ExecutionContext}
4646
* @throws Exception If an error occurs during execution
4747
*/
48-
protected abstract void execute(Config config) throws Exception;
48+
protected abstract void execute(ExecutionContext context) throws Exception;
49+
50+
protected String registryToUse(Config config) {
51+
return config.getDefaultRegistry();
52+
}
4953
}

mcp/mcp-cli/src/main/java/software/amazon/smithy/java/mcp/cli/commands/InstallBundle.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import software.amazon.smithy.java.json.JsonCodec;
1919
import software.amazon.smithy.java.json.JsonSettings;
2020
import software.amazon.smithy.java.mcp.cli.ConfigUtils;
21-
import software.amazon.smithy.java.mcp.cli.RegistryUtils;
21+
import software.amazon.smithy.java.mcp.cli.ExecutionContext;
2222
import software.amazon.smithy.java.mcp.cli.SmithyMcpCommand;
2323
import software.amazon.smithy.java.mcp.cli.model.Config;
2424
import software.amazon.smithy.java.mcp.cli.model.McpServerConfig;
@@ -35,7 +35,7 @@ public class InstallBundle extends SmithyMcpCommand {
3535

3636
@Option(names = {"-r", "--registry"},
3737
description = "Name of the registry to list the bundles from. If not provided it will use the default registry.")
38-
String registry;
38+
String registryName;
3939

4040
@Option(names = {"-n", "--name"}, description = "Name of the MCP Bundle to install.")
4141
String name;
@@ -49,11 +49,10 @@ public class InstallBundle extends SmithyMcpCommand {
4949
boolean print;
5050

5151
@Override
52-
protected void execute(Config config) throws IOException {
53-
if (registry != null && !config.getRegistries().containsKey(registry)) {
54-
throw new IllegalArgumentException("The registry '" + registry + "' does not exist.");
55-
}
56-
var bundle = RegistryUtils.getRegistry(registry).getMcpBundle(name);
52+
protected void execute(ExecutionContext context) throws IOException {
53+
var registry = context.registry();
54+
var config = context.config();
55+
var bundle = registry.getMcpBundle(name);
5756
ConfigUtils.addMcpBundle(config, name, bundle);
5857
var newConfig = McpServerConfig.builder().command("mcp-registry").args(List.of("start-server", name)).build();
5958
if (print) {
@@ -80,4 +79,9 @@ protected void execute(Config config) throws IOException {
8079
StandardOpenOption.TRUNCATE_EXISTING);
8180
}
8281
}
82+
83+
@Override
84+
protected String registryToUse(Config config) {
85+
return registryName;
86+
}
8387
}

mcp/mcp-cli/src/main/java/software/amazon/smithy/java/mcp/cli/commands/ListBundles.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import static picocli.CommandLine.Command;
99

1010
import picocli.CommandLine.Option;
11-
import software.amazon.smithy.java.mcp.cli.RegistryUtils;
11+
import software.amazon.smithy.java.mcp.cli.ExecutionContext;
1212
import software.amazon.smithy.java.mcp.cli.SmithyMcpCommand;
1313
import software.amazon.smithy.java.mcp.cli.model.Config;
1414

@@ -20,12 +20,8 @@ public class ListBundles extends SmithyMcpCommand {
2020
String registryName;
2121

2222
@Override
23-
protected void execute(Config config) {
24-
if (registryName != null && !config.getRegistries().containsKey(registryName)) {
25-
throw new IllegalArgumentException("The registry '" + registryName + "' does not exist.");
26-
}
27-
28-
var registry = registryName != null ? RegistryUtils.getRegistry(registryName) : RegistryUtils.getRegistry();
23+
protected void execute(ExecutionContext context) {
24+
var registry = context.registry();
2925
registry
3026
.listMcpBundles()
3127
.forEach(bundle -> {
@@ -39,4 +35,9 @@ protected void execute(Config config) {
3935
});
4036

4137
}
38+
39+
@Override
40+
protected String registryToUse(Config config) {
41+
return registryName;
42+
}
4243
}

mcp/mcp-cli/src/main/java/software/amazon/smithy/java/mcp/cli/commands/StartServer.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
import picocli.CommandLine.Option;
1414
import picocli.CommandLine.Parameters;
1515
import software.amazon.smithy.java.mcp.cli.ConfigUtils;
16-
import software.amazon.smithy.java.mcp.cli.RegistryUtils;
16+
import software.amazon.smithy.java.mcp.cli.ExecutionContext;
1717
import software.amazon.smithy.java.mcp.cli.SmithyMcpCommand;
18-
import software.amazon.smithy.java.mcp.cli.model.Config;
1918
import software.amazon.smithy.java.mcp.cli.model.Location;
2019
import software.amazon.smithy.java.mcp.cli.model.McpBundleConfig;
2120
import software.amazon.smithy.java.mcp.cli.model.SmithyModeledBundleConfig;
@@ -33,6 +32,7 @@
3332
import software.amazon.smithy.java.server.RequestContext;
3433
import software.amazon.smithy.java.server.Service;
3534
import software.amazon.smithy.mcp.bundle.api.McpBundles;
35+
import software.amazon.smithy.mcp.bundle.api.Registry;
3636
import software.amazon.smithy.mcp.bundle.api.model.BundleMetadata;
3737

3838
/**
@@ -59,30 +59,30 @@ public final class StartServer extends SmithyMcpCommand {
5959
* Loads the requested tool bundles from configuration, creates appropriate services,
6060
* and starts the MCP server.
6161
*
62-
* @param config The MCP configuration
62+
* @param context {@link ExecutionContext}
6363
* @throws IllegalArgumentException If no tool bundles are configured or requested bundles not found
6464
*/
6565
@Override
66-
public void execute(Config config) throws IOException {
66+
public void execute(ExecutionContext context) throws IOException {
67+
68+
var config = context.config();
6769
// By default, load all available tools
6870
if (toolBundles == null || toolBundles.isEmpty()) {
69-
try {
70-
toolBundles = new ArrayList<>(ConfigUtils.loadOrCreateConfig().getToolBundles().keySet());
71-
} catch (IOException e) {
72-
throw new RuntimeException(e);
73-
}
71+
toolBundles = new ArrayList<>(config.getToolBundles().keySet());
7472
}
7573

7674
if (toolBundles.isEmpty() && !registryServer) {
7775
throw new IllegalStateException("No bundles installed");
7876
}
7977

78+
var registry = context.registry();
79+
8080
List<McpBundleConfig> toolBundleConfigs = new ArrayList<>(toolBundles.size());
8181

8282
for (var toolBundle : toolBundles) {
8383
var toolBundleConfig = config.getToolBundles().get(toolBundle);
8484
if (toolBundleConfig == null) {
85-
var bundle = RegistryUtils.getRegistry().getMcpBundle(toolBundle);
85+
var bundle = registry.getMcpBundle(toolBundle);
8686
if (bundle == null) {
8787
throw new IllegalArgumentException("Can't find a configured tool bundle for '" + toolBundle + "'.");
8888
} else {
@@ -107,8 +107,8 @@ public void execute(Config config) throws IOException {
107107

108108
if (registryServer) {
109109
services.add(McpRegistry.builder()
110-
.addInstallServerOperation(new InstallOp())
111-
.addListServersOperation(new ListOp())
110+
.addInstallServerOperation(new InstallOp(registry))
111+
.addListServersOperation(new ListOp(registry))
112112
.build());
113113
}
114114

@@ -141,9 +141,16 @@ private static Service bundleToService(McpBundleConfig toolBundleConfig) {
141141
}
142142

143143
private static final class ListOp implements ListServersOperation {
144+
145+
private final Registry registry;
146+
147+
private ListOp(Registry registry) {
148+
this.registry = registry;
149+
}
150+
144151
@Override
145152
public ListServersOutput listServers(ListServersInput input, RequestContext context) {
146-
var servers = RegistryUtils.getRegistry()
153+
var servers = registry
147154
.listMcpBundles()
148155
.stream()
149156
.unordered()
@@ -159,12 +166,19 @@ public ListServersOutput listServers(ListServersInput input, RequestContext cont
159166
}
160167

161168
private final class InstallOp implements InstallServerOperation {
169+
170+
private final Registry registry;
171+
172+
private InstallOp(Registry registry) {
173+
this.registry = registry;
174+
}
175+
162176
@Override
163177
public InstallServerOutput installServer(InstallServerInput input, RequestContext context) {
164178
try {
165179
var config = ConfigUtils.loadOrCreateConfig();
166180
if (!config.getToolBundles().containsKey(input.getServerName())) {
167-
var bundle = RegistryUtils.getRegistry().getMcpBundle(input.getServerName());
181+
var bundle = registry.getMcpBundle(input.getServerName());
168182
if (bundle == null) {
169183
throw new IllegalArgumentException(
170184
"Can't find a configured tool bundle for '" + input.getServerName() + "'.");

0 commit comments

Comments
 (0)