1- using ModelContextProtocol . Client ;
1+ using Microsoft . Extensions . AI ;
2+ using Microsoft . Extensions . Logging ;
3+ using ModelContextProtocol . Client ;
24using ModelContextProtocol . Protocol . Messages ;
35using ModelContextProtocol . Protocol . Transport ;
46using ModelContextProtocol . Protocol . Types ;
57using ModelContextProtocol . Server ;
68using ModelContextProtocol . Tests . Utils ;
7- using Microsoft . Extensions . AI ;
8- using Microsoft . Extensions . Logging ;
99using Moq ;
10+ using System . Reflection ;
1011
1112namespace ModelContextProtocol . Tests . Server ;
1213
@@ -43,7 +44,7 @@ private static McpServerOptions CreateOptions(ServerCapabilities? capabilities =
4344 public async Task Constructor_Should_Initialize_With_Valid_Parameters ( )
4445 {
4546 // Arrange & Act
46- await using var server = new McpServer ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
47+ await using var server = McpServerFactory . Create ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
4748
4849 // Assert
4950 Assert . NotNull ( server ) ;
@@ -53,21 +54,21 @@ public async Task Constructor_Should_Initialize_With_Valid_Parameters()
5354 public void Constructor_Throws_For_Null_Transport ( )
5455 {
5556 // Arrange, Act & Assert
56- Assert . Throws < ArgumentNullException > ( ( ) => new McpServer ( null ! , _options , _loggerFactory . Object , _serviceProvider ) ) ;
57+ Assert . Throws < ArgumentNullException > ( ( ) => McpServerFactory . Create ( null ! , _options , _loggerFactory . Object , _serviceProvider ) ) ;
5758 }
5859
5960 [ Fact ]
6061 public void Constructor_Throws_For_Null_Options ( )
6162 {
6263 // Arrange, Act & Assert
63- Assert . Throws < ArgumentNullException > ( ( ) => new McpServer ( _serverTransport . Object , null ! , _loggerFactory . Object , _serviceProvider ) ) ;
64+ Assert . Throws < ArgumentNullException > ( ( ) => McpServerFactory . Create ( _serverTransport . Object , null ! , _loggerFactory . Object , _serviceProvider ) ) ;
6465 }
6566
6667 [ Fact ]
6768 public async Task Constructor_Does_Not_Throw_For_Null_Logger ( )
6869 {
6970 // Arrange & Act
70- await using var server = new McpServer ( _serverTransport . Object , _options , null , _serviceProvider ) ;
71+ await using var server = McpServerFactory . Create ( _serverTransport . Object , _options , null , _serviceProvider ) ;
7172
7273 // Assert
7374 Assert . NotNull ( server ) ;
@@ -77,25 +78,17 @@ public async Task Constructor_Does_Not_Throw_For_Null_Logger()
7778 public async Task Constructor_Does_Not_Throw_For_Null_ServiceProvider ( )
7879 {
7980 // Arrange & Act
80- await using var server = new McpServer ( _serverTransport . Object , _options , _loggerFactory . Object , null ) ;
81+ await using var server = McpServerFactory . Create ( _serverTransport . Object , _options , _loggerFactory . Object , null ) ;
8182
8283 // Assert
8384 Assert . NotNull ( server ) ;
8485 }
8586
86- [ Fact ]
87- public async Task Property_EndpointName_Return_Infos ( )
88- {
89- await using var server = new McpServer ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
90- server . ClientInfo = new Implementation { Name = "TestClient" , Version = "1.1" } ;
91- Assert . Equal ( "Server (TestServer 1.0), Client (TestClient 1.1)" , server . EndpointName ) ;
92- }
93-
9487 [ Fact ]
9588 public async Task StartAsync_Should_Throw_InvalidOperationException_If_Already_Initializing ( )
9689 {
9790 // Arrange
98- await using var server = new McpServer ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
91+ await using var server = McpServerFactory . Create ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
9992 server . GetType ( ) . GetField ( "_isInitializing" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ? . SetValue ( server , true ) ;
10093
10194 // Act & Assert
@@ -106,8 +99,8 @@ public async Task StartAsync_Should_Throw_InvalidOperationException_If_Already_I
10699 public async Task StartAsync_Should_Do_Nothing_If_Already_Initialized ( )
107100 {
108101 // Arrange
109- await using var server = new McpServer ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
110- server . IsInitialized = true ;
102+ await using var server = McpServerFactory . Create ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
103+ SetInitialized ( server , true ) ;
111104
112105 await server . StartAsync ( TestContext . Current . CancellationToken ) ;
113106
@@ -119,7 +112,7 @@ public async Task StartAsync_Should_Do_Nothing_If_Already_Initialized()
119112 public async Task StartAsync_ShouldStartListening ( )
120113 {
121114 // Arrange
122- await using var server = new McpServer ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
115+ await using var server = McpServerFactory . Create ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
123116
124117 // Act
125118 await server . StartAsync ( TestContext . Current . CancellationToken ) ;
@@ -132,7 +125,7 @@ public async Task StartAsync_ShouldStartListening()
132125 public async Task StartAsync_Sets_Initialized_After_Transport_Responses_Initialized_Notification ( )
133126 {
134127 await using var transport = new TestServerTransport ( ) ;
135- await using var server = new McpServer ( transport , _options , _loggerFactory . Object , _serviceProvider ) ;
128+ await using var server = McpServerFactory . Create ( transport , _options , _loggerFactory . Object , _serviceProvider ) ;
136129
137130 await server . StartAsync ( TestContext . Current . CancellationToken ) ;
138131
@@ -152,8 +145,8 @@ await transport.SendMessageAsync(new JsonRpcNotification
152145 public async Task RequestSamplingAsync_Should_Throw_McpServerException_If_Client_Does_Not_Support_Sampling ( )
153146 {
154147 // Arrange
155- await using var server = new McpServer ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
156- server . ClientCapabilities = new ClientCapabilities ( ) ;
148+ await using var server = McpServerFactory . Create ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
149+ SetClientCapabilities ( server , new ClientCapabilities ( ) ) ;
157150
158151 var action = ( ) => server . RequestSamplingAsync ( new CreateMessageRequestParams { Messages = [ ] } , CancellationToken . None ) ;
159152
@@ -166,8 +159,8 @@ public async Task RequestSamplingAsync_Should_SendRequest()
166159 {
167160 // Arrange
168161 await using var transport = new TestServerTransport ( ) ;
169- await using var server = new McpServer ( transport , _options , _loggerFactory . Object , _serviceProvider ) ;
170- server . ClientCapabilities = new ClientCapabilities { Sampling = new SamplingCapability ( ) } ;
162+ await using var server = McpServerFactory . Create ( transport , _options , _loggerFactory . Object , _serviceProvider ) ;
163+ SetClientCapabilities ( server , new ClientCapabilities { Sampling = new SamplingCapability ( ) } ) ;
171164
172165 await server . StartAsync ( TestContext . Current . CancellationToken ) ;
173166
@@ -184,8 +177,8 @@ public async Task RequestSamplingAsync_Should_SendRequest()
184177 public async Task RequestRootsAsync_Should_Throw_McpServerException_If_Client_Does_Not_Support_Roots ( )
185178 {
186179 // Arrange
187- await using var server = new McpServer ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
188- server . ClientCapabilities = new ClientCapabilities ( ) ;
180+ await using var server = McpServerFactory . Create ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
181+ SetClientCapabilities ( server , new ClientCapabilities ( ) ) ;
189182
190183 // Act & Assert
191184 await Assert . ThrowsAsync < ArgumentException > ( "server" , ( ) => server . RequestRootsAsync ( new ListRootsRequestParams ( ) , CancellationToken . None ) ) ;
@@ -196,8 +189,8 @@ public async Task RequestRootsAsync_Should_SendRequest()
196189 {
197190 // Arrange
198191 await using var transport = new TestServerTransport ( ) ;
199- await using var server = new McpServer ( transport , _options , _loggerFactory . Object , _serviceProvider ) ;
200- server . ClientCapabilities = new ClientCapabilities { Roots = new RootsCapability ( ) } ;
192+ await using var server = McpServerFactory . Create ( transport , _options , _loggerFactory . Object , _serviceProvider ) ;
193+ SetClientCapabilities ( server , new ClientCapabilities { Roots = new RootsCapability ( ) } ) ;
201194 await server . StartAsync ( TestContext . Current . CancellationToken ) ;
202195
203196 // Act
@@ -213,8 +206,8 @@ public async Task RequestRootsAsync_Should_SendRequest()
213206 [ Fact ]
214207 public async Task Throws_Exception_If_Not_Connected ( )
215208 {
216- await using var server = new McpServer ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
217- server . ClientCapabilities = new ClientCapabilities { Roots = new RootsCapability ( ) } ;
209+ await using var server = McpServerFactory . Create ( _serverTransport . Object , _options , _loggerFactory . Object , _serviceProvider ) ;
210+ SetClientCapabilities ( server , new ClientCapabilities { Roots = new RootsCapability ( ) } ) ;
218211 _serverTransport . SetupGet ( t => t . IsConnected ) . Returns ( false ) ;
219212
220213 var action = async ( ) => await server . RequestRootsAsync ( new ListRootsRequestParams ( ) , CancellationToken . None ) ;
@@ -522,7 +515,7 @@ private async Task Can_Handle_Requests(ServerCapabilities? serverCapabilities, s
522515 var options = CreateOptions ( serverCapabilities ) ;
523516 configureOptions ? . Invoke ( options ) ;
524517
525- await using var server = new McpServer ( transport , options , _loggerFactory . Object , _serviceProvider ) ;
518+ await using var server = McpServerFactory . Create ( transport , options , _loggerFactory . Object , _serviceProvider ) ;
526519
527520 await server . StartAsync ( ) ;
528521
@@ -595,6 +588,20 @@ public async Task AsSamplingChatClient_HandlesRequestResponse()
595588 Assert . Equal ( ChatRole . Assistant , response . Messages [ 0 ] . Role ) ;
596589 }
597590
591+ private static void SetClientCapabilities ( IMcpServer server , ClientCapabilities capabilities )
592+ {
593+ PropertyInfo ? property = server . GetType ( ) . GetProperty ( "ClientCapabilities" , BindingFlags . Public | BindingFlags . Instance ) ;
594+ Assert . NotNull ( property ) ;
595+ property . SetValue ( server , capabilities ) ;
596+ }
597+
598+ private static void SetInitialized ( IMcpServer server , bool isInitialized )
599+ {
600+ PropertyInfo ? property = server . GetType ( ) . GetProperty ( "IsInitialized" , BindingFlags . Public | BindingFlags . Instance ) ;
601+ Assert . NotNull ( property ) ;
602+ property . SetValue ( server , isInitialized ) ;
603+ }
604+
598605 private sealed class TestServerForIChatClient ( bool supportsSampling ) : IMcpServer
599606 {
600607 public ClientCapabilities ? ClientCapabilities =>
0 commit comments