diff --git a/src/main/java/rife/bld/operations/McpOperation.java b/src/main/java/rife/bld/operations/McpOperation.java index 909ee06..96841b9 100644 --- a/src/main/java/rife/bld/operations/McpOperation.java +++ b/src/main/java/rife/bld/operations/McpOperation.java @@ -843,7 +843,7 @@ public class McpOperation extends AbstractOperation { resources.object(r -> r .set("uri", RESOURCE_PROJECT) .set("name", "project") - .set("description", "The identification and directory layout of the project") + .set("description", "The identification and directory layout of the project, values that aren't configured are omitted") .set("mimeType", "application/json")); resources.object(r -> r .set("uri", RESOURCE_DEPENDENCIES) @@ -904,24 +904,34 @@ public class McpOperation extends AbstractOperation { /** * Part of the {@link #execute} operation, describes the project * identification and directory layout as JSON. + *

+ * Values that aren't configured are omitted, so that their absence + * can't be mistaken for missing data. * * @return the JSON description of the project * @since 2.4.0 */ protected String readProjectResource() { - return new JsonObject() - .set("name", project_.name()) - .set("version", project_.version() == null ? null : project_.version().toString()) - .set("package", project_.pkg()) - .set("mainClass", project_.mainClass()) - .set("javaRelease", project_.javaRelease()) - .object("directories", d -> d - .set("work", directoryPath(project_.workDirectory())) - .set("srcMainJava", directoryPath(project_.srcMainJavaDirectory())) - .set("srcTestJava", directoryPath(project_.srcTestJavaDirectory())) - .set("buildMain", directoryPath(project_.buildMainDirectory())) - .set("buildTest", directoryPath(project_.buildTestDirectory()))) - .toPrettyString(); + var json = new JsonObject(); + setIfPresent(json, "name", project_.name()); + setIfPresent(json, "version", project_.version() == null ? null : project_.version().toString()); + setIfPresent(json, "package", project_.pkg()); + setIfPresent(json, "mainClass", project_.mainClass()); + setIfPresent(json, "javaRelease", project_.javaRelease()); + var directories = new JsonObject(); + setIfPresent(directories, "work", directoryPath(project_.workDirectory())); + setIfPresent(directories, "srcMainJava", directoryPath(project_.srcMainJavaDirectory())); + setIfPresent(directories, "srcTestJava", directoryPath(project_.srcTestJavaDirectory())); + setIfPresent(directories, "buildMain", directoryPath(project_.buildMainDirectory())); + setIfPresent(directories, "buildTest", directoryPath(project_.buildTestDirectory())); + json.set("directories", directories); + return json.toPrettyString(); + } + + private static void setIfPresent(JsonObject json, String name, Object value) { + if (value != null) { + json.set(name, value); + } } private static String directoryPath(File directory) { diff --git a/src/test/java/rife/bld/operations/TestMcpOperation.java b/src/test/java/rife/bld/operations/TestMcpOperation.java index 7476062..9b3dcb0 100644 --- a/src/test/java/rife/bld/operations/TestMcpOperation.java +++ b/src/test/java/rife/bld/operations/TestMcpOperation.java @@ -823,6 +823,8 @@ public class TestMcpOperation { assertEquals("1.0.0", description.getString("version")); assertEquals("test.pkg", description.getString("package")); assertNotNull(description.getObject("directories").getString("srcMainJava")); + // values that aren't configured are omitted instead of null + assertFalse(description.containsKey("javaRelease")); // the dependencies resource describes the scopes var read_dependencies = process(operation, """