Skip to content

Commit 6673274

Browse files
adwsinghrhernandez35
authored andcommitted
Add validation to generic mcp creation and installation to avoid creating a self-referencing executable
1 parent b68803c commit 6673274

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ public static McpBundleConfig addMcpBundle(Config config, String id, Bundle bund
312312
.build());
313313
case genericBundle -> {
314314
GenericBundle genericBundle = bundle.getValue();
315+
validate(genericBundle);
315316
install(genericBundle.getInstall());
316317
builder.genericConfig(
317318
GenericToolBundleConfig.builder()
@@ -330,6 +331,15 @@ public static McpBundleConfig addMcpBundle(Config config, String id, Bundle bund
330331
return mcpBundleConfig;
331332
}
332333

334+
private static void validate(GenericBundle genericBundle) {
335+
if (!genericBundle.isExecuteDirectly() &&
336+
genericBundle.getRun().getExecutable().equals(genericBundle.getMetadata().getId())) {
337+
throw new IllegalStateException(
338+
"The generic MCP run command has the same value as id which isn't allowed.");
339+
}
340+
341+
}
342+
333343
private static void install(List<ExecSpec> execSpecs) {
334344

335345
for (var execSpec : execSpecs) {
@@ -511,7 +521,7 @@ public static void createWrapperAndUpdateClientConfigs(
511521
boolean shouldCreateWrapper = true;
512522
List<String> args = List.of();
513523
String command = id;
514-
if (bundle.getValue() instanceof GenericBundle genericBundle) {
524+
if (bundle.getValue() instanceof GenericBundle genericBundle && genericBundle.isExecuteDirectly()) {
515525
command = genericBundle.getRun().getExecutable();
516526
args = genericBundle.getRun().getArgs();
517527
shouldCreateWrapper = false;

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected Bundle getNewBundle(CreateGenericBundleInput input) {
3939
.description(input.description)
4040
.build())
4141
.artifact(new GenericArtifact.EmptyMember())
42-
.executeDirectly(true);
42+
.executeDirectly(input.executeDirectly);
4343

4444
// Parse and add install commands
4545
if (input.installCommands != null && !input.installCommands.isEmpty()) {
@@ -52,6 +52,10 @@ protected Bundle getNewBundle(CreateGenericBundleInput input) {
5252

5353
// Parse and add run command
5454
if (input.runCommand != null) {
55+
if (!input.executeDirectly && input.runCommand.equals(input.id)) {
56+
throw new IllegalArgumentException("Run command cannot be the same as the id for MCPs that are not " +
57+
"executed directly");
58+
}
5559
genericBundleBuilder.run(parseExecSpec(input.runCommand));
5660
}
5761

@@ -98,5 +102,8 @@ public static class CreateGenericBundleInput extends CreateBundleInput {
98102
"Example: --run 'node server.js' or --run 'python main.py'",
99103
required = true)
100104
protected String runCommand;
105+
106+
@Option(names = "--execute-directly", description = "Whether to execute the MCP server directly or via the CLI")
107+
protected boolean executeDirectly = true; //TODO make this false once our MCP proxy is fully fleshed out.
101108
}
102109
}

0 commit comments

Comments
 (0)