Skip to content

Commit 926aacd

Browse files
committed
Log tool errors
1 parent ee9b04b commit 926aacd

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Microsoft.Extensions.AI;
22
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.Logging.Abstractions;
35
using ModelContextProtocol.Protocol;
46
using System.ComponentModel;
57
using System.Diagnostics.CodeAnalysis;
@@ -11,6 +13,8 @@ namespace ModelContextProtocol.Server;
1113
/// <summary>Provides an <see cref="McpServerTool"/> that's implemented via an <see cref="AIFunction"/>.</summary>
1214
internal sealed class AIFunctionMcpServerTool : McpServerTool
1315
{
16+
private readonly ILogger _logger;
17+
1418
/// <summary>
1519
/// Creates an <see cref="McpServerTool"/> instance for a method, specified via a <see cref="Delegate"/> instance.
1620
/// </summary>
@@ -19,7 +23,7 @@ internal sealed class AIFunctionMcpServerTool : McpServerTool
1923
McpServerToolCreateOptions? options)
2024
{
2125
Throw.IfNull(method);
22-
26+
2327
options = DeriveOptions(method.Method, options);
2428

2529
return Create(method.Method, method.Target, options);
@@ -172,7 +176,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
172176
{
173177
Name = options?.Name ?? function.Name,
174178
Description = options?.Description ?? function.Description,
175-
InputSchema = function.JsonSchema,
179+
InputSchema = function.JsonSchema,
176180
};
177181

178182
if (options is not null)
@@ -194,7 +198,7 @@ options.OpenWorld is not null ||
194198
}
195199
}
196200

197-
return new AIFunctionMcpServerTool(function, tool);
201+
return new AIFunctionMcpServerTool(function, tool, options?.Services);
198202
}
199203

200204
private static McpServerToolCreateOptions DeriveOptions(MethodInfo method, McpServerToolCreateOptions? options)
@@ -239,10 +243,11 @@ private static McpServerToolCreateOptions DeriveOptions(MethodInfo method, McpSe
239243
internal AIFunction AIFunction { get; }
240244

241245
/// <summary>Initializes a new instance of the <see cref="McpServerTool"/> class.</summary>
242-
private AIFunctionMcpServerTool(AIFunction function, Tool tool)
246+
private AIFunctionMcpServerTool(AIFunction function, Tool tool, IServiceProvider? serviceProvider)
243247
{
244248
AIFunction = function;
245249
ProtocolTool = tool;
250+
_logger = serviceProvider?.GetService<ILoggerFactory>()?.CreateLogger<AIFunctionMcpServerTool>() ?? (ILogger)NullLogger.Instance;
246251
}
247252

248253
/// <inheritdoc />
@@ -277,6 +282,9 @@ public override async ValueTask<CallToolResponse> InvokeAsync(
277282
}
278283
catch (Exception e) when (e is not OperationCanceledException)
279284
{
285+
_logger.LogError(e, "Error invoking AIFunction tool '{ToolName}' with arguments '{Args}'.",
286+
request.Params?.Name, string.Join(",", request.Params?.Arguments?.Keys ?? Array.Empty<string>()));
287+
280288
string errorMessage = e is McpException ?
281289
$"An error occurred invoking '{request.Params?.Name}': {e.Message}" :
282290
$"An error occurred invoking '{request.Params?.Name}'.";
@@ -300,29 +308,29 @@ public override async ValueTask<CallToolResponse> InvokeAsync(
300308
{
301309
Content = []
302310
},
303-
311+
304312
string text => new()
305313
{
306314
Content = [new() { Text = text, Type = "text" }]
307315
},
308-
316+
309317
Content content => new()
310318
{
311319
Content = [content]
312320
},
313-
321+
314322
IEnumerable<string> texts => new()
315323
{
316324
Content = [.. texts.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })]
317325
},
318-
326+
319327
IEnumerable<AIContent> contentItems => ConvertAIContentEnumerableToCallToolResponse(contentItems),
320-
328+
321329
IEnumerable<Content> contents => new()
322330
{
323331
Content = [.. contents]
324332
},
325-
333+
326334
CallToolResponse callToolResponse => callToolResponse,
327335

328336
_ => new()

0 commit comments

Comments
 (0)