From 7f7306fc41dfff339b03bd162872a28f64f0356c Mon Sep 17 00:00:00 2001 From: Stephen Walsh Date: Wed, 28 Oct 2026 14:00:57 +0000 Subject: [PATCH 1/3] Run the MCP Inspector in Docker Compose, pointed at the server docker compose up now also starts the official MCP Inspector on localhost:6274, already configured for the server over Streamable HTTP, so the tools, their descriptions and input schemas can be browsed and run without installing anything. --- README.md | 2 +- docker-compose.yml | 9 +++++++++ docs/RUN.md | 5 +++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f4fc306..8798de2 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ dotnet new cleanmodelcontextprotocol --name {YOUR_SOLUTION_NAMESPACE} --author " docker compose up -d ``` -Then open the MCP endpoint (connect with an MCP client such as the [MCP Inspector](https://github.com/modelcontextprotocol/inspector)) at . See [docs/RUN.md](docs/RUN.md) for every way to run it. +Then open the [MCP Inspector](https://github.com/modelcontextprotocol/inspector) at . It's already pointed at the MCP endpoint (), so connect and open **Tools** to browse and run them. See [docs/RUN.md](docs/RUN.md) for every way to run it. The `Dockerfile` has stages for testing, publishing and running, so you can build just the part you need. You can also stamp a version into the assembly. diff --git a/docker-compose.yml b/docker-compose.yml index 14946d5..ca1bb02 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,6 +14,15 @@ services: - cleanmodelcontextprotocol_network depends_on: - seq + inspector: + image: ghcr.io/modelcontextprotocol/inspector:2.12.0 + command: ["--web", "--transport", "http", "--server-url", "http://api:8080"] + ports: + - "127.0.0.1:6274:6274" + networks: + - cleanmodelcontextprotocol_network + depends_on: + - api seq: image: datalust/seq:latest ports: diff --git a/docs/RUN.md b/docs/RUN.md index bece1f6..69edcad 100644 --- a/docs/RUN.md +++ b/docs/RUN.md @@ -12,13 +12,14 @@ dotnet run --project ./src/Presentation ## Docker Compose -`docker-compose.yml` starts the app and a Seq container for logs. +`docker-compose.yml` starts the app, the [MCP Inspector](https://github.com/modelcontextprotocol/inspector) already pointed at it, and a Seq container for logs. ``` bash docker compose up -d --build ``` -- App: (MCP endpoint (connect with an MCP client such as the [MCP Inspector](https://github.com/modelcontextprotocol/inspector))) +- App: (MCP endpoint, Streamable HTTP) +- MCP Inspector: . Flip the switch on the server card to connect, then open **Tools** to see every tool with its description and input schema and run it. - Seq: To shut the containers down: From 0231b12b115efe2474cbb051fa14257935e896b1 Mon Sep 17 00:00:00 2001 From: Stephen Walsh Date: Wed, 28 Oct 2026 15:38:30 +0000 Subject: [PATCH 2/3] feat: give every MCP tool agent-ready metadata Rename the tools by resource (authors_list, movies_get, reviews_create and so on), add titles and the read-only, destructive, idempotent and open-world hints, and rewrite the descriptions and parameter names for agents. The server instructions now describe the domain instead of output formatting, the server info has a proper title and an absolute icon URL, and not-found and validation errors tell the agent how to recover. The server now runs stateless at /mcp, and a tools/list test pins the names, titles and hints. --- README.md | 7 +- docker-compose.yml | 2 +- docs/RUN.md | 2 +- src/Presentation/Errors/ToolErrorFilter.cs | 17 +++-- .../WebApplicationBuilderExtensions.cs | 24 ++++--- .../Extensions/WebApplicationExtensions.cs | 2 +- .../Properties/launchSettings.json | 3 +- src/Presentation/Tools/AuthorTools.cs | 17 +++-- src/Presentation/Tools/MovieTools.cs | 17 +++-- src/Presentation/Tools/ReviewTools.cs | 53 +++++++------- src/Presentation/Tools/VersionTools.cs | 4 +- .../Tools/AuthorToolTests.cs | 24 +++---- .../Tools/BaseToolTests.cs | 2 +- .../Tools/MovieToolTests.cs | 24 +++---- .../Tools/ReviewToolTests.cs | 54 +++++++------- .../Tools/ToolListTests.cs | 71 +++++++++++++++++++ .../Tools/VersionToolTests.cs | 2 +- .../Errors/ToolErrorFilterTests.cs | 16 ++++- .../Tools/AuthorToolTests.cs | 4 +- .../Tools/MovieToolTests.cs | 4 +- .../Tools/ReviewToolTests.cs | 4 +- 21 files changed, 230 insertions(+), 123 deletions(-) create mode 100644 tests/Presentation.Tests.Integration/Tools/ToolListTests.cs diff --git a/README.md b/README.md index 8798de2..b522130 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ dotnet new cleanmodelcontextprotocol --name {YOUR_SOLUTION_NAMESPACE} --author " docker compose up -d ``` -Then open the [MCP Inspector](https://github.com/modelcontextprotocol/inspector) at . It's already pointed at the MCP endpoint (), so connect and open **Tools** to browse and run them. See [docs/RUN.md](docs/RUN.md) for every way to run it. +Then open the [MCP Inspector](https://github.com/modelcontextprotocol/inspector) at . It's already pointed at the MCP endpoint (), so connect and open **Tools** to browse and run them. See [docs/RUN.md](docs/RUN.md) for every way to run it. The `Dockerfile` has stages for testing, publishing and running, so you can build just the part you need. You can also stamp a version into the assembly. @@ -79,11 +79,12 @@ The one deliberate break from Clean Architecture is that the Presentation projec ## Features -- MCP server using the [MCP C# SDK](https://github.com/modelcontextprotocol/csharp-sdk) +- MCP server using the [MCP C# SDK](https://github.com/modelcontextprotocol/csharp-sdk), stateless Streamable HTTP at `/mcp` +- Tools named by resource (`movies_list`, `reviews_create`) with titles, read-only, destructive and idempotent hints, and descriptions written for agents - Logging using [Serilog](https://github.com/serilog/serilog), with [Seq](https://datalust.co/seq) when running in Docker - Mediator pattern using [Mediator](https://github.com/martinothamar/Mediator) - Validation using [FluentValidation](https://github.com/FluentValidation/FluentValidation) in an Application pipeline behaviour -- Tool errors mapped from Application exceptions by a single call-tool filter +- Tool errors mapped from Application exceptions by a single call-tool filter, with messages that tell the agent how to recover - Object mapping using [Mapperly](https://mapperly.riok.app) - In-memory data using [Entity Framework Core](https://learn.microsoft.com/ef/core/providers/in-memory/) seeded with [Bogus](https://github.com/bchavez/Bogus) - Testing using [xUnit](https://xunit.net), [Shouldly](https://github.com/shouldly/shouldly) and [NSubstitute](https://github.com/nsubstitute/NSubstitute), with coverage from [Coverlet](https://github.com/coverlet-coverage/coverlet) diff --git a/docker-compose.yml b/docker-compose.yml index ca1bb02..7f5267a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,7 +16,7 @@ services: - seq inspector: image: ghcr.io/modelcontextprotocol/inspector:2.12.0 - command: ["--web", "--transport", "http", "--server-url", "http://api:8080"] + command: ["--web", "--transport", "http", "--server-url", "http://api:8080/mcp"] ports: - "127.0.0.1:6274:6274" networks: diff --git a/docs/RUN.md b/docs/RUN.md index 69edcad..9ee7f48 100644 --- a/docs/RUN.md +++ b/docs/RUN.md @@ -18,7 +18,7 @@ dotnet run --project ./src/Presentation docker compose up -d --build ``` -- App: (MCP endpoint, Streamable HTTP) +- App: (MCP endpoint, Streamable HTTP) - MCP Inspector: . Flip the switch on the server card to connect, then open **Tools** to see every tool with its description and input schema and run it. - Seq: diff --git a/src/Presentation/Errors/ToolErrorFilter.cs b/src/Presentation/Errors/ToolErrorFilter.cs index c46e932..16516cf 100644 --- a/src/Presentation/Errors/ToolErrorFilter.cs +++ b/src/Presentation/Errors/ToolErrorFilter.cs @@ -20,19 +20,19 @@ public static McpRequestHandler Filter(Mc } catch (Exception exception) when (exception is not OperationCanceledException) { - return ToResult(exception); + return ToResult(exception, request?.Params?.Name); } }; } - public static CallToolResult ToResult(Exception exception) + public static CallToolResult ToResult(Exception exception, string toolName = null) { var message = exception switch { ValidationException validationException => "Validation Failed: " + string.Join(" ", validationException.Errors .Select(e => $"{e.PropertyName}: {e.ErrorMessage}") - .Distinct()), - NotFoundException => $"Not Found: {exception.Message}", + .Distinct()) + " Correct the arguments and call the tool again.", + NotFoundException => $"Not Found: {exception.Message}{ListToolHint(toolName)}", _ => "Error: An error occurred while processing your request." }; @@ -42,4 +42,13 @@ public static CallToolResult ToResult(Exception exception) Content = [new TextContentBlock { Text = message }] }; } + + private static string ListToolHint(string toolName) + { + var separator = toolName?.IndexOf('_') ?? -1; + + return separator > 0 + ? $" Check the ids, using {toolName[..separator]}_list or the other list tools to find valid ones." + : string.Empty; + } } diff --git a/src/Presentation/Extensions/WebApplicationBuilderExtensions.cs b/src/Presentation/Extensions/WebApplicationBuilderExtensions.cs index 28c0ee0..3e4bf3d 100644 --- a/src/Presentation/Extensions/WebApplicationBuilderExtensions.cs +++ b/src/Presentation/Extensions/WebApplicationBuilderExtensions.cs @@ -61,28 +61,30 @@ public static WebApplicationBuilder ConfigureApplicationBuilder(this WebApplicat { options.ServerInfo = new() { - Name = "Clean Model Context Protocol", - Description = "A demonstration of the Clean Model Context Protocol in a .NET application", - Title = "Clean Model Context Protocol - Presentation Layer", + Name = "clean-model-context-protocol", + Title = "Clean Model Context Protocol", + Description = "Movies, the authors who review them, and their star ratings, served by a Clean Architecture .NET MCP server.", Version = assembly?.GetCustomAttribute()?.InformationalVersion ?? string.Empty, WebsiteUrl = "https://github.com/stphnwlsh/CleanModelContextProtocol", Icons = [ new() { - Source = "images/Icon.png", + Source = "https://raw.githubusercontent.com/stphnwlsh/CleanModelContextProtocol/main/icon.png", MimeType = "image/png" } ], }; - options.ServerInstructions = "You are a helpful assistant for handling requests related to movies, " + - "authors, and reviews. Use the available tools to retrieve information about movies, authors, and " + - "reviews based on the queries you receive. Always ensure that your responses are accurate and relevant " + - "to the user's request. Return the full sets of data anc convert them into tabulated markdown formats " + - "for display where sensible. Some datasets involve a number of sub objects, please calculate a count of " + - "that data for better formatting."; + options.ServerInstructions = """ + This server holds a catalogue of movies, the authors who review them, and their reviews. + A review links one author to one movie with a star rating from 1 (worst) to 5 (best). + Every movie, author and review is identified by a GUID id. Ids are never guessable, so look them up + with movies_list, authors_list or reviews_list before calling a tool that needs one. + To change a review use reviews_update, which overwrites the author, movie and stars together. + reviews_delete can't be undone, so confirm with the user before calling it. + """; }) - .WithHttpTransport() + .WithHttpTransport(options => options.Stateless = true) .WithToolsFromAssembly() .WithRequestFilters(filters => filters.AddCallToolFilter(ToolErrorFilter.Filter)); diff --git a/src/Presentation/Extensions/WebApplicationExtensions.cs b/src/Presentation/Extensions/WebApplicationExtensions.cs index 75f2544..d6c3709 100644 --- a/src/Presentation/Extensions/WebApplicationExtensions.cs +++ b/src/Presentation/Extensions/WebApplicationExtensions.cs @@ -17,7 +17,7 @@ public static WebApplication ConfigureApplication(this WebApplication app) #region Model Context Protocol - _ = app.MapMcp(); + _ = app.MapMcp("/mcp"); #endregion Model Context Protocol diff --git a/src/Presentation/Properties/launchSettings.json b/src/Presentation/Properties/launchSettings.json index 82ac895..227f1a5 100644 --- a/src/Presentation/Properties/launchSettings.json +++ b/src/Presentation/Properties/launchSettings.json @@ -3,8 +3,7 @@ "profiles": { "CleanModelContextProtocol": { "commandName": "Project", - "launchBrowser": true, - "launchUrl": "swagger", + "launchBrowser": false, "applicationUrl": "https://localhost:7032;http://localhost:5032", "environmentVariables": { "DOTNET_ENVIRONMENT": "local" diff --git a/src/Presentation/Tools/AuthorTools.cs b/src/Presentation/Tools/AuthorTools.cs index 26fd2bd..bbfa88e 100644 --- a/src/Presentation/Tools/AuthorTools.cs +++ b/src/Presentation/Tools/AuthorTools.cs @@ -9,22 +9,25 @@ namespace CleanModelContextProtocol.Presentation.Tools; [McpServerToolType] public static class AuthorTools { - [McpServerTool(Name = "GetAllAuthors", ReadOnly = true)] - [Description("Get all authors in the system")] - public static async Task GetAllAuthors(ISender sender) + [McpServerTool(Name = "authors_list", Title = "List authors", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [Description("List every author who has written reviews, with their id, first and last name, and the reviews they have written. " + + "Use this to find an author's id before calling authors_get, reviews_create or reviews_update.")] + public static async Task ListAuthors(ISender sender) { var result = await sender.Send(new Queries.GetAuthors.GetAuthorsQuery()); return JsonSerializer.Serialize(result); } - [McpServerTool(Name = "GetAuthorById", ReadOnly = true)] - [Description("Get a specific author by their unique ID")] - public static async Task GetAuthorById(ISender sender, [Description("The GUID of the author")] Guid id) + [McpServerTool(Name = "authors_get", Title = "Get author", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [Description("Get one author by id, including every review they have written and the movie each review is for.")] + public static async Task GetAuthor( + ISender sender, + [Description("The author's id (a GUID). Use authors_list to find it.")] Guid authorId) { return JsonSerializer.Serialize(await sender.Send(new Queries.GetAuthorById.GetAuthorByIdQuery { - Id = id + Id = authorId })); } } diff --git a/src/Presentation/Tools/MovieTools.cs b/src/Presentation/Tools/MovieTools.cs index 30546e7..bc84866 100644 --- a/src/Presentation/Tools/MovieTools.cs +++ b/src/Presentation/Tools/MovieTools.cs @@ -9,22 +9,25 @@ namespace CleanModelContextProtocol.Presentation.Tools; [McpServerToolType] public static class MovieTools { - [McpServerTool(Name = "GetAllMovies", ReadOnly = true)] - [Description("Get all movies in the system")] - public static async Task GetAllMovies(ISender sender) + [McpServerTool(Name = "movies_list", Title = "List movies", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [Description("List every movie with its id, title and reviews. " + + "Use this to find a movie's id before calling movies_get, reviews_create or reviews_update.")] + public static async Task ListMovies(ISender sender) { var result = await sender.Send(new Queries.GetMovies.GetMoviesQuery()); return JsonSerializer.Serialize(result); } - [McpServerTool(Name = "GetMovieById", ReadOnly = true)] - [Description("Get a specific movie by its unique ID")] - public static async Task GetMovieById(ISender sender, [Description("The GUID of the movie")] Guid id) + [McpServerTool(Name = "movies_get", Title = "Get movie", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [Description("Get one movie by id, including every review of it with the star rating and the author who wrote it.")] + public static async Task GetMovie( + ISender sender, + [Description("The movie's id (a GUID). Use movies_list to find it.")] Guid movieId) { return JsonSerializer.Serialize(await sender.Send(new Queries.GetMovieById.GetMovieByIdQuery { - Id = id + Id = movieId })); } } diff --git a/src/Presentation/Tools/ReviewTools.cs b/src/Presentation/Tools/ReviewTools.cs index 14470f4..d8d7bfc 100644 --- a/src/Presentation/Tools/ReviewTools.cs +++ b/src/Presentation/Tools/ReviewTools.cs @@ -10,33 +10,35 @@ namespace CleanModelContextProtocol.Presentation.Tools; [McpServerToolType] public static class ReviewTools { - [McpServerTool(Name = "GetAllReviews", ReadOnly = true)] - [Description("Get all reviews in the system")] - public static async Task GetAllReviews(ISender sender) + [McpServerTool(Name = "reviews_list", Title = "List reviews", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [Description("List every review with its id, star rating (1 to 5), the movie it is for and the author who wrote it.")] + public static async Task ListReviews(ISender sender) { var result = await sender.Send(new Queries.GetReviews.GetReviewsQuery()); return JsonSerializer.Serialize(result); } - [McpServerTool(Name = "GetReviewById", ReadOnly = true)] - [DisplayName("GetReviewById2")] - [Description("Get a specific review by its unique ID")] - public static async Task GetReviewById(ISender sender, [Description("The GUID of the review")] Guid id) + [McpServerTool(Name = "reviews_get", Title = "Get review", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [Description("Get one review by id, with its star rating, the movie it is for and the author who wrote it.")] + public static async Task GetReview( + ISender sender, + [Description("The review's id (a GUID). Use reviews_list to find it.")] Guid reviewId) { return JsonSerializer.Serialize(await sender.Send(new Queries.GetReviewById.GetReviewByIdQuery { - Id = id + Id = reviewId })); } - [McpServerTool(Name = "CreateReview")] - [Description("Create a new review linking an author and a movie")] + [McpServerTool(Name = "reviews_create", Title = "Create review", ReadOnly = false, Destructive = false, Idempotent = false, OpenWorld = false)] + [Description("Create a review of a movie by an author with a star rating from 1 to 5, and return the new review. " + + "Each call creates another review, so don't retry a call that succeeded.")] public static async Task CreateReview( ISender sender, - [Description("The GUID of the author writing the review")] Guid authorId, - [Description("The GUID of the movie being reviewed")] Guid movieId, - [Description("Star rating for the movie (e.g. 1-5)")] int stars) + [Description("The id of the author writing the review. Use authors_list to find it.")] Guid authorId, + [Description("The id of the movie being reviewed. Use movies_list to find it.")] Guid movieId, + [Description("The star rating, a whole number from 1 (worst) to 5 (best).")] int stars) { var result = await sender.Send(new Commands.CreateReview.CreateReviewCommand { @@ -48,18 +50,19 @@ public static async Task CreateReview( return JsonSerializer.Serialize(result); } - [McpServerTool(Name = "UpdateReview")] - [Description("Update an existing review")] + [McpServerTool(Name = "reviews_update", Title = "Update review", ReadOnly = false, Destructive = true, Idempotent = true, OpenWorld = false)] + [Description("Replace an existing review's author, movie and star rating. Every field is overwritten, " + + "so pass the current author and movie ids when you only want to change the stars.")] public static async Task UpdateReview( ISender sender, - [Description("The GUID of the review to update")] Guid id, - [Description("The GUID of the author writing the review")] Guid authorId, - [Description("The GUID of the movie being reviewed")] Guid movieId, - [Description("Star rating for the movie (e.g. 1-5)")] int stars) + [Description("The id of the review to update. Use reviews_list to find it.")] Guid reviewId, + [Description("The id of the author writing the review. Use authors_list to find it.")] Guid authorId, + [Description("The id of the movie being reviewed. Use movies_list to find it.")] Guid movieId, + [Description("The star rating, a whole number from 1 (worst) to 5 (best).")] int stars) { _ = await sender.Send(new Commands.UpdateReview.UpdateReviewCommand { - Id = id, + Id = reviewId, AuthorId = authorId, MovieId = movieId, Stars = stars @@ -68,13 +71,15 @@ public static async Task UpdateReview( return "Review updated successfully"; } - [McpServerTool(Name = "DeleteReview")] - [Description("Delete a review by its unique ID")] - public static async Task DeleteReview(ISender sender, [Description("The GUID of the review to delete")] Guid id) + [McpServerTool(Name = "reviews_delete", Title = "Delete review", ReadOnly = false, Destructive = true, Idempotent = true, OpenWorld = false)] + [Description("Permanently delete a review. This can't be undone, so confirm with the user first.")] + public static async Task DeleteReview( + ISender sender, + [Description("The id of the review to delete. Use reviews_list to find it.")] Guid reviewId) { _ = await sender.Send(new Commands.DeleteReview.DeleteReviewCommand { - Id = id + Id = reviewId }); return "Review deleted successfully"; diff --git a/src/Presentation/Tools/VersionTools.cs b/src/Presentation/Tools/VersionTools.cs index 1e86066..53baf03 100644 --- a/src/Presentation/Tools/VersionTools.cs +++ b/src/Presentation/Tools/VersionTools.cs @@ -8,8 +8,8 @@ namespace CleanModelContextProtocol.Presentation.Tools; [McpServerToolType] public static class VersionTools { - [McpServerTool(Name = "GetVersion", ReadOnly = true)] - [Description("Get the current application version details")] + [McpServerTool(Name = "server_get_version", Title = "Get server version", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [Description("Get the version of this server: the file version and the informational version, which includes the commit it was built from.")] public static async Task GetVersion(ISender sender) { var result = await sender.Send(new Queries.GetVersion.GetVersionQuery()); diff --git a/tests/Presentation.Tests.Integration/Tools/AuthorToolTests.cs b/tests/Presentation.Tests.Integration/Tools/AuthorToolTests.cs index 77c0dff..6086f2c 100644 --- a/tests/Presentation.Tests.Integration/Tools/AuthorToolTests.cs +++ b/tests/Presentation.Tests.Integration/Tools/AuthorToolTests.cs @@ -13,7 +13,7 @@ public class AuthorToolTests : BaseToolTests public async Task GetAllAuthors_ShouldReturn_Authors() { // Act - var result = await this.Client.CallToolAsync("GetAllAuthors"); + var result = await this.Client.CallToolAsync("authors_list"); var text = result.Content.OfType().First().Text; var authors = text.Deserialize>(); @@ -48,13 +48,13 @@ public async Task GetAllAuthors_ShouldReturn_Authors() public async Task GetAuthorById_ShouldReturn_Author() { // Arrange - var allResult = await this.Client.CallToolAsync("GetAllAuthors"); + var allResult = await this.Client.CallToolAsync("authors_list"); var allText = allResult.Content.OfType().First().Text; var firstAuthor = allText.Deserialize>()[0]; // Act - var result = await this.Client.CallToolAsync("GetAuthorById", - new Dictionary { ["id"] = firstAuthor.Id }); + var result = await this.Client.CallToolAsync("authors_get", + new Dictionary { ["authorId"] = firstAuthor.Id }); var text = result.Content.OfType().First().Text; var author = text.Deserialize(); @@ -83,26 +83,26 @@ public async Task GetAuthorById_ShouldReturn_Author() public async Task GetAuthorById_WithEmptyId_ShouldReturn_ValidationError() { // Act - var result = await this.Client.CallToolAsync("GetAuthorById", - new Dictionary { ["id"] = Guid.Empty }); + var result = await this.Client.CallToolAsync("authors_get", + new Dictionary { ["authorId"] = Guid.Empty }); var text = result.Content.OfType().First().Text; // Assert result.IsError.ShouldBe(true); - text.ShouldBe("Validation Failed: Id: An Author Id was not supplied."); + text.ShouldBe("Validation Failed: Id: An Author Id was not supplied. Correct the arguments and call the tool again."); } [Fact] public async Task GetAuthorById_WithUnknownId_ShouldReturn_NotFoundError() { // Act - var result = await this.Client.CallToolAsync("GetAuthorById", - new Dictionary { ["id"] = Guid.NewGuid() }); + var result = await this.Client.CallToolAsync("authors_get", + new Dictionary { ["authorId"] = Guid.NewGuid() }); var text = result.Content.OfType().First().Text; // Assert result.IsError.ShouldBe(true); - text.ShouldBe("Not Found: The Author with the supplied id was not found."); + text.ShouldBe("Not Found: The Author with the supplied id was not found. Check the ids, using authors_list or the other list tools to find valid ones."); } [Theory] @@ -113,8 +113,8 @@ public async Task GetAuthorById_WithInvalidId_ShouldReturn_Error(string input) try { // Act - var result = await this.Client.CallToolAsync("GetAuthorById", - new Dictionary { ["id"] = input }); + var result = await this.Client.CallToolAsync("authors_get", + new Dictionary { ["authorId"] = input }); // Assert result.IsError.ShouldBe(true); diff --git a/tests/Presentation.Tests.Integration/Tools/BaseToolTests.cs b/tests/Presentation.Tests.Integration/Tools/BaseToolTests.cs index 9e9633e..bcebd96 100644 --- a/tests/Presentation.Tests.Integration/Tools/BaseToolTests.cs +++ b/tests/Presentation.Tests.Integration/Tools/BaseToolTests.cs @@ -18,7 +18,7 @@ public async Task InitializeAsync() this.application = new CleanModelContextProtocolApplication(); this.Client = await McpClient.CreateAsync( new HttpClientTransport( - new HttpClientTransportOptions { Endpoint = new Uri("http://localhost") }, + new HttpClientTransportOptions { Endpoint = new Uri("http://localhost/mcp") }, this.application.CreateClient(), null, false), diff --git a/tests/Presentation.Tests.Integration/Tools/MovieToolTests.cs b/tests/Presentation.Tests.Integration/Tools/MovieToolTests.cs index 5314d61..d45bae6 100644 --- a/tests/Presentation.Tests.Integration/Tools/MovieToolTests.cs +++ b/tests/Presentation.Tests.Integration/Tools/MovieToolTests.cs @@ -13,7 +13,7 @@ public class MovieToolTests : BaseToolTests public async Task GetAllMovies_ShouldReturn_Movies() { // Act - var result = await this.Client.CallToolAsync("GetAllMovies"); + var result = await this.Client.CallToolAsync("movies_list"); var text = result.Content.OfType().First().Text; var movies = text.Deserialize>(); @@ -49,13 +49,13 @@ public async Task GetAllMovies_ShouldReturn_Movies() public async Task GetMovieById_ShouldReturn_Movie() { // Arrange - var allResult = await this.Client.CallToolAsync("GetAllMovies"); + var allResult = await this.Client.CallToolAsync("movies_list"); var allText = allResult.Content.OfType().First().Text; var firstMovie = allText.Deserialize>().First(m => m.Reviews?.Count > 0); // Act - var result = await this.Client.CallToolAsync("GetMovieById", - new Dictionary { ["id"] = firstMovie.Id }); + var result = await this.Client.CallToolAsync("movies_get", + new Dictionary { ["movieId"] = firstMovie.Id }); var text = result.Content.OfType().First().Text; var movie = text.Deserialize(); @@ -85,26 +85,26 @@ public async Task GetMovieById_ShouldReturn_Movie() public async Task GetMovieById_WithEmptyId_ShouldReturn_ValidationError() { // Act - var result = await this.Client.CallToolAsync("GetMovieById", - new Dictionary { ["id"] = Guid.Empty }); + var result = await this.Client.CallToolAsync("movies_get", + new Dictionary { ["movieId"] = Guid.Empty }); var text = result.Content.OfType().First().Text; // Assert result.IsError.ShouldBe(true); - text.ShouldBe("Validation Failed: Id: A Movie Id was not supplied."); + text.ShouldBe("Validation Failed: Id: A Movie Id was not supplied. Correct the arguments and call the tool again."); } [Fact] public async Task GetMovieById_WithUnknownId_ShouldReturn_NotFoundError() { // Act - var result = await this.Client.CallToolAsync("GetMovieById", - new Dictionary { ["id"] = Guid.NewGuid() }); + var result = await this.Client.CallToolAsync("movies_get", + new Dictionary { ["movieId"] = Guid.NewGuid() }); var text = result.Content.OfType().First().Text; // Assert result.IsError.ShouldBe(true); - text.ShouldBe("Not Found: The Movie with the supplied id was not found."); + text.ShouldBe("Not Found: The Movie with the supplied id was not found. Check the ids, using movies_list or the other list tools to find valid ones."); } [Theory] @@ -115,8 +115,8 @@ public async Task GetMovieById_WithInvalidId_ShouldReturn_Error(string input) try { // Act - var result = await this.Client.CallToolAsync("GetMovieById", - new Dictionary { ["id"] = input }); + var result = await this.Client.CallToolAsync("movies_get", + new Dictionary { ["movieId"] = input }); // Assert result.IsError.ShouldBe(true); diff --git a/tests/Presentation.Tests.Integration/Tools/ReviewToolTests.cs b/tests/Presentation.Tests.Integration/Tools/ReviewToolTests.cs index 41c984d..c8b9541 100644 --- a/tests/Presentation.Tests.Integration/Tools/ReviewToolTests.cs +++ b/tests/Presentation.Tests.Integration/Tools/ReviewToolTests.cs @@ -15,7 +15,7 @@ public class ReviewToolTests : BaseToolTests public async Task GetAllReviews_ShouldReturn_Reviews() { // Act - var result = await this.Client.CallToolAsync("GetAllReviews"); + var result = await this.Client.CallToolAsync("reviews_list"); var text = result.Content.OfType().First().Text; var reviews = text.Deserialize>(); @@ -50,13 +50,13 @@ public async Task GetAllReviews_ShouldReturn_Reviews() public async Task GetReviewById_ShouldReturn_Review() { // Arrange - var allResult = await this.Client.CallToolAsync("GetAllReviews"); + var allResult = await this.Client.CallToolAsync("reviews_list"); var allText = allResult.Content.OfType().First().Text; var firstReview = allText.Deserialize>()[0]; // Act - var result = await this.Client.CallToolAsync("GetReviewById", - new Dictionary { ["id"] = firstReview.Id }); + var result = await this.Client.CallToolAsync("reviews_get", + new Dictionary { ["reviewId"] = firstReview.Id }); var text = result.Content.OfType().First().Text; var review = text.Deserialize(); @@ -84,16 +84,16 @@ public async Task GetReviewById_ShouldReturn_Review() public async Task CreateReview_ShouldReturn_Review() { // Arrange - var authorResult = await this.Client.CallToolAsync("GetAllAuthors"); + var authorResult = await this.Client.CallToolAsync("authors_list"); var authorText = authorResult.Content.OfType().First().Text; var author = authorText.Deserialize>()[0]; - var movieResult = await this.Client.CallToolAsync("GetAllMovies"); + var movieResult = await this.Client.CallToolAsync("movies_list"); var movieText = movieResult.Content.OfType().First().Text; var movie = movieText.Deserialize>()[0]; // Act - var result = await this.Client.CallToolAsync("CreateReview", + var result = await this.Client.CallToolAsync("reviews_create", new Dictionary { ["authorId"] = author.Id, @@ -124,23 +124,23 @@ public async Task CreateReview_ShouldReturn_Review() public async Task UpdateReview_ShouldReturn_Success() { // Arrange - var authorResult = await this.Client.CallToolAsync("GetAllAuthors"); + var authorResult = await this.Client.CallToolAsync("authors_list"); var authorText = authorResult.Content.OfType().First().Text; var author = authorText.Deserialize>()[0]; - var movieResult = await this.Client.CallToolAsync("GetAllMovies"); + var movieResult = await this.Client.CallToolAsync("movies_list"); var movieText = movieResult.Content.OfType().First().Text; var movie = movieText.Deserialize>()[0]; - var reviewsResult = await this.Client.CallToolAsync("GetAllReviews"); + var reviewsResult = await this.Client.CallToolAsync("reviews_list"); var reviewsText = reviewsResult.Content.OfType().First().Text; var existingReview = reviewsText.Deserialize>()[0]; // Act - var result = await this.Client.CallToolAsync("UpdateReview", + var result = await this.Client.CallToolAsync("reviews_update", new Dictionary { - ["id"] = existingReview.Id, + ["reviewId"] = existingReview.Id, ["authorId"] = author.Id, ["movieId"] = movie.Id, ["stars"] = 5 @@ -150,8 +150,8 @@ public async Task UpdateReview_ShouldReturn_Success() // Assert text.ShouldBe("Review updated successfully"); - var validateResult = await this.Client.CallToolAsync("GetReviewById", - new Dictionary { ["id"] = existingReview.Id }); + var validateResult = await this.Client.CallToolAsync("reviews_get", + new Dictionary { ["reviewId"] = existingReview.Id }); var validateText = validateResult.Content.OfType().First().Text; var updatedReview = validateText.Deserialize(); @@ -174,20 +174,20 @@ public async Task UpdateReview_ShouldReturn_Success() public async Task DeleteReview_ShouldReturn_Success() { // Arrange - var reviewsResult = await this.Client.CallToolAsync("GetAllReviews"); + var reviewsResult = await this.Client.CallToolAsync("reviews_list"); var reviewsText = reviewsResult.Content.OfType().First().Text; var existingReview = reviewsText.Deserialize>()[0]; // Act - var result = await this.Client.CallToolAsync("DeleteReview", - new Dictionary { ["id"] = existingReview.Id }); + var result = await this.Client.CallToolAsync("reviews_delete", + new Dictionary { ["reviewId"] = existingReview.Id }); var text = result.Content.OfType().First().Text; // Assert text.ShouldBe("Review deleted successfully"); - var validateResult = await this.Client.CallToolAsync("GetReviewById", - new Dictionary { ["id"] = existingReview.Id }); + var validateResult = await this.Client.CallToolAsync("reviews_get", + new Dictionary { ["reviewId"] = existingReview.Id }); var validateText = validateResult.Content.OfType().First().Text; validateText.ShouldStartWith("Not Found:"); @@ -197,26 +197,26 @@ public async Task DeleteReview_ShouldReturn_Success() public async Task GetReviewById_WithEmptyId_ShouldReturn_ValidationError() { // Act - var result = await this.Client.CallToolAsync("GetReviewById", - new Dictionary { ["id"] = Guid.Empty }); + var result = await this.Client.CallToolAsync("reviews_get", + new Dictionary { ["reviewId"] = Guid.Empty }); var text = result.Content.OfType().First().Text; // Assert result.IsError.ShouldBe(true); - text.ShouldBe("Validation Failed: Id: A Review Id was not supplied."); + text.ShouldBe("Validation Failed: Id: A Review Id was not supplied. Correct the arguments and call the tool again."); } [Fact] public async Task GetReviewById_WithUnknownId_ShouldReturn_NotFoundError() { // Act - var result = await this.Client.CallToolAsync("GetReviewById", - new Dictionary { ["id"] = Guid.NewGuid() }); + var result = await this.Client.CallToolAsync("reviews_get", + new Dictionary { ["reviewId"] = Guid.NewGuid() }); var text = result.Content.OfType().First().Text; // Assert result.IsError.ShouldBe(true); - text.ShouldBe("Not Found: The Review with the supplied id was not found."); + text.ShouldBe("Not Found: The Review with the supplied id was not found. Check the ids, using reviews_list or the other list tools to find valid ones."); } [Theory] @@ -227,8 +227,8 @@ public async Task GetReviewById_WithInvalidId_ShouldReturn_Error(string input) try { // Act - var result = await this.Client.CallToolAsync("GetReviewById", - new Dictionary { ["id"] = input }); + var result = await this.Client.CallToolAsync("reviews_get", + new Dictionary { ["reviewId"] = input }); // Assert result.IsError.ShouldBe(true); diff --git a/tests/Presentation.Tests.Integration/Tools/ToolListTests.cs b/tests/Presentation.Tests.Integration/Tools/ToolListTests.cs new file mode 100644 index 0000000..0849c36 --- /dev/null +++ b/tests/Presentation.Tests.Integration/Tools/ToolListTests.cs @@ -0,0 +1,71 @@ +namespace CleanModelContextProtocol.Presentation.Tests.Integration.Tools; + +using Shouldly; +using Xunit; + +public class ToolListTests : BaseToolTests +{ + public static TheoryData Tools => new() + { + { "authors_list", "List authors", true, false, true }, + { "authors_get", "Get author", true, false, true }, + { "movies_list", "List movies", true, false, true }, + { "movies_get", "Get movie", true, false, true }, + { "reviews_list", "List reviews", true, false, true }, + { "reviews_get", "Get review", true, false, true }, + { "reviews_create", "Create review", false, false, false }, + { "reviews_update", "Update review", false, true, true }, + { "reviews_delete", "Delete review", false, true, true }, + { "server_get_version", "Get server version", true, false, true } + }; + + [Fact] + public async Task ListTools_ShouldReturn_EveryTool() + { + // Act + var tools = await this.Client.ListToolsAsync(); + + // Assert + tools.Select(t => t.Name).ShouldBe(Tools.Select(t => (string)t[0]), ignoreOrder: true); + } + + [Theory] + [MemberData(nameof(Tools))] + public async Task ListTools_ShouldDescribe_Tool(string name, string title, bool readOnly, bool destructive, bool idempotent) + { + // Act + var tool = (await this.Client.ListToolsAsync()).Single(t => t.Name == name).ProtocolTool; + + // Assert + tool.Title.ShouldBe(title); + tool.Description.ShouldNotBeNullOrWhiteSpace(); + + _ = tool.Annotations.ShouldNotBeNull(); + tool.Annotations.ReadOnlyHint.ShouldBe(readOnly); + tool.Annotations.OpenWorldHint.ShouldBe(false); + tool.Annotations.IdempotentHint.ShouldBe(idempotent); + + if (!readOnly) + { + tool.Annotations.DestructiveHint.ShouldBe(destructive); + } + + foreach (var property in tool.InputSchema.GetProperty("properties").EnumerateObject()) + { + property.Value.GetProperty("description").GetString().ShouldNotBeNullOrWhiteSpace(); + } + } + + [Fact] + public async Task ServerInfo_ShouldDescribe_Server() + { + // Assert + var serverInfo = this.Client.ServerInfo; + + serverInfo.Name.ShouldBe("clean-model-context-protocol"); + serverInfo.Title.ShouldBe("Clean Model Context Protocol"); + this.Client.ServerInstructions.ShouldNotBeNullOrWhiteSpace(); + + await Task.CompletedTask; + } +} diff --git a/tests/Presentation.Tests.Integration/Tools/VersionToolTests.cs b/tests/Presentation.Tests.Integration/Tools/VersionToolTests.cs index 32bd661..41b038d 100644 --- a/tests/Presentation.Tests.Integration/Tools/VersionToolTests.cs +++ b/tests/Presentation.Tests.Integration/Tools/VersionToolTests.cs @@ -10,7 +10,7 @@ public class VersionToolTests : BaseToolTests public async Task GetVersion_ShouldReturn_Version() { // Act - var result = await this.Client.CallToolAsync("GetVersion"); + var result = await this.Client.CallToolAsync("server_get_version"); var text = result.Content.OfType().First().Text; // Assert diff --git a/tests/Presentation.Tests.Unit/Errors/ToolErrorFilterTests.cs b/tests/Presentation.Tests.Unit/Errors/ToolErrorFilterTests.cs index b94e727..ed2c61f 100644 --- a/tests/Presentation.Tests.Unit/Errors/ToolErrorFilterTests.cs +++ b/tests/Presentation.Tests.Unit/Errors/ToolErrorFilterTests.cs @@ -54,7 +54,7 @@ public void ToResult_ShouldReturn_ValidationError() // Assert result.IsError.ShouldBe(true); result.Content.OfType().Single().Text - .ShouldBe("Validation Failed: AuthorId: An author id was not supplied to create the review. Stars: A star rating must be between 1 and 5."); + .ShouldBe("Validation Failed: AuthorId: An author id was not supplied to create the review. Stars: A star rating must be between 1 and 5. Correct the arguments and call the tool again."); } [Fact] @@ -68,6 +68,20 @@ public void ToResult_ShouldReturn_NotFoundError() result.Content.OfType().Single().Text.ShouldBe("Not Found: Expected Exception"); } + [Theory] + [InlineData("movies_get", "Not Found: Expected Exception Check the ids, using movies_list or the other list tools to find valid ones.")] + [InlineData("server", "Not Found: Expected Exception")] + [InlineData("_get", "Not Found: Expected Exception")] + public void ToResult_ShouldReturn_NotFoundErrorWithHint(string toolName, string expected) + { + // Act + var result = ToolErrorFilter.ToResult(new NotFoundException("Expected Exception"), toolName); + + // Assert + result.IsError.ShouldBe(true); + result.Content.OfType().Single().Text.ShouldBe(expected); + } + [Fact] public void ToResult_ShouldReturn_Error() { diff --git a/tests/Presentation.Tests.Unit/Tools/AuthorToolTests.cs b/tests/Presentation.Tests.Unit/Tools/AuthorToolTests.cs index e98855a..197dbce 100644 --- a/tests/Presentation.Tests.Unit/Tools/AuthorToolTests.cs +++ b/tests/Presentation.Tests.Unit/Tools/AuthorToolTests.cs @@ -27,7 +27,7 @@ public async Task GetAllAuthors_ShouldReturn_JsonResult() ]); // Act - var response = await AuthorTools.GetAllAuthors(sender); + var response = await AuthorTools.ListAuthors(sender); // Assert var result = JsonSerializer.Deserialize>(response, JsonOptions); @@ -50,7 +50,7 @@ public async Task GetAuthorById_ShouldReturn_JsonResult() .ReturnsForAnyArgs(new Entities.Author(Guid.Empty, "Lorem", "Ipsum")); // Act - var response = await AuthorTools.GetAuthorById(sender, Guid.Empty); + var response = await AuthorTools.GetAuthor(sender, Guid.Empty); // Assert var result = JsonSerializer.Deserialize(response, JsonOptions); diff --git a/tests/Presentation.Tests.Unit/Tools/MovieToolTests.cs b/tests/Presentation.Tests.Unit/Tools/MovieToolTests.cs index a939450..bfd5e01 100644 --- a/tests/Presentation.Tests.Unit/Tools/MovieToolTests.cs +++ b/tests/Presentation.Tests.Unit/Tools/MovieToolTests.cs @@ -27,7 +27,7 @@ public async Task GetAllMovies_ShouldReturn_JsonResult() ]); // Act - var response = await MovieTools.GetAllMovies(sender); + var response = await MovieTools.ListMovies(sender); // Assert var result = JsonSerializer.Deserialize>(response, JsonOptions); @@ -49,7 +49,7 @@ public async Task GetMovieById_ShouldReturn_JsonResult() .ReturnsForAnyArgs(new Entities.Movie(Guid.Empty, "Lorem Ipsum")); // Act - var response = await MovieTools.GetMovieById(sender, Guid.Empty); + var response = await MovieTools.GetMovie(sender, Guid.Empty); // Assert var result = JsonSerializer.Deserialize(response, JsonOptions); diff --git a/tests/Presentation.Tests.Unit/Tools/ReviewToolTests.cs b/tests/Presentation.Tests.Unit/Tools/ReviewToolTests.cs index abb1aa7..f096ed8 100644 --- a/tests/Presentation.Tests.Unit/Tools/ReviewToolTests.cs +++ b/tests/Presentation.Tests.Unit/Tools/ReviewToolTests.cs @@ -36,7 +36,7 @@ public async Task GetAllReviews_ShouldReturn_JsonResult() .ReturnsForAnyArgs([SampleReview()]); // Act - var response = await ReviewTools.GetAllReviews(sender); + var response = await ReviewTools.ListReviews(sender); // Assert var result = JsonSerializer.Deserialize>(response, JsonOptions); @@ -62,7 +62,7 @@ public async Task GetReviewById_ShouldReturn_JsonResult() .ReturnsForAnyArgs(SampleReview()); // Act - var response = await ReviewTools.GetReviewById(sender, Guid.Empty); + var response = await ReviewTools.GetReview(sender, Guid.Empty); // Assert var result = JsonSerializer.Deserialize(response, JsonOptions); From caa42fa99f8809b25e1fba5dddaece845144d82a Mon Sep 17 00:00:00 2001 From: Stephen Walsh Date: Wed, 28 Oct 2026 15:56:49 +0000 Subject: [PATCH 3/3] fix: guard the tool name before building the not-found hint --- src/Presentation/Errors/ToolErrorFilter.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Presentation/Errors/ToolErrorFilter.cs b/src/Presentation/Errors/ToolErrorFilter.cs index 16516cf..de302f8 100644 --- a/src/Presentation/Errors/ToolErrorFilter.cs +++ b/src/Presentation/Errors/ToolErrorFilter.cs @@ -45,7 +45,12 @@ public static CallToolResult ToResult(Exception exception, string toolName = nul private static string ListToolHint(string toolName) { - var separator = toolName?.IndexOf('_') ?? -1; + if (toolName is null) + { + return string.Empty; + } + + var separator = toolName.IndexOf('_'); return separator > 0 ? $" Check the ids, using {toolName[..separator]}_list or the other list tools to find valid ones."