Skip to content

Commit fdf9388

Browse files
AlejandroPa-panizza_globant
andauthored
- Add Run MCP tool server (#1207)
* - Add Run MCP tool server * - Fix observations from review . --------- Co-authored-by: a-panizza_globant <a.panizza@globant.com>
1 parent 9dbae04 commit fdf9388

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Text;
4+
5+
namespace GeneXus.Application
6+
{
7+
8+
public static class GxRunner
9+
{
10+
public static void RunAsync(
11+
string commandLine,
12+
string workingDir,
13+
string virtualPath,
14+
string schema,
15+
Action<int> onExit = null)
16+
{
17+
var stdout = new StringBuilder();
18+
var stderr = new StringBuilder();
19+
20+
using var proc = new Process
21+
{
22+
StartInfo = new ProcessStartInfo
23+
{
24+
FileName = commandLine,
25+
WorkingDirectory = workingDir,
26+
UseShellExecute = false, // required for redirection
27+
CreateNoWindow = true,
28+
RedirectStandardOutput = true,
29+
RedirectStandardError = true,
30+
RedirectStandardInput = false, // flip to true only if you need to write to stdin
31+
StandardOutputEncoding = Encoding.UTF8,
32+
StandardErrorEncoding = Encoding.UTF8
33+
},
34+
EnableRaisingEvents = true
35+
};
36+
37+
proc.StartInfo.ArgumentList.Add(virtualPath);
38+
proc.StartInfo.ArgumentList.Add(schema);
39+
40+
proc.OutputDataReceived += (_, e) =>
41+
{
42+
if (e.Data is null) return;
43+
stdout.AppendLine(e.Data);
44+
Console.WriteLine(e.Data); // forward to parent console (stdout)
45+
};
46+
47+
proc.ErrorDataReceived += (_, e) =>
48+
{
49+
if (e.Data is null) return;
50+
stderr.AppendLine(e.Data);
51+
Console.Error.WriteLine(e.Data); // forward to parent console (stderr)
52+
};
53+
54+
proc.Exited += (sender, e) =>
55+
{
56+
var p = (Process)sender!;
57+
int exitCode = p.ExitCode;
58+
p.Dispose();
59+
60+
Console.WriteLine($"[{DateTime.Now:T}] Process exited with code {exitCode}");
61+
62+
// Optional: call user-provided callback
63+
onExit?.Invoke(exitCode);
64+
};
65+
66+
if (!proc.Start())
67+
throw new InvalidOperationException("Failed to start process");
68+
Console.WriteLine($"[{DateTime.Now:T}] MCP Server Started.");
69+
}
70+
}
71+
72+
}

dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public static void Main(string[] args)
6262
LocatePhysicalLocalPath();
6363

6464
}
65+
66+
MCPTools.ServerStart(Startup.VirtualPath, Startup.LocalPath, schema);
67+
6568
if (port == DEFAULT_PORT)
6669
{
6770
BuildWebHost(null).Run();
@@ -839,6 +842,37 @@ public void Apply(ApplicationModel application)
839842
}
840843
}
841844

845+
static class MCPTools
846+
{
847+
public static void ServerStart(string virtualPath, string workingDir, string schema)
848+
{
849+
IGXLogger log = GXLoggerFactory.GetLogger(typeof(CustomBadRequestObjectResult).FullName);
850+
bool isMpcServer = false;
851+
try
852+
{
853+
List<string> L = Directory.GetFiles(Path.Combine(workingDir,"bin"), "*mcp_service.dll").ToList<string>();
854+
isMpcServer = L.Count > 0;
855+
if (isMpcServer)
856+
{
857+
GXLogging.Info(log, "Start MCP Server");
858+
859+
GxRunner.RunAsync("GxMcpStartup.exe", Path.Combine(workingDir,"bin"), virtualPath, schema, onExit: exitCode =>
860+
{
861+
if (exitCode == 0)
862+
Console.WriteLine("Process completed successfully.");
863+
else
864+
Console.Error.WriteLine($"Process failed (exit code {exitCode})");
865+
});
866+
}
867+
}
868+
catch (Exception ex)
869+
{
870+
GXLogging.Error(log, "Error starting MCP Server", ex);
871+
}
872+
}
873+
}
874+
875+
842876
internal class HomeControllerConvention : IApplicationModelConvention
843877
{
844878
private static bool FindAndStoreDefaultFile()

0 commit comments

Comments
 (0)