77using Microsoft . Extensions . DependencyInjection ;
88using ModelContextProtocol ;
99using ModelContextProtocol . Protocol ;
10+ using ModelContextProtocol . Server ;
1011
1112namespace Azure . DataApiBuilder . Mcp . Core
1213{
@@ -16,80 +17,84 @@ namespace Azure.DataApiBuilder.Mcp.Core
1617 internal static class McpServerConfiguration
1718 {
1819 /// <summary>
19- /// Configures the MCP server with tool capabilities
20+ /// Configures the MCP server with tool capabilities.
2021 /// </summary>
2122 internal static IServiceCollection ConfigureMcpServer ( this IServiceCollection services )
2223 {
23- services . AddMcpServer ( options =>
24+ services . AddMcpServer ( )
25+ . WithListToolsHandler ( ( RequestContext < ListToolsRequestParams > request , CancellationToken ct ) =>
2426 {
25- options . ServerInfo = new ( ) { Name = McpProtocolDefaults . MCP_SERVER_NAME , Version = McpProtocolDefaults . MCP_SERVER_VERSION } ;
26- options . Capabilities = new ( )
27+ McpToolRegistry ? toolRegistry = request . Services ? . GetRequiredService < McpToolRegistry > ( ) ;
28+ if ( toolRegistry == null )
2729 {
28- Tools = new ( )
29- {
30- ListToolsHandler = ( request , ct ) =>
31- {
32- McpToolRegistry ? toolRegistry = request . Services ? . GetRequiredService < McpToolRegistry > ( ) ;
33- if ( toolRegistry == null )
34- {
35- throw new InvalidOperationException ( "Tool registry is not available." ) ;
36- }
30+ throw new InvalidOperationException ( "Tool registry is not available." ) ;
31+ }
3732
38- List < Tool > tools = toolRegistry . GetAllTools ( ) . ToList ( ) ;
33+ List < Tool > tools = toolRegistry . GetAllTools ( ) . ToList ( ) ;
3934
40- return ValueTask . FromResult ( new ListToolsResult
41- {
42- Tools = tools
43- } ) ;
44- } ,
45- CallToolHandler = async ( request , ct ) =>
46- {
47- McpToolRegistry ? toolRegistry = request . Services ? . GetRequiredService < McpToolRegistry > ( ) ;
48- if ( toolRegistry == null )
49- {
50- throw new InvalidOperationException ( "Tool registry is not available." ) ;
51- }
52-
53- string ? toolName = request . Params ? . Name ;
54- if ( string . IsNullOrEmpty ( toolName ) )
55- {
56- throw new McpException ( "Tool name is required." ) ;
57- }
35+ return ValueTask . FromResult ( new ListToolsResult
36+ {
37+ Tools = tools
38+ } ) ;
39+ } )
40+ . WithCallToolHandler ( async ( RequestContext < CallToolRequestParams > request , CancellationToken ct ) =>
41+ {
42+ McpToolRegistry ? toolRegistry = request . Services ? . GetRequiredService < McpToolRegistry > ( ) ;
43+ if ( toolRegistry == null )
44+ {
45+ throw new InvalidOperationException ( "Tool registry is not available." ) ;
46+ }
5847
59- if ( ! toolRegistry . TryGetTool ( toolName , out IMcpTool ? tool ) )
60- {
61- throw new McpException ( $ "Unknown tool: '{ toolName } '") ;
62- }
48+ string ? toolName = request . Params ? . Name ;
49+ if ( string . IsNullOrEmpty ( toolName ) )
50+ {
51+ throw new McpException ( "Tool name is required." ) ;
52+ }
6353
64- JsonDocument ? arguments = null ;
65- try
66- {
67- if ( request . Params ? . Arguments != null )
68- {
69- // Convert IReadOnlyDictionary<string, JsonElement> to JsonDocument
70- Dictionary < string , object ? > jsonObject = new ( ) ;
71- foreach ( KeyValuePair < string , JsonElement > kvp in request . Params . Arguments )
72- {
73- jsonObject [ kvp . Key ] = kvp . Value ;
74- }
54+ if ( ! toolRegistry . TryGetTool ( toolName , out IMcpTool ? tool ) )
55+ {
56+ throw new McpException ( $ "Unknown tool: '{ toolName } '") ;
57+ }
7558
76- string json = JsonSerializer . Serialize ( jsonObject ) ;
77- arguments = JsonDocument . Parse ( json ) ;
78- }
59+ if ( tool is null || request . Services is null )
60+ {
61+ throw new InvalidOperationException ( "Tool or service provider unexpectedly null." ) ;
62+ }
7963
80- return await McpTelemetryHelper . ExecuteWithTelemetryAsync (
81- tool ! , toolName , arguments , request . Services ! , ct ) ;
82- }
83- finally
84- {
85- arguments ? . Dispose ( ) ;
86- }
64+ JsonDocument ? arguments = null ;
65+ try
66+ {
67+ if ( request . Params ? . Arguments != null )
68+ {
69+ // Convert IReadOnlyDictionary<string, JsonElement> to JsonDocument
70+ Dictionary < string , object ? > jsonObject = new ( ) ;
71+ foreach ( KeyValuePair < string , JsonElement > kvp in request . Params . Arguments )
72+ {
73+ jsonObject [ kvp . Key ] = kvp . Value ;
8774 }
75+
76+ string json = JsonSerializer . Serialize ( jsonObject ) ;
77+ arguments = JsonDocument . Parse ( json ) ;
8878 }
89- } ;
79+
80+ return await McpTelemetryHelper . ExecuteWithTelemetryAsync (
81+ tool , toolName , arguments , request . Services , ct ) ;
82+ }
83+ finally
84+ {
85+ arguments ? . Dispose ( ) ;
86+ }
9087 } )
9188 . WithHttpTransport ( ) ;
9289
90+ // Configure underlying MCP server options
91+ services . PostConfigure < McpServerOptions > ( options =>
92+ {
93+ options . ServerInfo = new ( ) { Name = McpProtocolDefaults . MCP_SERVER_NAME , Version = McpProtocolDefaults . MCP_SERVER_VERSION } ;
94+ options . Capabilities ??= new ( ) ;
95+ options . Capabilities . Tools ??= new ( ) ;
96+ } ) ;
97+
9398 return services ;
9499 }
95100 }
0 commit comments