49 lines
2.0 KiB
C#
49 lines
2.0 KiB
C#
using ClawdDotNet.Core.Instances;
|
|
using ClawdDotNet.Core.Mcp;
|
|
|
|
namespace ClawdDotNet.Desktop;
|
|
|
|
/// <summary>
|
|
/// #713: <c>ClawdDotNet --mcp-bridge --instance <dir></c> — the instance's MCP server
|
|
/// over stdio, for a runtime that starts its MCP servers as child processes. In a sandbox
|
|
/// (#712), where there is no instance directory, <c>--socket <path></c> names the
|
|
/// mounted MCP socket instead.
|
|
///
|
|
/// <para>No application is built here: no settings, no database, no engine. The bridge
|
|
/// only carries messages to the running instance over its MCP socket; everything is
|
|
/// decided there. Diagnostics go to stderr — stdout belongs to the protocol.</para>
|
|
/// </summary>
|
|
internal static class McpBridgeRun
|
|
{
|
|
private const int ExitOk = 0;
|
|
private const int ExitNoInstance = 2;
|
|
private const int ExitNoToken = 5;
|
|
|
|
public static async Task<int> RunAsync(string[] args)
|
|
{
|
|
var socketPath = CommandLine.ReadValue(args, McpStdioBridge.SocketArgument);
|
|
var instancePath = CommandLine.ReadInstancePath(args);
|
|
if (socketPath is null && (string.IsNullOrWhiteSpace(instancePath) || !Directory.Exists(instancePath)))
|
|
{
|
|
await Console.Error.WriteLineAsync($"Expected {CommandLine.InstanceArgument} <instance directory> or {McpStdioBridge.SocketArgument} <path>.");
|
|
return ExitNoInstance;
|
|
}
|
|
|
|
var (token, problem) = McpStdioBridge.ReadToken(Environment.GetEnvironmentVariable);
|
|
if (token is null)
|
|
{
|
|
await Console.Error.WriteLineAsync(problem);
|
|
return ExitNoToken;
|
|
}
|
|
|
|
using var client = socketPath is not null
|
|
? InstanceEndpoint.CreateMcpClientForSocket(socketPath)
|
|
: InstanceEndpoint.CreateMcpClient(instancePath!);
|
|
using var stdin = new StreamReader(Console.OpenStandardInput());
|
|
await using var stdout = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
|
|
|
|
await McpStdioBridge.RunAsync(stdin, stdout, client, token, CancellationToken.None);
|
|
return ExitOk;
|
|
}
|
|
}
|