-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
95 lines (79 loc) · 3.31 KB
/
Program.cs
File metadata and controls
95 lines (79 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using Fatbeans.MCP.Client.Configuration;
using Fatbeans.MCP.Client.Features.Mcp;
using Fatbeans.MCP.Client.Features.SystemFeature;
using Fatbeans.MCP.Client.Infrastructure.Pipe;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
namespace Fatbeans.MCP.Client
{
internal static class Program
{
private static async Task<int> Main(string[] args)
{
try
{
ClientOptions options = ClientOptions.Parse(args);
if (options.ShowHelp)
{
Console.WriteLine(ClientOptions.GetUsage());
return 0;
}
if (options.RunMcpServer)
{
await McpServerHost.RunAsync(McpOptions.FromClientOptions(options));
return 0;
}
object localResponse = SystemIntrospection.Execute(options.Method, options.Parameters);
if (localResponse != null)
{
WriteResponse(localResponse, options.PrettyOutput);
return GetExitCode(localResponse);
}
using (var pipeInvoker = new PipeInvoker(options.PipeName, options.ServerName))
{
pipeInvoker.Connect(options.TimeoutMilliseconds);
var systemClient = new SystemServiceClient(pipeInvoker);
object response = Execute(options, pipeInvoker, systemClient);
WriteResponse(response, options.PrettyOutput);
return GetExitCode(response);
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return 1;
}
}
private static object Execute(ClientOptions options, PipeInvoker pipeInvoker, SystemServiceClient systemClient)
{
if (string.Equals(options.Method, Fatbeans.MCP.System.SystemMethodNames.Ping, StringComparison.OrdinalIgnoreCase))
return systemClient.Ping();
if (string.Equals(options.Method, Fatbeans.MCP.System.SystemMethodNames.Version, StringComparison.OrdinalIgnoreCase))
return systemClient.Version();
return pipeInvoker.Invoke(options.Method, options.Parameters);
}
private static void WriteResponse(object response, bool prettyOutput)
{
string json = JsonConvert.SerializeObject(
response,
prettyOutput ? Formatting.Indented : Formatting.None);
Console.WriteLine(json);
}
private static int GetExitCode(object response)
{
if (response == null)
return 2;
var pingResponse = response as TypedPipeResponse<Fatbeans.MCP.System.SystemPingResult>;
if (pingResponse != null)
return pingResponse.Success ? 0 : 2;
var versionResponse = response as TypedPipeResponse<Fatbeans.MCP.System.SystemVersionResult>;
if (versionResponse != null)
return versionResponse.Success ? 0 : 2;
var rawResponse = response as Fatbeans.MCP.Pipe.PipeResponse;
if (rawResponse != null)
return rawResponse.Success ? 0 : 2;
return 2;
}
}
}