Skip to content

Commit 2acdb6b

Browse files
committed
Fix tests
1 parent cb0cbd4 commit 2acdb6b

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

src/TALXIS.CLI.MCP/Program.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
using TALXIS.CLI.MCP;
88
using System.Reflection;
99

10-
var builder = new HostApplicationBuilder(args);
11-
builder.Logging.AddConsole(consoleLogOptions =>
12-
{
13-
// Configure all logs to go to stderr
14-
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
15-
});
16-
1710
// Create a single instance of McpToolRegistry
1811
var mcpToolRegistry = new McpToolRegistry();
1912

13+
try
14+
{
15+
var builder = new HostApplicationBuilder(args);
16+
builder.Logging.AddConsole(consoleLogOptions =>
17+
{
18+
// Configure all logs to go to stderr
19+
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
20+
});
21+
2022
builder.Services.AddMcpServer(options =>
2123
{
2224
var version = typeof(Program).Assembly.GetName().Version?.ToString() ?? "unknown";
@@ -35,13 +37,16 @@
3537
CallToolHandler = CallToolAsync
3638
}
3739
};
38-
})
39-
.WithStdioServerTransport();
40+
})
41+
.WithStdioServerTransport();
4042

41-
42-
await builder.Build().RunAsync();
43-
44-
// MCP tool listing
43+
await builder.Build().RunAsync();
44+
}
45+
catch (Exception ex)
46+
{
47+
Console.Error.WriteLine($"Fatal error starting MCP server: {ex}");
48+
Environment.Exit(1);
49+
}// MCP tool listing
4550
ValueTask<ListToolsResult> ListToolsAsync(RequestContext<ListToolsRequestParams> ctx, CancellationToken ct)
4651
=> ValueTask.FromResult(new ListToolsResult { Tools = mcpToolRegistry.ListTools() });
4752

tests/TALXIS.CLI.IntegrationTests/McpClient.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using ModelContextProtocol.Client;
@@ -24,15 +25,27 @@ private McpClient(IMcpClient client)
2425

2526
private static async Task<McpClient> CreateInstanceAsync()
2627
{
28+
var mcpProjectPath = GetMcpProjectPath();
29+
30+
// Build the MCP project first to ensure it's available
31+
await BuildMcpProjectAsync(mcpProjectPath);
32+
2733
var transport = new StdioClientTransport(new StdioClientTransportOptions
2834
{
2935
Name = "TALXIS CLI MCP Test Client",
3036
Command = "dotnet",
31-
Arguments = ["run", "--project", GetMcpProjectPath(), "--no-build"]
37+
Arguments = ["run", "--project", mcpProjectPath]
3238
});
3339

34-
var client = await McpClientFactory.CreateAsync(transport);
35-
return new McpClient(client);
40+
try
41+
{
42+
var client = await McpClientFactory.CreateAsync(transport);
43+
return new McpClient(client);
44+
}
45+
catch (Exception ex)
46+
{
47+
throw new InvalidOperationException($"Failed to create MCP client. This might be due to server startup issues. Error: {ex.Message}", ex);
48+
}
3649
}
3750

3851
public async Task<CallToolResult> CallToolAsync(string toolName, Dictionary<string, object> arguments = null)
@@ -66,4 +79,34 @@ private static string GetMcpProjectPath()
6679

6780
return System.IO.Path.Combine(dir.FullName, "src", "TALXIS.CLI.MCP", "TALXIS.CLI.MCP.csproj");
6881
}
82+
83+
/// <summary>
84+
/// Builds the MCP project to ensure it's available for testing.
85+
/// This is especially important in CI environments where --no-build might not work reliably.
86+
/// </summary>
87+
private static async Task BuildMcpProjectAsync(string projectPath)
88+
{
89+
var processInfo = new ProcessStartInfo
90+
{
91+
FileName = "dotnet",
92+
Arguments = $"build \"{projectPath}\" --configuration Release",
93+
UseShellExecute = false,
94+
RedirectStandardOutput = true,
95+
RedirectStandardError = true,
96+
CreateNoWindow = true
97+
};
98+
99+
using var process = Process.Start(processInfo);
100+
if (process == null)
101+
throw new InvalidOperationException("Failed to start dotnet build process");
102+
103+
await process.WaitForExitAsync();
104+
105+
if (process.ExitCode != 0)
106+
{
107+
var stdout = await process.StandardOutput.ReadToEndAsync();
108+
var stderr = await process.StandardError.ReadToEndAsync();
109+
throw new InvalidOperationException($"Failed to build MCP project. Exit code: {process.ExitCode}. Output: {stdout}. Error: {stderr}");
110+
}
111+
}
69112
}

0 commit comments

Comments
 (0)