Skip to content

AddCallToolFilter does not allow to catch exceptions #820

@gophph

Description

@gophph

Describe the bug
AddCallToolFilter does not allow to handle exception as exception is getting handled and suppressed inside next filter

To Reproduce
Steps to reproduce the behavior:

  1. Use following code described in filters.md to handle exception
        services
            .AddMcpServer()
            .AddCallToolFilter(next => async (context, cancellationToken) =>
            {
                try
                {
                    var result = await next(context, cancellationToken);
                    return result;
                }
                catch (Exception ex)
                {
                    return new CallToolResult
                    {
                        Content = [new TextContentBlock { Type = "text", Text = $"Error: {ex.Message}" }],
                        IsError = true
                    };
                }
            })
            .WithHttpTransport()
            .WithToolsFromAssembly();
  1. Don't configure other filters
  2. Make call of a tool that throws an exception
  3. Observe result object containing CallToolResult object with
    IsError = true,
    Text = "An error occurred invoking '<redacted>'."
    and no exception thrown to catch

Expected behavior
I should be able to handle exception and build my custom CallToolResult

Logs
n/a

Additional context
Version: 0.4.0-preview.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions