@@ -107,7 +107,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer
107
107
throw new Exception ( $ "Unknown tool '{ request . Params ? . Name } '") ;
108
108
}
109
109
} )
110
- . WithTools < EchoTool > ( serializerOptions : JsonContext . Default . Options ) ;
110
+ . WithTools < EchoTool > ( serializerOptions : BuilderToolsJsonContext . Default . Options ) ;
111
111
112
112
services . AddSingleton ( new ObjectWithId ( ) ) ;
113
113
}
@@ -429,8 +429,13 @@ public void Empty_Enumerables_Is_Allowed()
429
429
[ Fact ]
430
430
public void Register_Tools_From_Current_Assembly ( )
431
431
{
432
+ if ( ! JsonSerializer . IsReflectionEnabledByDefault )
433
+ {
434
+ return ;
435
+ }
436
+
432
437
ServiceCollection sc = new ( ) ;
433
- sc . AddMcpServer ( ) . WithToolsFromAssembly ( serializerOptions : JsonContext . Default . Options ) ;
438
+ sc . AddMcpServer ( ) . WithToolsFromAssembly ( ) ;
434
439
IServiceProvider services = sc . BuildServiceProvider ( ) ;
435
440
436
441
Assert . Contains ( services . GetServices < McpServerTool > ( ) , t => t . ProtocolTool . Name == "Echo" ) ;
@@ -446,7 +451,7 @@ public void WithTools_Parameters_Satisfiable_From_DI(bool parameterInServices)
446
451
{
447
452
sc . AddSingleton ( new ComplexObject ( ) ) ;
448
453
}
449
- sc . AddMcpServer ( ) . WithTools ( [ typeof ( EchoTool ) ] , JsonContext . Default . Options ) ;
454
+ sc . AddMcpServer ( ) . WithTools ( [ typeof ( EchoTool ) ] , BuilderToolsJsonContext . Default . Options ) ;
450
455
IServiceProvider services = sc . BuildServiceProvider ( ) ;
451
456
452
457
McpServerTool tool = services . GetServices < McpServerTool > ( ) . First ( t => t . ProtocolTool . Name == "EchoComplex" ) ;
@@ -460,13 +465,19 @@ public void WithTools_Parameters_Satisfiable_From_DI(bool parameterInServices)
460
465
}
461
466
}
462
467
468
+
463
469
[ Theory ]
464
470
[ InlineData ( ServiceLifetime . Singleton ) ]
465
471
[ InlineData ( ServiceLifetime . Scoped ) ]
466
472
[ InlineData ( ServiceLifetime . Transient ) ]
467
473
[ InlineData ( null ) ]
468
474
public void WithToolsFromAssembly_Parameters_Satisfiable_From_DI ( ServiceLifetime ? lifetime )
469
475
{
476
+ if ( ! JsonSerializer . IsReflectionEnabledByDefault )
477
+ {
478
+ return ;
479
+ }
480
+
470
481
ServiceCollection sc = new ( ) ;
471
482
switch ( lifetime )
472
483
{
@@ -483,7 +494,7 @@ public void WithToolsFromAssembly_Parameters_Satisfiable_From_DI(ServiceLifetime
483
494
break ;
484
495
}
485
496
486
- sc . AddMcpServer ( ) . WithToolsFromAssembly ( serializerOptions : JsonContext . Default . Options ) ;
497
+ sc . AddMcpServer ( ) . WithToolsFromAssembly ( ) ;
487
498
IServiceProvider services = sc . BuildServiceProvider ( ) ;
488
499
489
500
McpServerTool tool = services . GetServices < McpServerTool > ( ) . First ( t => t . ProtocolTool . Name == "EchoComplex" ) ;
@@ -526,9 +537,9 @@ public void Register_Tools_From_Multiple_Sources()
526
537
{
527
538
ServiceCollection sc = new ( ) ;
528
539
sc . AddMcpServer ( )
529
- . WithTools < EchoTool > ( serializerOptions : JsonContext . Default . Options )
530
- . WithTools < AnotherToolType > ( serializerOptions : JsonContext . Default . Options )
531
- . WithTools ( [ typeof ( ToolTypeWithNoAttribute ) ] , JsonContext . Default . Options ) ;
540
+ . WithTools < EchoTool > ( serializerOptions : BuilderToolsJsonContext . Default . Options )
541
+ . WithTools < AnotherToolType > ( serializerOptions : BuilderToolsJsonContext . Default . Options )
542
+ . WithTools ( [ typeof ( ToolTypeWithNoAttribute ) ] , BuilderToolsJsonContext . Default . Options ) ;
532
543
IServiceProvider services = sc . BuildServiceProvider ( ) ;
533
544
534
545
Assert . Contains ( services . GetServices < McpServerTool > ( ) , t => t . ProtocolTool . Name == "double_echo" ) ;
@@ -759,4 +770,22 @@ public class ObjectWithId
759
770
{
760
771
public string Id { get ; set ; } = Guid . NewGuid ( ) . ToString ( "N" ) ;
761
772
}
762
- }
773
+
774
+ public class ComplexObject
775
+ {
776
+ public string ? Name { get ; set ; }
777
+ public int Age { get ; set ; }
778
+ }
779
+
780
+ [ JsonSerializable ( typeof ( bool ) ) ]
781
+ [ JsonSerializable ( typeof ( int ) ) ]
782
+ [ JsonSerializable ( typeof ( long ) ) ]
783
+ [ JsonSerializable ( typeof ( double ) ) ]
784
+ [ JsonSerializable ( typeof ( string ) ) ]
785
+ [ JsonSerializable ( typeof ( DateTime ) ) ]
786
+ [ JsonSerializable ( typeof ( DateTimeOffset ) ) ]
787
+ [ JsonSerializable ( typeof ( ComplexObject ) ) ]
788
+ [ JsonSerializable ( typeof ( string [ ] ) ) ]
789
+ [ JsonSerializable ( typeof ( JsonElement ) ) ]
790
+ partial class BuilderToolsJsonContext : JsonSerializerContext ;
791
+ }
0 commit comments