diff --git a/src/client/java/cuspymd/mcp/mod/MCPServerModClient.java b/src/client/java/cuspymd/mcp/mod/MCPServerModClient.java index 6368c73..5486da4 100644 --- a/src/client/java/cuspymd/mcp/mod/MCPServerModClient.java +++ b/src/client/java/cuspymd/mcp/mod/MCPServerModClient.java @@ -1,7 +1,6 @@ package cuspymd.mcp.mod; import net.fabricmc.api.ClientModInitializer; -import cuspymd.mcp.mod.bridge.IPCServer; import cuspymd.mcp.mod.bridge.HTTPMCPServer; import cuspymd.mcp.mod.config.MCPConfig; import cuspymd.mcp.mod.utils.ScreenshotUtils; @@ -11,7 +10,6 @@ public class MCPServerModClient implements ClientModInitializer { public static final Logger LOGGER = LoggerFactory.getLogger("mcp-server-mod"); - private IPCServer ipcServer; private HTTPMCPServer httpServer; @Override @@ -37,10 +35,7 @@ public void onInitializeClient() { httpServer.start(); LOGGER.info("HTTP MCP Server started on port {}", httpServer.getPort()); } else { - // Default to stdio transport - ipcServer = new IPCServer(config, new cuspymd.mcp.mod.command.CommandExecutor(config)); - ipcServer.start(); - LOGGER.info("IPC Server started on port {}", ipcServer.getPort()); + LOGGER.warn("Unsupported transport: {}. Only 'http' is supported.", transport); } } } catch (Exception e) { @@ -49,10 +44,6 @@ public void onInitializeClient() { } public void onClientShutdown() { - if (ipcServer != null) { - ipcServer.stop(); - LOGGER.info("IPC Server stopped"); - } if (httpServer != null) { httpServer.stop(); LOGGER.info("HTTP MCP Server stopped"); diff --git a/src/main/java/cuspymd/mcp/mod/MCPServerModServer.java b/src/main/java/cuspymd/mcp/mod/MCPServerModServer.java index 7d9d897..00be4f7 100644 --- a/src/main/java/cuspymd/mcp/mod/MCPServerModServer.java +++ b/src/main/java/cuspymd/mcp/mod/MCPServerModServer.java @@ -3,7 +3,6 @@ import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import cuspymd.mcp.mod.bridge.HTTPMCPServer; -import cuspymd.mcp.mod.bridge.IPCServer; import cuspymd.mcp.mod.config.MCPConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,7 +10,6 @@ public class MCPServerModServer implements DedicatedServerModInitializer { public static final Logger LOGGER = LoggerFactory.getLogger("mcp-server-mod"); private HTTPMCPServer httpServer; - private IPCServer ipcServer; @Override public void onInitializeServer() { @@ -34,10 +32,7 @@ public void onInitializeServer() { httpServer.start(); LOGGER.info("HTTP MCP Server started on port {}", httpServer.getPort()); } else { - // Default to stdio transport - ipcServer = new IPCServer(config, new cuspymd.mcp.mod.server.tools.ServerCommandExecutor(config, server)); - ipcServer.start(); - LOGGER.info("IPC Server started on port {}", ipcServer.getPort()); + LOGGER.warn("Unsupported transport: {}. Only 'http' is supported.", transport); } } } catch (Exception e) { @@ -46,10 +41,6 @@ public void onInitializeServer() { }); ServerLifecycleEvents.SERVER_STOPPING.register(server -> { - if (ipcServer != null) { - ipcServer.stop(); - LOGGER.info("IPC Server stopped"); - } if (httpServer != null) { httpServer.stop(); LOGGER.info("HTTP MCP Server stopped"); diff --git a/src/main/java/cuspymd/mcp/mod/bridge/IPCClient.java b/src/main/java/cuspymd/mcp/mod/bridge/IPCClient.java deleted file mode 100644 index 37e92e2..0000000 --- a/src/main/java/cuspymd/mcp/mod/bridge/IPCClient.java +++ /dev/null @@ -1,97 +0,0 @@ -package cuspymd.mcp.mod.bridge; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - -import java.io.*; -import java.net.Socket; -import java.util.concurrent.CompletableFuture; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class IPCClient { - private static final Logger LOGGER = LoggerFactory.getLogger(IPCClient.class); - private static final Gson GSON = new Gson(); - private static final int IPC_PORT = 25565; - private static final String IPC_HOST = "localhost"; - - private Socket socket; - private BufferedReader reader; - private PrintWriter writer; - - public boolean connect() { - try { - socket = new Socket(IPC_HOST, IPC_PORT); - reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); - writer = new PrintWriter(socket.getOutputStream(), true); - return true; - } catch (IOException e) { - return false; - } - } - - public JsonObject executeCommands(JsonObject arguments) { - if (socket == null || socket.isClosed()) { - return createErrorResponse("Not connected to IPC server"); - } - - try { - JsonObject request = new JsonObject(); - request.addProperty("type", "execute_commands"); - request.add("arguments", arguments); - - writer.println(GSON.toJson(request)); - - String response = reader.readLine(); - if (response != null) { - return JsonParser.parseString(response).getAsJsonObject(); - } else { - return createErrorResponse("No response from IPC server"); - } - } catch (IOException e) { - return createErrorResponse("IPC communication error: " + e.getMessage()); - } - } - - public CompletableFuture connectAsync() { - return CompletableFuture.supplyAsync(() -> { - int attempts = 0; - while (attempts < 10) { - if (connect()) { - return true; - } - attempts++; - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - } - return false; - }); - } - - private JsonObject createErrorResponse(String message) { - JsonObject response = new JsonObject(); - response.addProperty("isError", true); - response.addProperty("error", message); - return response; - } - - public void disconnect() { - try { - if (reader != null) reader.close(); - if (writer != null) writer.close(); - if (socket != null) socket.close(); - } catch (IOException e) { - LOGGER.error("Failed to disconnect IPC client", e); - } - } - - public boolean isConnected() { - return socket != null && !socket.isClosed(); - } -} \ No newline at end of file diff --git a/src/main/java/cuspymd/mcp/mod/bridge/IPCServer.java b/src/main/java/cuspymd/mcp/mod/bridge/IPCServer.java deleted file mode 100644 index f528153..0000000 --- a/src/main/java/cuspymd/mcp/mod/bridge/IPCServer.java +++ /dev/null @@ -1,132 +0,0 @@ -package cuspymd.mcp.mod.bridge; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import cuspymd.mcp.mod.command.ICommandExecutor; -import cuspymd.mcp.mod.config.MCPConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.*; -import java.net.ServerSocket; -import java.net.Socket; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.atomic.AtomicBoolean; - -public class IPCServer { - private static final Logger LOGGER = LoggerFactory.getLogger(IPCServer.class); - private static final Gson GSON = new Gson(); - private static final int IPC_PORT = 25565; // Default port for IPC - - private final ICommandExecutor commandExecutor; - private final AtomicBoolean running = new AtomicBoolean(false); - private ServerSocket serverSocket; - private ExecutorService executor; - - public IPCServer(MCPConfig config, ICommandExecutor commandExecutor) { - this.commandExecutor = commandExecutor; - } - - public void start() throws IOException { - if (running.get()) { - return; - } - - running.set(true); - serverSocket = new ServerSocket(IPC_PORT); - executor = Executors.newCachedThreadPool(); - - Thread acceptThread = new Thread(this::acceptConnections); - acceptThread.setDaemon(true); - acceptThread.setName("IPC-Accept-Thread"); - acceptThread.start(); - - LOGGER.info("IPC Server started on port {}", IPC_PORT); - } - - private void acceptConnections() { - while (running.get() && !serverSocket.isClosed()) { - try { - Socket clientSocket = serverSocket.accept(); - executor.submit(() -> handleClient(clientSocket)); - } catch (IOException e) { - if (running.get()) { - LOGGER.error("Error accepting IPC connection", e); - } - } - } - } - - private void handleClient(Socket clientSocket) { - try (BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true)) { - - LOGGER.info("IPC client connected from {}", clientSocket.getRemoteSocketAddress()); - - String line; - while ((line = reader.readLine()) != null) { - try { - LOGGER.info("Received IPC request: {}", line); - JsonObject request = JsonParser.parseString(line).getAsJsonObject(); - JsonObject response = handleIPCRequest(request); - LOGGER.info("Sending IPC response: {}", response); - - writer.println(GSON.toJson(response)); - } catch (Exception e) { - LOGGER.error("Error processing IPC request: " + line, e); - JsonObject errorResponse = createErrorResponse("Error processing request: " + e.getMessage()); - writer.println(GSON.toJson(errorResponse)); - } - } - } catch (IOException e) { - LOGGER.error("Error handling IPC client", e); - } finally { - try { - clientSocket.close(); - } catch (IOException e) { - LOGGER.error("Error closing IPC client socket", e); - } - } - } - - private JsonObject handleIPCRequest(JsonObject request) { - String type = request.get("type").getAsString(); - - if ("execute_commands".equals(type)) { - JsonObject arguments = request.getAsJsonObject("arguments"); - return commandExecutor.executeCommands(arguments); - } else { - return createErrorResponse("Unknown IPC request type: " + type); - } - } - - private JsonObject createErrorResponse(String message) { - JsonObject response = new JsonObject(); - response.addProperty("isError", true); - response.addProperty("error", message); - return response; - } - - public void stop() { - if (running.get()) { - running.set(false); - try { - if (serverSocket != null && !serverSocket.isClosed()) { - serverSocket.close(); - } - if (executor != null) { - executor.shutdown(); - } - LOGGER.info("IPC Server stopped"); - } catch (IOException e) { - LOGGER.error("Error stopping IPC server", e); - } - } - } - - public int getPort() { - return IPC_PORT; - } -} \ No newline at end of file