Skip to content

WithListToolsHandler not overriding default functionality fully #707

@dowdoliver

Description

@dowdoliver

Describe the bug
When defining a custom tools .WithListToolsHandler(SOME_FUNCTION), it doesn't fully override the tool handler

To Reproduce

builder.Services
    .AddMcpServer()
    .WithListToolsHandler((context, cancellationToken) =>
    {
        var result = new ListToolsResult();
        return ValueTask.FromResult(result);
    })
    .WithStdioServerTransport() 
    .WithTools<MyTool>()

When calling the tools/list method MyTool will still be listed

Expected behavior
Just the result from the WithListToolsHandler (empty list) should be returned

Additional context
From McpServer.cs

var originalListToolsHandler = listToolsHandler;
listToolsHandler = async (request, cancellationToken) =>
{
    ListToolsResult result = originalListToolsHandler is not null ?
        await originalListToolsHandler(request, cancellationToken).ConfigureAwait(false) :
        new();

    if (request.Params?.Cursor is null)
    {
        foreach (var t in tools)
        {
            result.Tools.Add(t.ProtocolTool);
        }
    }

    return result;
};

It feels like there should be a default list tool handler that just returns the list of tools, and it gets replaced by the listToolsHandler specified in options if it is not null. Let me know if I'm not understanding how it is supposed to be working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions