feat(studio): integrate project MCP setup and lifecycle
Intrepid/Prometeu/Studio/pipeline/pr-master This commit looks good
Test / Build skipped: 15, passed: 926
Intrepid/Prometeu/Studio/pipeline/head This commit looks good

This commit is contained in:
2026-09-29 19:24:28 +01:00
parent 97cf5e1919
commit d0737df0ac
48 changed files with 2048 additions and 225 deletions
@@ -0,0 +1,115 @@
package p.studio.app.mcp;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.*;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
class ProjectConnectionPublisherTest {
@TempDir Path temporary;
final ObjectMapper mapper = new ObjectMapper();
LocalMcpServer server;
Path root;
@BeforeEach void start() throws Exception {
root = temporary.toRealPath();
server = new LocalMcpServer(mapper, new McpToolRegistry(new McpToolCatalog(mapper), mapper, Map.of())); server.start();
}
@AfterEach void stop() { server.close(); }
@Test void publishesPrivatelyOnlyActivatesExplicitlyAndCleansUpOwnedGeneration() throws Exception {
Files.writeString(root.resolve(".gitignore"), "# existing\n/build/\n");
var publisher = new ProjectConnectionPublisher(server, mapper);
var publication = publisher.prepare(root);
publication.publish();
var descriptor = ProjectConnectionFiles.read(root, mapper);
assertNull(server.credentials().authenticate("Bearer " + descriptor.credential()));
publication.activate();
assertEquals(root, server.credentials().authenticate("Bearer " + descriptor.credential()).root());
var file = root.resolve(ProjectConnectionFiles.RELATIVE);
assertEquals(PosixFilePermissions.fromString("rw-------"), Files.getPosixFilePermissions(file));
assertEquals(PosixFilePermissions.fromString("rwx------"), Files.getPosixFilePermissions(file.getParent()));
assertFalse(descriptor.toString().contains(descriptor.credential()));
assertThrows(Exception.class, () -> publisher.prepare(root));
publication.invalidate(); assertNull(server.credentials().authenticate("Bearer " + descriptor.credential()));
publication.close(); assertFalse(Files.exists(file)); assertTrue(Files.exists(file.resolveSibling("publication.lock")));
try (var next = publisher.prepare(root)) {
next.publish(); next.activate(); publication.close();
assertTrue(Files.exists(file));
assertNotEquals(descriptor.credential(), ProjectConnectionFiles.read(root, mapper).credential());
}
assertEquals("# existing\n/build/\n/.studio/mcp/\n", Files.readString(root.resolve(".gitignore")));
}
@Test void unsafePathsAndPermissionsNeverPublishSecrets() throws Exception {
Path destination = Files.createDirectory(root.resolve("outside"));
Files.createSymbolicLink(root.resolve(".studio"), destination);
assertThrows(Exception.class, () -> new ProjectConnectionPublisher(server, mapper).prepare(root));
assertFalse(Files.exists(destination.resolve("mcp")));
Files.delete(root.resolve(".studio"));
Path directory = ProjectConnectionFiles.directory(root);
Files.setPosixFilePermissions(directory, PosixFilePermissions.fromString("rwxr-xr-x"));
assertThrows(Exception.class, () -> new ProjectConnectionPublisher(server, mapper).prepare(root));
}
@Test void malformedDescriptorsNeverSupplyAnArbitraryEndpoint() throws Exception {
try (var publication = new ProjectConnectionPublisher(server, mapper).prepare(root)) {
publication.publish();
Path path = root.resolve(ProjectConnectionFiles.RELATIVE);
var valid = mapper.readTree(Files.readString(path));
for (String endpoint : java.util.List.of("https://example.com/mcp", "http://127.0.0.1:123/mcp?x=1", "http://localhost:123/mcp")) {
var bad = valid.deepCopy(); ((com.fasterxml.jackson.databind.node.ObjectNode) bad).put("endpoint", endpoint);
Files.writeString(path, bad.toString()); assertThrows(Exception.class, () -> ProjectConnectionFiles.read(root, mapper));
}
Files.writeString(path, valid.toString());
}
}
@Test void osLockRejectsAnotherJvmAndCrashAllowsRecoveryWithoutUnlinkingLock() throws Exception {
Process owner = ownerProcess();
try {
assertEquals("READY", java.util.concurrent.CompletableFuture.supplyAsync(() -> {
try { return owner.inputReader().readLine(); } catch (java.io.IOException failure) { throw new java.io.UncheckedIOException(failure); }
}).get(10, java.util.concurrent.TimeUnit.SECONDS));
var lock = root.resolve(".studio/mcp/publication.lock");
var inode = Files.readAttributes(lock, java.nio.file.attribute.BasicFileAttributes.class).fileKey();
Process conflict = ownerProcess();
try { assertTrue(conflict.waitFor(10, java.util.concurrent.TimeUnit.SECONDS)); assertEquals(2, conflict.exitValue()); }
finally { conflict.destroyForcibly(); }
owner.destroyForcibly(); assertTrue(owner.waitFor(10, java.util.concurrent.TimeUnit.SECONDS));
assertTrue(Files.exists(root.resolve(ProjectConnectionFiles.RELATIVE)));
try (var recovered = new ProjectConnectionPublisher(server, mapper).prepare(root)) {
recovered.publish(); recovered.activate();
assertEquals(server.instanceId(), ProjectConnectionFiles.read(root, mapper).instanceId());
assertEquals(inode, Files.readAttributes(lock, java.nio.file.attribute.BasicFileAttributes.class).fileKey());
}
} finally { owner.destroyForcibly(); }
}
@Test void trackedDiscoveryTargetIsRejectedWithoutWritingCredentials() throws Exception {
var directory = ProjectConnectionFiles.directory(root);
var target = directory.resolve("connection.json"); Files.writeString(target, "tracked placeholder");
for (var command : java.util.List.of(java.util.List.of("git", "init", "-q"), java.util.List.of("git", "add", "-f", ProjectConnectionFiles.RELATIVE))) {
var process = new ProcessBuilder(command).directory(root.toFile()).redirectError(ProcessBuilder.Redirect.DISCARD).start();
assertTrue(process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS)); assertEquals(0, process.exitValue());
}
assertThrows(Exception.class, () -> new ProjectConnectionPublisher(server, mapper).prepare(root));
assertEquals("tracked placeholder", Files.readString(target));
assertFalse(Files.exists(directory.resolve("publication.lock")));
}
private Process ownerProcess() throws Exception {
return new ProcessBuilder(Path.of(System.getProperty("java.home"), "bin/java").toString(), "-cp",
System.getProperty("studio.test.classpath"), Owner.class.getName(), root.toString())
.redirectError(ProcessBuilder.Redirect.DISCARD).start();
}
public static class Owner {
public static void main(String[] args) throws Exception {
var mapper = new ObjectMapper();
try (var server = new LocalMcpServer(mapper, new McpToolRegistry(new McpToolCatalog(mapper), mapper, Map.of()))) {
server.start();
try (var publication = new ProjectConnectionPublisher(server, mapper).prepare(Path.of(args[0]))) {
publication.publish(); publication.activate(); System.out.println("READY"); System.out.flush(); System.in.read();
} catch (Exception conflict) { System.exit(2); }
}
}
}
}