connect MCP language queries to project LSP
This commit is contained in:
@@ -301,12 +301,25 @@ public class StudioApplicationConfiguration {
|
||||
return new p.studio.app.compiler.CompilerProjectKnowledge(projects, catalog, mapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
p.studio.core.language.LanguageQueryPort automationLanguagePort(LspServerLifecycle lifecycle, ProjectCatalog catalog,
|
||||
p.studio.api.automation.ProjectKnowledge knowledge, ObjectMapper mapper) {
|
||||
return new p.studio.app.lsp.AutomationLspQueries(lifecycle, catalog, knowledge, mapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
p.studio.api.automation.LanguageQueries languageQueries(p.studio.core.project.ProjectAccessRegistry projects,
|
||||
p.studio.core.language.LanguageQueryPort language) {
|
||||
return new p.studio.core.language.TaskLanguageQueries(projects, language);
|
||||
}
|
||||
|
||||
@Bean
|
||||
p.studio.app.mcp.McpToolRegistry mcpToolRegistry(p.studio.app.mcp.McpToolCatalog catalog, ObjectMapper mapper,
|
||||
p.studio.api.automation.ProjectKnowledge knowledge) {
|
||||
// Further providers are connected in PLN-0139 steps 19–20. Never advertise placeholders.
|
||||
return new p.studio.app.mcp.McpToolRegistry(catalog, mapper,
|
||||
p.studio.app.mcp.McpProjectKnowledgeTools.handlers(knowledge, mapper));
|
||||
p.studio.api.automation.ProjectKnowledge knowledge, p.studio.api.automation.LanguageQueries language) {
|
||||
// Control/build/runtime/operation providers are connected in PLN-0139 step 20.
|
||||
var handlers = new java.util.LinkedHashMap<>(p.studio.app.mcp.McpProjectKnowledgeTools.handlers(knowledge, mapper));
|
||||
handlers.putAll(p.studio.app.mcp.McpLanguageTools.handlers(language, mapper));
|
||||
return new p.studio.app.mcp.McpToolRegistry(catalog, mapper, handlers);
|
||||
}
|
||||
|
||||
@Bean(initMethod = "start", destroyMethod = "close")
|
||||
|
||||
@@ -153,6 +153,7 @@ public final class CompilerProjectKnowledge implements ProjectKnowledge {
|
||||
}
|
||||
}
|
||||
limitations.add("SDK resources are provider-owned, read-only resources, not editable project files");
|
||||
limitations.add("Virtual SDK symbols may have no physical LSP definition location; consult indexed SDK references");
|
||||
// Closing a project during discovery must not publish a successful context for that closed project.
|
||||
openProjects.requireOpenRoot(root);
|
||||
var descriptors = references.values().stream().map(owned -> {
|
||||
@@ -232,6 +233,7 @@ public final class CompilerProjectKnowledge implements ProjectKnowledge {
|
||||
}
|
||||
capabilities.add(capability("lsp", true));
|
||||
capabilities.add(capability("diagnostics", true));
|
||||
capabilities.add(capability("semanticTokens", true));
|
||||
capabilities.add(capability("hover", server.hoverSupported()));
|
||||
capabilities.add(capability("completion", server.completionSupported()));
|
||||
capabilities.add(capability("signatureHelp", server.signatureHelpSupported()));
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
package p.studio.app.lsp;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.eclipse.lsp4j.*;
|
||||
import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either;
|
||||
import org.eclipse.lsp4j.launch.LSPLauncher;
|
||||
import org.eclipse.lsp4j.services.*;
|
||||
import p.studio.api.automation.*;
|
||||
import p.studio.api.project.ProjectCatalog;
|
||||
import p.studio.core.language.LanguageQueryPort;
|
||||
import p.studio.lsp.api.LspServerLifecycle;
|
||||
|
||||
import java.net.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/** A real client of the already-running project LSP. Each query owns and closes its connection. */
|
||||
public final class AutomationLspQueries implements LanguageQueryPort {
|
||||
private final LspServerLifecycle lifecycle;
|
||||
private final ProjectCatalog projects;
|
||||
private final ProjectKnowledge knowledge;
|
||||
private final ObjectMapper mapper;
|
||||
private final com.google.gson.Gson gson = new MessageJsonHandler(Map.of()).getGson();
|
||||
private final Map<TaskIdentity, Map<String, CallHierarchyItem>> items = new ConcurrentHashMap<>();
|
||||
private static final Set<String> OPERATIONS = Set.of("diagnostics", "completion", "hover", "signatureHelp", "definition",
|
||||
"references", "documentSymbols", "workspaceSymbols", "prepareRename", "rename", "codeActions", "formatting",
|
||||
"documentLinks", "foldingRanges", "selectionRanges", "prepareCallHierarchy", "incomingCalls", "outgoingCalls", "semanticTokens");
|
||||
private static final Set<String> EDITS = Set.of("completion", "prepareRename", "rename", "codeActions", "formatting");
|
||||
|
||||
public AutomationLspQueries(LspServerLifecycle lifecycle, ProjectCatalog projects, ProjectKnowledge knowledge, ObjectMapper mapper) {
|
||||
this.lifecycle = lifecycle; this.projects = projects; this.knowledge = knowledge; this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Override public void endTask(TaskIdentity task) { items.remove(task); }
|
||||
|
||||
@Override public Map<String, Object> query(TaskIdentity task, String operation, Map<String, Object> parameters) throws Exception {
|
||||
if (!OPERATIONS.contains(operation)) throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, "Unsupported language operation: " + operation);
|
||||
var context = knowledge.context(task.projectRoot());
|
||||
var primary = context.languages().stream().filter(ProjectKnowledge.Language::primary).findFirst().orElseThrow();
|
||||
if (primary.capabilities().stream().anyMatch(cap -> cap.name().equals("lsp") && cap.support() != ProjectKnowledge.Support.SUPPORTED))
|
||||
throw failure(AutomationFailure.Code.UNSUPPORTED_LANGUAGE, "Project language has no available LSP");
|
||||
var project = projects.openProject(context.projectRoot());
|
||||
var binding = LspLifecycleAdapter.context(project);
|
||||
var endpoint = lifecycle.endpoint(binding).orElseThrow(() -> failure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "Project LSP is not running; reopen the project if its language changed"));
|
||||
var params = mapper.valueToTree(parameters);
|
||||
CallHierarchyItem item = null;
|
||||
String uri = null;
|
||||
if (operation.equals("incomingCalls") || operation.equals("outgoingCalls")) {
|
||||
item = items.getOrDefault(task, Map.of()).get(params.path("itemId").asText());
|
||||
if (item == null) throw new IllegalArgumentException("Unknown task-local hierarchy itemId");
|
||||
uri = item.getUri();
|
||||
} else if (!operation.equals("workspaceSymbols")) {
|
||||
uri = params.path("uri").asText(null);
|
||||
if (uri == null) throw new IllegalArgumentException("uri is required");
|
||||
}
|
||||
Path document = uri == null ? null : sourcePath(context, uri);
|
||||
if (document != null && EDITS.contains(operation)) requireEditable(context, document.toUri().toString());
|
||||
String source = null;
|
||||
if (document != null) {
|
||||
try (var input = Files.newInputStream(document)) {
|
||||
var bytes = input.readNBytes(4 * 1024 * 1024 + 1);
|
||||
if (bytes.length > 4 * 1024 * 1024) throw failure(AutomationFailure.Code.LIMIT_EXCEEDED, "Source document exceeds 4 MiB");
|
||||
source = java.nio.charset.StandardCharsets.UTF_8.newDecoder().decode(java.nio.ByteBuffer.wrap(bytes)).toString();
|
||||
}
|
||||
uri = document.toUri().toString();
|
||||
}
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
|
||||
var workers = Executors.newCachedThreadPool(r -> { var thread = new Thread(r, "studio-automation-lsp"); thread.setDaemon(true); return thread; });
|
||||
try (var socket = new Socket()) {
|
||||
if (!InetAddress.getByName(endpoint.host()).isLoopbackAddress()) throw failure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "Project LSP endpoint is not local");
|
||||
socket.connect(new InetSocketAddress(endpoint.host(), endpoint.port()), 2000);
|
||||
var diagnostics = new CompletableFuture<List<Diagnostic>>();
|
||||
String expectedUri = uri;
|
||||
LanguageClient client = new LanguageClient() {
|
||||
public void telemetryEvent(Object event) {}
|
||||
public void publishDiagnostics(PublishDiagnosticsParams event) { if (Objects.equals(event.getUri(), expectedUri)) diagnostics.complete(List.copyOf(event.getDiagnostics())); }
|
||||
public void showMessage(MessageParams message) {}
|
||||
public CompletableFuture<MessageActionItem> showMessageRequest(ShowMessageRequestParams request) { return CompletableFuture.completedFuture(null); }
|
||||
public void logMessage(MessageParams message) {}
|
||||
@Override public CompletableFuture<ApplyWorkspaceEditResponse> applyEdit(ApplyWorkspaceEditParams edit) {
|
||||
return CompletableFuture.completedFuture(new ApplyWorkspaceEditResponse(false));
|
||||
}
|
||||
};
|
||||
var launcher = LSPLauncher.createClientLauncher(client, socket.getInputStream(), socket.getOutputStream(), workers, consumer -> consumer);
|
||||
var listening = launcher.startListening();
|
||||
try {
|
||||
var server = launcher.getRemoteProxy();
|
||||
var initialize = new InitializeParams();
|
||||
initialize.setRootUri(binding.projectRoot().toUri().toString());
|
||||
initialize.setCapabilities(new ClientCapabilities());
|
||||
var capabilities = await(server.initialize(initialize), deadline).getCapabilities();
|
||||
server.initialized(new InitializedParams());
|
||||
requireCapability(operation, capabilities);
|
||||
List<Diagnostic> currentDiagnostics = List.of();
|
||||
if (document != null) {
|
||||
// Fresh client overlay contains exactly the saved UTF-8 file. Wait for analysis before querying.
|
||||
server.getTextDocumentService().didOpen(new DidOpenTextDocumentParams(new TextDocumentItem(uri, primary.id(), 1, source)));
|
||||
currentDiagnostics = await(diagnostics, deadline);
|
||||
}
|
||||
var text = server.getTextDocumentService();
|
||||
var id = uri == null ? null : new TextDocumentIdentifier(uri);
|
||||
Object result = switch (operation) {
|
||||
case "diagnostics" -> currentDiagnostics;
|
||||
case "hover" -> await(text.hover(new HoverParams(id, position(params.path("position"), source))), deadline);
|
||||
case "completion" -> await(text.completion(new CompletionParams(id, position(params.path("position"), source))), deadline);
|
||||
case "signatureHelp" -> await(text.signatureHelp(new SignatureHelpParams(id, position(params.path("position"), source))), deadline);
|
||||
case "definition" -> await(text.definition(new DefinitionParams(id, position(params.path("position"), source))), deadline);
|
||||
case "references" -> await(text.references(new ReferenceParams(id, position(params.path("position"), source), new ReferenceContext(params.path("includeDeclaration").asBoolean(false)))), deadline);
|
||||
case "documentSymbols" -> await(text.documentSymbol(new DocumentSymbolParams(id)), deadline);
|
||||
case "workspaceSymbols" -> await(server.getWorkspaceService().symbol(new WorkspaceSymbolParams(params.path("query").asText(""))), deadline);
|
||||
case "prepareRename" -> await(text.prepareRename(new PrepareRenameParams(id, position(params.path("position"), source))), deadline);
|
||||
case "rename" -> {
|
||||
String name = params.path("newName").asText("");
|
||||
if (name.isBlank()) throw new IllegalArgumentException("newName is required");
|
||||
yield await(text.rename(new RenameParams(id, position(params.path("position"), source), name)), deadline);
|
||||
}
|
||||
case "formatting" -> await(text.formatting(new DocumentFormattingParams(id, new FormattingOptions(4, true))), deadline);
|
||||
case "codeActions" -> await(text.codeAction(new CodeActionParams(id,
|
||||
range(params.path("range"), source),
|
||||
new CodeActionContext(currentDiagnostics))), deadline);
|
||||
case "documentLinks" -> await(text.documentLink(new DocumentLinkParams(id)), deadline);
|
||||
case "foldingRanges" -> await(text.foldingRange(new FoldingRangeRequestParams(id)), deadline);
|
||||
case "selectionRanges" -> {
|
||||
if (!params.path("positions").isArray() || params.path("positions").isEmpty()) throw new IllegalArgumentException("positions are required");
|
||||
var positions = new ArrayList<Position>();
|
||||
for (var p : params.path("positions")) positions.add(position(p, source));
|
||||
yield await(text.selectionRange(new SelectionRangeParams(id, positions)), deadline);
|
||||
}
|
||||
case "prepareCallHierarchy" -> await(text.prepareCallHierarchy(new CallHierarchyPrepareParams(id, position(params.path("position"), source))), deadline);
|
||||
case "incomingCalls" -> await(text.callHierarchyIncomingCalls(new CallHierarchyIncomingCallsParams(item)), deadline);
|
||||
case "outgoingCalls" -> await(text.callHierarchyOutgoingCalls(new CallHierarchyOutgoingCallsParams(item)), deadline);
|
||||
case "semanticTokens" -> await(text.semanticTokensFull(new SemanticTokensParams(id)), deadline);
|
||||
default -> throw new IllegalArgumentException("Unsupported operation");
|
||||
};
|
||||
var wire = mapper.readTree(gson.toJson(result));
|
||||
validateEdits(context, wire);
|
||||
var limitations = new ArrayList<String>();
|
||||
if (operation.equals("codeActions") && wire.isArray()) {
|
||||
for (var action : wire) if (action.has("command")) {
|
||||
((com.fasterxml.jackson.databind.node.ObjectNode) action).remove("command");
|
||||
limitations.add("Executable LSP commands are not exposed; only edit proposals are returned");
|
||||
}
|
||||
}
|
||||
if (operation.equals("prepareCallHierarchy") || operation.equals("incomingCalls") || operation.equals("outgoingCalls")) addItemIds(task, wire);
|
||||
var output = new LinkedHashMap<String, Object>();
|
||||
output.put("operation", operation); output.put("result", mapper.convertValue(wire, Object.class));
|
||||
output.put("limitations", limitations);
|
||||
return Collections.unmodifiableMap(output);
|
||||
} finally { listening.cancel(true); }
|
||||
} finally { workers.shutdownNow(); }
|
||||
}
|
||||
|
||||
private void addItemIds(TaskIdentity task, com.fasterxml.jackson.databind.JsonNode node) {
|
||||
if (node.isObject() && node.has("uri") && node.has("selectionRange") && node.has("name") && node.has("kind")) {
|
||||
var stored = items.computeIfAbsent(task, ignored -> new ConcurrentHashMap<>());
|
||||
if (stored.size() >= 1024) throw failure(AutomationFailure.Code.LIMIT_EXCEEDED, "Too many hierarchy items for this task");
|
||||
String key = UUID.randomUUID().toString();
|
||||
stored.put(key, gson.fromJson(node.toString(), CallHierarchyItem.class));
|
||||
((com.fasterxml.jackson.databind.node.ObjectNode) node).put("itemId", key);
|
||||
} else if (node.isContainerNode()) node.forEach(child -> addItemIds(task, child));
|
||||
}
|
||||
|
||||
private static void requireCapability(String operation, ServerCapabilities caps) {
|
||||
Object capability = switch (operation) {
|
||||
case "diagnostics" -> caps.getTextDocumentSync();
|
||||
case "hover" -> caps.getHoverProvider(); case "completion" -> caps.getCompletionProvider();
|
||||
case "signatureHelp" -> caps.getSignatureHelpProvider(); case "definition" -> caps.getDefinitionProvider();
|
||||
case "references" -> caps.getReferencesProvider(); case "documentSymbols" -> caps.getDocumentSymbolProvider();
|
||||
case "workspaceSymbols" -> caps.getWorkspaceSymbolProvider(); case "rename", "prepareRename" -> caps.getRenameProvider();
|
||||
case "codeActions" -> caps.getCodeActionProvider(); case "formatting" -> caps.getDocumentFormattingProvider();
|
||||
case "documentLinks" -> caps.getDocumentLinkProvider(); case "foldingRanges" -> caps.getFoldingRangeProvider();
|
||||
case "selectionRanges" -> caps.getSelectionRangeProvider(); case "prepareCallHierarchy", "incomingCalls", "outgoingCalls" -> caps.getCallHierarchyProvider();
|
||||
case "semanticTokens" -> caps.getSemanticTokensProvider(); default -> null;
|
||||
};
|
||||
if (capability instanceof Either<?, ?> either) capability = either.isLeft() ? either.getLeft() : either.getRight();
|
||||
if (operation.equals("prepareRename") && (!(capability instanceof RenameOptions options) || !Boolean.TRUE.equals(options.getPrepareProvider())))
|
||||
throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, "LSP does not advertise prepareRename");
|
||||
if (capability == null || Boolean.FALSE.equals(capability)) throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, "LSP does not advertise " + operation);
|
||||
}
|
||||
|
||||
private static Position position(com.fasterxml.jackson.databind.JsonNode node, String text) {
|
||||
if (!node.path("line").isIntegralNumber() || !node.path("character").isIntegralNumber()
|
||||
|| !node.path("line").canConvertToInt() || !node.path("character").canConvertToInt()) throw new IllegalArgumentException("UTF-16 position is required");
|
||||
int line = node.path("line").intValue(), character = node.path("character").intValue();
|
||||
String[] lines = text.split("\\r\\n|\\n|\\r", -1);
|
||||
if (line < 0 || line >= lines.length || character < 0 || character > lines[line].length()
|
||||
|| character > 0 && character < lines[line].length() && Character.isHighSurrogate(lines[line].charAt(character - 1)) && Character.isLowSurrogate(lines[line].charAt(character)))
|
||||
throw new IllegalArgumentException("Position is outside the saved document or splits a UTF-16 pair");
|
||||
return new Position(line, character);
|
||||
}
|
||||
|
||||
private static Range range(com.fasterxml.jackson.databind.JsonNode node, String text) {
|
||||
var start = position(node.path("start"), text); var end = position(node.path("end"), text);
|
||||
if (start.getLine() > end.getLine() || start.getLine() == end.getLine() && start.getCharacter() > end.getCharacter())
|
||||
throw new IllegalArgumentException("Range end precedes its start");
|
||||
return new Range(start, end);
|
||||
}
|
||||
|
||||
private static Path sourcePath(ProjectKnowledge.Context context, String uri) throws Exception {
|
||||
final Path path;
|
||||
try { path = Path.of(URI.create(uri)).toRealPath(); }
|
||||
catch (RuntimeException | java.io.IOException invalid) { throw failure(AutomationFailure.Code.OUTSIDE_PROJECT_SCOPE, "Source URI is not an accessible local project/dependency file"); }
|
||||
boolean scope = path.startsWith(context.projectRoot()) || context.readOnlyRoots().stream().anyMatch(path::startsWith)
|
||||
|| context.libraries().stream().flatMap(lib -> lib.roots().stream()).anyMatch(path::startsWith);
|
||||
boolean source = context.languages().stream().flatMap(lang -> lang.extensions().stream()).anyMatch(extension -> path.getFileName().toString().endsWith("." + extension));
|
||||
if (!scope || !source || !Files.isRegularFile(path)) throw failure(AutomationFailure.Code.OUTSIDE_PROJECT_SCOPE, "URI is outside the known source scope");
|
||||
return path;
|
||||
}
|
||||
|
||||
private static void requireEditable(ProjectKnowledge.Context context, String uri) throws Exception {
|
||||
Path path = sourcePath(context, uri);
|
||||
if (!path.startsWith(context.projectRoot()) || context.readOnlyRoots().stream().anyMatch(path::startsWith))
|
||||
throw failure(AutomationFailure.Code.READ_ONLY_SCOPE, "LSP edit proposal targets a read-only SDK/dependency");
|
||||
}
|
||||
|
||||
private static void validateEdits(ProjectKnowledge.Context context, com.fasterxml.jackson.databind.JsonNode node) throws Exception {
|
||||
if (node.isObject()) {
|
||||
if (node.has("changes")) {
|
||||
var fields = node.path("changes").fieldNames();
|
||||
while (fields.hasNext()) requireEditable(context, fields.next());
|
||||
}
|
||||
if (node.has("textDocument") && node.has("edits")) requireEditable(context, node.path("textDocument").path("uri").asText());
|
||||
if (node.has("kind") && Set.of("create", "rename", "delete").contains(node.path("kind").asText()))
|
||||
throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, "LSP file operations are not exposed");
|
||||
}
|
||||
if (node.isContainerNode()) for (var child : node) validateEdits(context, child);
|
||||
}
|
||||
|
||||
private static <T> T await(CompletableFuture<T> future, long deadline) throws Exception {
|
||||
try { return future.get(Math.max(1, deadline - System.nanoTime()), TimeUnit.NANOSECONDS); }
|
||||
catch (ExecutionException failure) {
|
||||
if (failure.getCause() instanceof org.eclipse.lsp4j.jsonrpc.ResponseErrorException response) {
|
||||
int code = response.getResponseError().getCode();
|
||||
if (code == -32602) throw new IllegalArgumentException("LSP rejected query parameters");
|
||||
if (code == -32601 || code == -32600)
|
||||
throw failure(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, response.getResponseError().getMessage());
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
catch (Exception failure) { future.cancel(true); throw failure; }
|
||||
}
|
||||
private static AutomationException failure(AutomationFailure.Code code, String message) { return new AutomationException(code, message); }
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import java.util.Objects;
|
||||
|
||||
public final class LspLifecycleAdapter implements LspLifecyclePort {
|
||||
private final LspServerLifecycle lifecycle;
|
||||
private final java.util.Map<java.nio.file.Path, LspProjectContext> bindings = new java.util.concurrent.ConcurrentHashMap<>();
|
||||
|
||||
public LspLifecycleAdapter(LspServerLifecycle lifecycle) {
|
||||
this.lifecycle = Objects.requireNonNull(lifecycle, "lifecycle");
|
||||
@@ -17,19 +18,26 @@ public final class LspLifecycleAdapter implements LspLifecyclePort {
|
||||
|
||||
@Override
|
||||
public void boot(ProjectReference project) {
|
||||
lifecycle.bootServer(LspServerConfiguration.defaults(), context(project));
|
||||
var binding = context(project);
|
||||
lifecycle.bootServer(LspServerConfiguration.builder().endpoint(
|
||||
new p.studio.lsp.messages.LspServerEndpoint("127.0.0.1", 0)).build(), binding);
|
||||
bindings.put(project.rootPath().toAbsolutePath().normalize(), binding);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown(ProjectReference project) {
|
||||
lifecycle.shutdownServer(context(project));
|
||||
var binding = bindings.remove(project.rootPath().toAbsolutePath().normalize());
|
||||
if (binding != null) lifecycle.shutdownServer(binding);
|
||||
}
|
||||
|
||||
private static LspProjectContext context(ProjectReference project) {
|
||||
static LspProjectContext context(ProjectReference project) {
|
||||
final var safeProject = Objects.requireNonNull(project, "project");
|
||||
final java.nio.file.Path root;
|
||||
try { root = safeProject.rootPath().toRealPath(); }
|
||||
catch (java.io.IOException failure) { throw new IllegalStateException("Project root unavailable", failure); }
|
||||
return new LspProjectContext(
|
||||
safeProject.rootPath().toAbsolutePath().normalize().toString(),
|
||||
root.toString(),
|
||||
safeProject.languageId(),
|
||||
safeProject.rootPath());
|
||||
root);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package p.studio.app.mcp;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import p.studio.api.automation.LanguageQueries;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public final class McpLanguageTools {
|
||||
private McpLanguageTools() {}
|
||||
public static Map<String, Function<JsonNode, McpToolResult>> handlers(LanguageQueries language, ObjectMapper mapper) {
|
||||
return Map.of("studio_language_query", arguments -> McpToolResult.success(mapper, mapper.valueToTree(
|
||||
language.query(Path.of(arguments.path("projectRoot").asText()), arguments.path("taskId").asText(),
|
||||
arguments.path("operation").asText(), mapper.convertValue(arguments.path("parameters"),
|
||||
new com.fasterxml.jackson.core.type.TypeReference<Map<String, Object>>() {})))));
|
||||
}
|
||||
}
|
||||
@@ -573,7 +573,7 @@
|
||||
},
|
||||
"exampleValue": {
|
||||
"operation": "definition",
|
||||
"locations": [],
|
||||
"result": [],
|
||||
"limitations": []
|
||||
}
|
||||
},
|
||||
|
||||
@@ -30,9 +30,10 @@ class StudioApplicationConfigurationTest {
|
||||
assertFalse(executor.isShutdown());
|
||||
assertTrue(mcp.isRunning());
|
||||
var tools = context.getBean(p.studio.app.mcp.McpToolRegistry.class).descriptors();
|
||||
org.junit.jupiter.api.Assertions.assertEquals(2, tools.size());
|
||||
org.junit.jupiter.api.Assertions.assertEquals(3, tools.size());
|
||||
org.junit.jupiter.api.Assertions.assertEquals("studio_project_context", tools.get(0).path("name").asText());
|
||||
org.junit.jupiter.api.Assertions.assertEquals("studio_reference_read", tools.get(1).path("name").asText());
|
||||
org.junit.jupiter.api.Assertions.assertEquals("studio_language_query", tools.get(2).path("name").asText());
|
||||
}
|
||||
assertTrue(executor.isShutdown());
|
||||
assertFalse(mcp.isRunning());
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
package p.studio.app.lsp;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.api.automation.*;
|
||||
import p.studio.api.session.ProjectSession;
|
||||
import p.studio.app.compiler.CompilerProjectKnowledge;
|
||||
import p.studio.app.operation.ExecutorOperationExecutor;
|
||||
import p.studio.core.language.TaskLanguageQueries;
|
||||
import p.studio.core.project.*;
|
||||
import p.studio.core.session.DefaultProjectSessionFactory;
|
||||
import p.studio.lsp.services.LspServerLifecycleImpl;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AutomationLspQueriesTest {
|
||||
@TempDir Path temporary;
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final ExecutorService workers = Executors.newCachedThreadPool();
|
||||
final LspServerLifecycleImpl lifecycle = new LspServerLifecycleImpl();
|
||||
ProjectSession session;
|
||||
TaskLanguageQueries queries;
|
||||
final ProjectAccessRegistry access = new ProjectAccessRegistry();
|
||||
Path root, source;
|
||||
String task;
|
||||
static final String CODE = "fn helper() -> int { return 1; }\nfn main() -> int { return helper(); }\n";
|
||||
|
||||
@BeforeEach void open() throws Exception {
|
||||
root = Files.createDirectory(temporary.resolve("app")).toRealPath();
|
||||
source = Files.createDirectories(root.resolve("src")).resolve("main.pbs");
|
||||
manifest(root, "app", List.of());
|
||||
Files.writeString(source, CODE);
|
||||
Files.writeString(source.resolveSibling("mod.barrel"), "pub fn helper() -> int;\npub fn main() -> int;\n");
|
||||
var setup = new ProjectLocalStudioSetupStore(mapper);
|
||||
var catalog = new ProjectCatalogService(temporary, mapper, setup);
|
||||
session = new DefaultProjectSessionFactory(new ProjectLocalStudioStateStore(mapper), setup,
|
||||
new LspLifecycleAdapter(lifecycle), access, new ExecutorOperationExecutor(workers)).open(catalog.openProject(root));
|
||||
var knowledge = new CompilerProjectKnowledge(access, catalog, mapper);
|
||||
queries = new TaskLanguageQueries(access, new AutomationLspQueries(lifecycle, catalog, knowledge, mapper));
|
||||
}
|
||||
@AfterEach void close() { if (session != null) session.close(); workers.shutdownNow(); }
|
||||
void acquire() {
|
||||
assertTrue(session.automation().grant(false).toCompletableFuture().join().success());
|
||||
task = session.automation().acquire().task().value();
|
||||
}
|
||||
JsonNode query(String operation, Map<String, Object> parameters) {
|
||||
return mapper.valueToTree(queries.query(root, task, operation, parameters));
|
||||
}
|
||||
Map<String, Object> at(int line, int character) {
|
||||
return Map.of("uri", source.toUri().toString(), "position", Map.of("line", line, "character", character));
|
||||
}
|
||||
@Test void diagnosticsAndNavigationSeeSavedChangesOverTheActualServer() throws Exception {
|
||||
acquire();
|
||||
assertTrue(query("diagnostics", at(0, 0)).path("result").isArray());
|
||||
var definition = query("definition", at(1, 26)).path("result");
|
||||
assertFalse(definition.isEmpty(), definition.toString());
|
||||
assertEquals(source.toUri().toString(), definition.get(0).path("uri").asText());
|
||||
Files.writeString(source, "fn main( {");
|
||||
assertFalse(query("diagnostics", at(0, 0)).path("result").isEmpty());
|
||||
Files.writeString(source, CODE.replace("helper", "renamed"));
|
||||
Files.writeString(source.resolveSibling("mod.barrel"), "pub fn renamed() -> int;\npub fn main() -> int;\n");
|
||||
var symbols = query("documentSymbols", at(0, 0)).path("result");
|
||||
assertTrue(symbols.toString().contains("renamed"), symbols.toString());
|
||||
assertFalse(symbols.toString().contains("helper"), symbols.toString());
|
||||
}
|
||||
@Test void proposalsDoNotWriteFilesAndQueriesCoverAdvertisedOperations() throws Exception {
|
||||
acquire();
|
||||
for (String operation : List.of("hover", "completion", "signatureHelp", "references", "prepareRename", "semanticTokens"))
|
||||
assertEquals(operation, query(operation, at(1, 26)).path("operation").asText());
|
||||
for (String operation : List.of("documentLinks", "foldingRanges", "formatting"))
|
||||
assertEquals(operation, query(operation, at(0, 0)).path("operation").asText());
|
||||
assertEquals("workspaceSymbols", query("workspaceSymbols", Map.of("query", "helper")).path("operation").asText());
|
||||
var rename = new HashMap<>(at(1, 26)); rename.put("newName", "renamed");
|
||||
assertTrue(query("rename", rename).path("result").has("changes"));
|
||||
assertEquals(CODE, Files.readString(source));
|
||||
var selections = Map.<String, Object>of("uri", source.toUri().toString(), "positions", List.of(Map.of("line", 0, "character", 4)));
|
||||
assertEquals("selectionRanges", query("selectionRanges", selections).path("operation").asText());
|
||||
var actions = Map.<String, Object>of("uri", source.toUri().toString(), "range", Map.of("start", Map.of("line", 0, "character", 0), "end", Map.of("line", 0, "character", 10)));
|
||||
assertEquals("codeActions", query("codeActions", actions).path("operation").asText());
|
||||
}
|
||||
@Test void hierarchyIdsBelongToTheTaskAndDisappearOnReturn() {
|
||||
acquire();
|
||||
var prepared = query("prepareCallHierarchy", at(0, 4)).path("result");
|
||||
assertFalse(prepared.isEmpty(), prepared.toString());
|
||||
String id = prepared.get(0).path("itemId").asText();
|
||||
assertFalse(id.isBlank());
|
||||
assertFalse(query("incomingCalls", Map.of("itemId", id)).path("result").isEmpty());
|
||||
assertEquals("outgoingCalls", query("outgoingCalls", Map.of("itemId", id)).path("operation").asText());
|
||||
session.automation().reclaim().toCompletableFuture().join();
|
||||
acquire();
|
||||
assertThrows(IllegalArgumentException.class, () -> query("incomingCalls", Map.of("itemId", id)));
|
||||
}
|
||||
@Test void admissionUnsupportedOperationsAndSourceScopeAreExplicit() throws Exception {
|
||||
assertThrows(ControlDeniedException.class, () -> queries.query(root, "ungranted", "diagnostics", at(0, 0)));
|
||||
acquire();
|
||||
assertThrows(ControlDeniedException.class, () -> queries.query(root, "wrong-task", "diagnostics", at(0, 0)));
|
||||
assertEquals(AutomationFailure.Code.UNSUPPORTED_CAPABILITY, assertThrows(AutomationException.class,
|
||||
() -> query("typeHierarchy", at(0, 0))).failure().code());
|
||||
Path outside = temporary.resolve("outside.pbs"); Files.writeString(outside, CODE);
|
||||
assertEquals(AutomationFailure.Code.OUTSIDE_PROJECT_SCOPE, assertThrows(AutomationException.class,
|
||||
() -> query("diagnostics", Map.of("uri", outside.toUri().toString()))).failure().code());
|
||||
assertThrows(IllegalArgumentException.class, () -> query("definition", at(999, 0)));
|
||||
var context = LspLifecycleAdapter.context(session.projectReference());
|
||||
var endpoint = lifecycle.endpoint(context).orElseThrow();
|
||||
session.close(); session = null;
|
||||
assertTrue(lifecycle.endpoint(context).isEmpty());
|
||||
assertThrows(java.io.IOException.class, () -> new java.net.Socket(endpoint.host(), endpoint.port()));
|
||||
assertEquals(AutomationFailure.Code.PROJECT_NOT_OPEN, assertThrows(AutomationException.class,
|
||||
() -> query("diagnostics", at(0, 0))).failure().code());
|
||||
}
|
||||
|
||||
@Test void mcpDispatchPreservesTaskFailuresAndReturnsNeutralLspResults() {
|
||||
var tools = new p.studio.app.mcp.McpToolRegistry(new p.studio.app.mcp.McpToolCatalog(mapper), mapper,
|
||||
p.studio.app.mcp.McpLanguageTools.handlers(queries, mapper));
|
||||
var arguments = mapper.createObjectNode().put("projectRoot", root.toString()).put("taskId", "missing").put("operation", "diagnostics");
|
||||
arguments.putObject("parameters").put("uri", source.toUri().toString());
|
||||
var denied = tools.call("studio_language_query", arguments).wire();
|
||||
assertEquals("INVALID_TASK", denied.path("structuredContent").path("error").path("code").asText());
|
||||
acquire(); arguments.put("taskId", task);
|
||||
var success = tools.call("studio_language_query", arguments).wire();
|
||||
assertFalse(success.path("isError").asBoolean(), success.toString());
|
||||
assertTrue(success.path("structuredContent").path("value").path("result").isArray());
|
||||
session.automation().reclaim().toCompletableFuture().join();
|
||||
assertEquals("INVALID_TASK", tools.call("studio_language_query", arguments).wire().path("structuredContent").path("error").path("code").asText());
|
||||
}
|
||||
|
||||
@Test void authenticatedHttpToolUsesTheRealLspButDoesNotGrantTaskAuthority() throws Exception {
|
||||
var tools = new p.studio.app.mcp.McpToolRegistry(new p.studio.app.mcp.McpToolCatalog(mapper), mapper,
|
||||
p.studio.app.mcp.McpLanguageTools.handlers(queries, mapper));
|
||||
try (var server = new p.studio.app.mcp.LocalMcpServer(mapper, tools);
|
||||
var client = java.net.http.HttpClient.newHttpClient()) {
|
||||
server.start();
|
||||
var initialize = mapper.createObjectNode().put("jsonrpc", "2.0").put("id", 1).put("method", "initialize");
|
||||
var setup = initialize.putObject("params").put("protocolVersion", "2025-06-18");
|
||||
setup.putObject("capabilities"); setup.putObject("clientInfo").put("name", "language-test").put("version", "1");
|
||||
String connection = post(client, server, null, initialize).headers().firstValue("Mcp-Session-Id").orElseThrow();
|
||||
post(client, server, connection, mapper.createObjectNode().put("jsonrpc", "2.0").put("method", "notifications/initialized"));
|
||||
var request = mapper.createObjectNode().put("jsonrpc", "2.0").put("id", 2).put("method", "tools/call");
|
||||
var args = request.putObject("params").put("name", "studio_language_query").putObject("arguments")
|
||||
.put("projectRoot", root.toString()).put("taskId", "ungranted").put("operation", "documentSymbols");
|
||||
args.putObject("parameters").put("uri", source.toUri().toString());
|
||||
var denied = mapper.readTree(post(client, server, connection, request).body()).path("result");
|
||||
assertEquals("INVALID_TASK", denied.path("structuredContent").path("error").path("code").asText());
|
||||
acquire(); args.put("taskId", task);
|
||||
var result = mapper.readTree(post(client, server, connection, request).body()).path("result");
|
||||
assertFalse(result.path("isError").asBoolean(), result.toString());
|
||||
assertTrue(result.path("structuredContent").path("value").path("result").toString().contains("helper"));
|
||||
}
|
||||
}
|
||||
|
||||
private java.net.http.HttpResponse<String> post(java.net.http.HttpClient client, p.studio.app.mcp.LocalMcpServer server,
|
||||
String connection, JsonNode body) throws Exception {
|
||||
var request = java.net.http.HttpRequest.newBuilder(server.connection().endpoint()).timeout(java.time.Duration.ofSeconds(15))
|
||||
.header("Authorization", server.connection().authorizationHeader()).header("Content-Type", "application/json")
|
||||
.header("Accept", "application/json, text/event-stream").header("MCP-Protocol-Version", "2025-06-18");
|
||||
if (connection != null) request.header("Mcp-Session-Id", connection);
|
||||
return client.send(request.POST(java.net.http.HttpRequest.BodyPublishers.ofString(body.toString())).build(), java.net.http.HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
|
||||
@Test void reclaimCancelsTheQueryAndEndsTaskResourcesAfterTheWorkerStops() throws Exception {
|
||||
var entered = new CountDownLatch(1);
|
||||
var stopped = new CountDownLatch(1);
|
||||
var cleaned = new java.util.concurrent.atomic.AtomicBoolean();
|
||||
var slow = new TaskLanguageQueries(access, new p.studio.core.language.LanguageQueryPort() {
|
||||
@Override public Map<String, Object> query(TaskIdentity task, String operation, Map<String, Object> params) throws Exception {
|
||||
entered.countDown();
|
||||
try { new CountDownLatch(1).await(); return Map.of(); }
|
||||
finally { stopped.countDown(); }
|
||||
}
|
||||
@Override public void endTask(TaskIdentity task) { assertEquals(0, stopped.getCount()); cleaned.set(true); }
|
||||
});
|
||||
acquire();
|
||||
var pending = workers.submit(() -> slow.query(root, task, "diagnostics", at(0, 0)));
|
||||
assertTrue(entered.await(2, TimeUnit.SECONDS));
|
||||
assertEquals(ControlResult.Status.BUSY, assertThrows(ControlDeniedException.class,
|
||||
() -> queries.query(root, task, "diagnostics", at(0, 0))).status());
|
||||
assertTrue(session.automation().reclaim().toCompletableFuture().get(2, TimeUnit.SECONDS).success());
|
||||
assertThrows(ExecutionException.class, () -> pending.get(2, TimeUnit.SECONDS));
|
||||
assertTrue(cleaned.get());
|
||||
assertEquals(ControlState.MANUAL, session.automation().snapshot().state());
|
||||
}
|
||||
@Test void externalDependencyNavigationIsReadableButRefactoringIsReadOnly() throws Exception {
|
||||
Path lib = Files.createDirectories(temporary.resolve("lib")).toRealPath();
|
||||
manifest(lib, "lib", List.of());
|
||||
Path module = Files.createDirectories(lib.resolve("src/shared"));
|
||||
Path declaration = module.resolve("main.pbs");
|
||||
Files.writeString(declaration, "fn answer() -> int { return 42; }\n");
|
||||
Files.writeString(module.resolve("mod.barrel"), "pub fn answer() -> int;\n");
|
||||
manifest(root, "app", List.of(Map.of("path", "../lib")));
|
||||
Files.createDirectories(root.resolve(".workspace/repos")); Files.writeString(root.resolve(".workspace/repos/registry.json"), "{}");
|
||||
Files.writeString(source, "import { answer } from @lib:shared;\nfn main() -> int { return answer(); }\n");
|
||||
Files.writeString(source.resolveSibling("mod.barrel"), "pub fn main() -> int;\n");
|
||||
acquire();
|
||||
var locations = query("definition", at(1, 26)).path("result");
|
||||
assertFalse(locations.isEmpty(), locations.toString());
|
||||
assertEquals(declaration.toUri().toString(), locations.get(0).path("uri").asText());
|
||||
assertEquals("documentSymbols", query("documentSymbols", Map.of("uri", declaration.toUri().toString())).path("operation").asText());
|
||||
assertEquals(AutomationFailure.Code.READ_ONLY_SCOPE, assertThrows(AutomationException.class,
|
||||
() -> query("formatting", Map.of("uri", declaration.toUri().toString()))).failure().code());
|
||||
var rename = new HashMap<>(at(1, 26)); rename.put("newName", "renamed");
|
||||
assertEquals(AutomationFailure.Code.READ_ONLY_SCOPE, assertThrows(AutomationException.class, () -> query("rename", rename)).failure().code());
|
||||
}
|
||||
private void manifest(Path path, String name, List<?> dependencies) throws Exception {
|
||||
mapper.writeValue(path.resolve("prometeu.json").toFile(), Map.of("name", name, "version", "1.0.0", "language", "pbs", "stdlib", "1", "target", "Game", "dependencies", dependencies));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user