Skip to content

Guidance regarding validation with MCP Tools #696

@KenBrannigan

Description

@KenBrannigan

I am hoping to get some guidance regarding validation when it comes to MCP Tools. Below is just some sample code to help with my questions:

[McpServerToolType]
internal sealed class Update
{

    private static readonly List<ItemRecord> s_items = new(
        [
            new(1, "Item 1", true, "Label 1"),
            new(2, "Item 2", false, "Label 2"),
            new(3, "Item 3", true)
        ]);

    [McpServerTool(Name = "UpdateItem", Title = "Update Item", UseStructuredContent = true), Description("Will update a item.")]
    public static async Task<ItemRecord> ProcessAsync(UpdateRequest request)
    {
        ItemRecord item = s_items
            .Single(i => i.Id == request.Id);

        return await Task.FromResult(
            new ItemRecord(
                item.Id,
                request.Description,
                request.Active,
                item.Label))
            .ConfigureAwait(false);
    }
}

internal sealed record UpdateRequest(int Id, string Description, bool Active);

internal sealed record ItemRecord(int Id, string Description, bool Active, string? Label = null);

Questions:

  • How do I validate UpdateRequest? In a REST API using something like FastEndpoints, I would use FluentValidation to make sure that the request was valid (i.e. required fields, string lengths, etc.) I know I can use Data Annotations on that request object so that the output schema shows those requirements but it doesn't seem to enforce those requirements when the client calls into the server.
  • If I need to manually validate my request, how do I signal back to the client that failure? My current return type is an ItemRecord so how do I handle returning that as a success call and returning a validation failure when validation fails? I assume that whatever the answer is for this, it would apply to when I would go look up an ItemRecord by the Id from he request, and it cannot be found.
  • Is there a way to use FluentValidation instead of DataAnnotations to determine the validation rules in the input and output schema when a tools list is requested?

I appreciate any help that can be provided.

Metadata

Metadata

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