connect DEV to live MCP configuration and verify tool conformance

This commit is contained in:
2026-09-29 10:36:50 +01:00
parent 20bbbd26bd
commit 9592c9b274
16 changed files with 417 additions and 18 deletions
@@ -26,6 +26,15 @@ public final class LocalMcpServer implements AutoCloseable {
private ExecutorService executor;
private ScheduledExecutorService deadlines;
private volatile Connection connection;
private final java.util.List<Runnable> connectionListeners = new CopyOnWriteArrayList<>();
public p.studio.api.operation.OperationSubscription onConnectionChanged(Runnable listener) {
connectionListeners.add(listener); notifyConnectionListener(listener);
return () -> connectionListeners.remove(listener);
}
private static void notifyConnectionListener(Runnable listener) {
try { listener.run(); } catch (RuntimeException ignored) { /* Observation cannot change lifecycle. */ }
}
public LocalMcpServer(ObjectMapper mapper, McpToolRegistry tools) {
this.mapper = mapper.copy().enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION)
@@ -47,6 +56,7 @@ public final class LocalMcpServer implements AutoCloseable {
next.setExecutor(workers);
next.start();
executor = workers; server = next;
connectionListeners.forEach(LocalMcpServer::notifyConnectionListener);
} catch (RuntimeException failure) {
next.stop(0); workers.shutdownNow(); timers.shutdownNow(); connection = null;
throw failure;
@@ -62,6 +72,7 @@ public final class LocalMcpServer implements AutoCloseable {
if (server == null) return;
connection = null; server.stop(0); server = null;
sessions.clear(); executor.shutdownNow(); deadlines.shutdownNow();
connectionListeners.forEach(LocalMcpServer::notifyConnectionListener);
}
/** Only explicit connection/configuration consumers may obtain the bearer token; never log it. */
@@ -144,7 +155,7 @@ public final class LocalMcpServer implements AutoCloseable {
var result = mapper.createObjectNode().put("protocolVersion", PROTOCOL_VERSION);
result.putObject("capabilities").putObject("tools").put("listChanged", false);
result.putObject("serverInfo").put("name", "prometeu-studio-mcp").put("version", "1.0");
result.put("instructions", "Read project context and indexed references. Human grant in DEV precedes acquisition. Edit saved project files with your native tools, never SDK/external libraries. Validate with LSP/build and explicitly release. Studio revocation does not stop your external file writes. Reconnect with new configuration after Studio restart.");
result.put("instructions", McpConnectionGuide.INSTRUCTIONS);
send(exchange, 200, response(id, result)); return;
}
if (initialized == null) { empty(exchange, 400); return; }
@@ -0,0 +1,50 @@
package p.studio.app.mcp;
import com.fasterxml.jackson.databind.ObjectMapper;
import p.studio.api.automation.AgentConnectionGuide;
import p.studio.api.operation.OperationSubscription;
import p.studio.core.project.ProjectAccessRegistry;
import java.nio.file.Path;
import java.util.Optional;
/** Builds a provider-neutral connection descriptor from the live server, only on explicit export. */
public final class McpConnectionGuide implements AgentConnectionGuide {
public static final String INSTRUCTIONS = """
Configure your external coding client's MCP connection using the url and Authorization header below.
This is a provider-neutral descriptor, not a provider-specific configuration file.
Keep the token private; do not commit this descriptor, write it into project files or include it in logs.
Connect and list available tools. Read studio_project_context for the explicit projectRoot and consult
studio_reference_read for the declared languages, SDK and libraries before editing as needed.
Do not assume PBS or another language when support/documentation is missing; report the limitation.
Wait for the developer to grant control in DEV, then call studio_control_acquire. Copying or connecting
does not grant permission. Use the returned taskId for all work and operate only on this project.
Edit saved project files with your native tools; SDK and external libraries are read-only.
Validate saved changes with studio_language_query and studio_build_start. Poll studio_operation_get.
Supply a unique clientRequestId per intended build/advance. Recover lost responses by lookup using
clientRequestId; do not blindly repeat frame advancement with a new ID. Cancellation acknowledgement
is not worker termination. Use only tools/capabilities actually advertised by this running Studio.
Observe headless images/state when those tools are available, and explicitly studio_control_release
when finished. On revocation stop external edits too: Studio cannot stop your native file writes.
Existing edits are preserved. Git/review/recovery remain the developer's responsibility.
Recopy and update your client's connection after Studio restart: endpoint and token are ephemeral.
""";
private final LocalMcpServer server;
private final ProjectAccessRegistry projects;
private final ObjectMapper mapper;
public McpConnectionGuide(LocalMcpServer server, ProjectAccessRegistry projects, ObjectMapper mapper) {
this.server = server; this.projects = projects; this.mapper = mapper;
}
@Override public Optional<String> endpoint() {
try { return Optional.of(server.connection().endpoint().toString()); }
catch (IllegalStateException unavailable) { return Optional.empty(); }
}
@Override public String configuration(Path root) {
Path canonical = projects.requireOpenRoot(root);
var live = server.connection();
var value = mapper.createObjectNode().put("transport", "streamable-http").put("url", live.endpoint().toString());
value.putObject("headers").put("Authorization", live.authorizationHeader());
value.put("projectRoot", canonical.toString()).put("instructions", INSTRUCTIONS);
return value.toPrettyString();
}
@Override public OperationSubscription subscribe(Runnable changed) { return server.onConnectionChanged(changed); }
}