add MCP tool contracts and authenticated local HTTP transport
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package p.studio.app.mcp;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import p.studio.api.automation.AutomationFailure;
|
||||
import p.studio.api.automation.ControlDeniedException;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Immutable adapter dispatch. Services, not this transport, own authority and operations. */
|
||||
public final class McpToolRegistry {
|
||||
private final McpToolCatalog catalog;
|
||||
private final ObjectMapper mapper;
|
||||
private final Map<String, Function<JsonNode, McpToolResult>> handlers;
|
||||
public McpToolRegistry(McpToolCatalog catalog, ObjectMapper mapper, Map<String, Function<JsonNode, McpToolResult>> handlers) {
|
||||
this.catalog = catalog; this.mapper = mapper; this.handlers = Map.copyOf(handlers);
|
||||
handlers.keySet().forEach(catalog::definition);
|
||||
}
|
||||
public ArrayNode descriptors() {
|
||||
var descriptors = mapper.createArrayNode();
|
||||
catalog.names().stream().filter(handlers::containsKey).forEach(name -> descriptors.add(catalog.descriptor(name)));
|
||||
return descriptors;
|
||||
}
|
||||
public McpToolResult call(String name, JsonNode arguments) {
|
||||
var handler = handlers.get(name);
|
||||
if (handler == null) throw new IllegalArgumentException("Unknown or unavailable tool");
|
||||
catalog.validateArguments(name, arguments);
|
||||
try { return handler.apply(arguments); }
|
||||
catch (ControlDeniedException denied) {
|
||||
var code = switch (denied.status()) {
|
||||
case INVALID_TASK -> AutomationFailure.Code.INVALID_TASK;
|
||||
case NOT_GRANTED -> AutomationFailure.Code.NOT_GRANTED;
|
||||
case CLOSED -> AutomationFailure.Code.CLOSED;
|
||||
default -> AutomationFailure.Code.BUSY;
|
||||
};
|
||||
return McpToolResult.failure(mapper, new AutomationFailure(code, "Project authority rejected the call"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user