33using ModelContextProtocol . Protocol ;
44using System . Collections . Concurrent ;
55using System . ComponentModel ;
6+ using System . Diagnostics ;
67using System . Globalization ;
78using System . Reflection ;
89using System . Text ;
@@ -64,8 +65,8 @@ internal sealed class AIFunctionMcpServerResource : McpServerResource
6465 return Create (
6566 AIFunctionFactory . Create ( method , args =>
6667 {
67- var request = ( RequestContext < ReadResourceRequestParams > ) args . Context ! [ typeof ( RequestContext < ReadResourceRequestParams > ) ] ! ;
68- return createTargetFunc ( request ) ;
68+ Debug . Assert ( args . Services is RequestServiceProvider < ReadResourceRequestParams > , $ "The service provider should be a { nameof ( RequestServiceProvider < ReadResourceRequestParams > ) } for this method to work correctly." ) ;
69+ return createTargetFunc ( ( ( RequestServiceProvider < ReadResourceRequestParams > ) args . Services ! ) . Request ) ;
6970 } , CreateAIFunctionFactoryOptions ( method , options ) ) ,
7071 options ) ;
7172 }
@@ -81,54 +82,15 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
8182 JsonSchemaCreateOptions = options ? . SchemaCreateOptions ,
8283 ConfigureParameterBinding = pi =>
8384 {
84- if ( pi . ParameterType == typeof ( RequestContext < ReadResourceRequestParams > ) )
85- {
86- return new ( )
87- {
88- ExcludeFromSchema = true ,
89- BindParameter = ( pi , args ) => GetRequestContext ( args ) ,
90- } ;
91- }
92-
93- if ( pi . ParameterType == typeof ( IMcpServer ) )
94- {
95- return new ( )
96- {
97- ExcludeFromSchema = true ,
98- BindParameter = ( pi , args ) => GetRequestContext ( args ) ? . Server ,
99- } ;
100- }
101-
102- if ( pi . ParameterType == typeof ( IProgress < ProgressNotificationValue > ) )
103- {
104- // Bind IProgress<ProgressNotificationValue> to the progress token in the request,
105- // if there is one. If we can't get one, return a nop progress.
106- return new ( )
107- {
108- ExcludeFromSchema = true ,
109- BindParameter = ( pi , args ) =>
110- {
111- var requestContent = GetRequestContext ( args ) ;
112- if ( requestContent ? . Server is { } server &&
113- requestContent ? . Params ? . ProgressToken is { } progressToken )
114- {
115- return new TokenProgress ( server , progressToken ) ;
116- }
117-
118- return NullProgress . Instance ;
119- } ,
120- } ;
121- }
122-
123- if ( options ? . Services is { } services &&
124- services . GetService < IServiceProviderIsService > ( ) is { } ispis &&
125- ispis . IsService ( pi . ParameterType ) )
85+ if ( RequestServiceProvider < ReadResourceRequestParams > . IsAugmentedWith ( pi . ParameterType ) ||
86+ ( options ? . Services ? . GetService < IServiceProviderIsService > ( ) is { } ispis &&
87+ ispis . IsService ( pi . ParameterType ) ) )
12688 {
12789 return new ( )
12890 {
12991 ExcludeFromSchema = true ,
13092 BindParameter = ( pi , args ) =>
131- GetRequestContext ( args ) ? . Services ? . GetService ( pi . ParameterType ) ??
93+ args . Services ? . GetService ( pi . ParameterType ) ??
13294 ( pi . HasDefaultValue ? null :
13395 throw new ArgumentException ( "No service of the requested type was found." ) ) ,
13496 } ;
@@ -140,7 +102,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
140102 {
141103 ExcludeFromSchema = true ,
142104 BindParameter = ( pi , args ) =>
143- ( GetRequestContext ( args ) ? . Services as IKeyedServiceProvider ) ? . GetKeyedService ( pi . ParameterType , keyedAttr . Key ) ??
105+ ( args ? . Services as IKeyedServiceProvider ) ? . GetKeyedService ( pi . ParameterType , keyedAttr . Key ) ??
144106 ( pi . HasDefaultValue ? null :
145107 throw new ArgumentException ( "No service of the requested type was found." ) ) ,
146108 } ;
@@ -172,17 +134,6 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
172134 }
173135
174136 return default ;
175-
176- static RequestContext < ReadResourceRequestParams > ? GetRequestContext ( AIFunctionArguments args )
177- {
178- if ( args . Context ? . TryGetValue ( typeof ( RequestContext < ReadResourceRequestParams > ) , out var rc ) is true &&
179- rc is RequestContext < ReadResourceRequestParams > requestContext )
180- {
181- return requestContext ;
182- }
183-
184- return null ;
185- }
186137 } ,
187138 } ;
188139
@@ -365,11 +316,8 @@ private AIFunctionMcpServerResource(AIFunction function, ResourceTemplate resour
365316 }
366317
367318 // Build up the arguments for the AIFunction call, including all of the name/value pairs from the URI.
368- AIFunctionArguments arguments = new ( )
369- {
370- Services = request . Services ,
371- Context = new Dictionary < object , object ? > ( ) { [ typeof ( RequestContext < ReadResourceRequestParams > ) ] = request }
372- } ;
319+ request . Services = new RequestServiceProvider < ReadResourceRequestParams > ( request , request . Services ) ;
320+ AIFunctionArguments arguments = new ( ) { Services = request . Services } ;
373321
374322 // For templates, populate the arguments from the URI template.
375323 if ( match is not null )
0 commit comments