11using System ;
22using System . Collections . Generic ;
3+ using System . Diagnostics ;
34using System . Threading ;
45using System . Threading . Tasks ;
56using 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