MemScan-EDR/src-csharp/MemScanEDR/Services/CeMcpManager.cs

66 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace MemScanEDR.Services;
public class CeMcpManager
{
private Process? _process;
public bool IsRunning => _process != null && !_process.HasExited;
public string FindServerPath()
{
var candidates = new[]
{
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "src", "ce_mcp_server", "server.py"),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ce_mcp_server", "server.py"),
@"d:\github\ce-mcp-server\server.py",
@"d:\github\MemScan-EDR\src\ce_mcp_server\server.py",
Path.Combine(Directory.GetCurrentDirectory(), "src", "ce_mcp_server", "server.py"),
};
foreach (var c in candidates)
if (File.Exists(c)) return c;
return "";
}
public bool Start(string pythonCommand = "python", string? serverPath = null)
{
if (IsRunning) return true;
serverPath ??= FindServerPath();
if (string.IsNullOrEmpty(serverPath) || !File.Exists(serverPath)) return false;
try
{
_process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = pythonCommand,
Arguments = $"\"{serverPath}\"",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
},
EnableRaisingEvents = true
};
_process.Start();
return true;
}
catch { return false; }
}
public void Stop()
{
if (_process != null && !_process.HasExited)
{
try { _process.Kill(); } catch { }
_process.Dispose();
_process = null;
}
}
}