-
Notifications
You must be signed in to change notification settings - Fork 819
Add round-trip integration tests for MCP Apps _meta.ui serialization #1698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jeffhandley
merged 4 commits into
modelcontextprotocol:main
from
yayayouyou:test/mcp-apps-meta-ui-roundtrip
Jul 26, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d3acebb
Add round-trip integration tests for MCP Apps _meta.ui serialization
yayayouyou 99abe99
Assert absence of _meta.ui keys instead of null value
yayayouyou 5929179
Merge branch 'main' into test/mcp-apps-meta-ui-roundtrip
jeffhandley 4c0bc43
Merge branch 'main' into test/mcp-apps-meta-ui-roundtrip
jeffhandley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
tests/ModelContextProtocol.Tests/Server/McpAppsIntegrationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| #pragma warning disable MCPEXP003 | ||
|
|
||
| using Microsoft.Extensions.DependencyInjection; | ||
| using ModelContextProtocol.Client; | ||
| using ModelContextProtocol.Extensions.Apps; | ||
| using ModelContextProtocol.Server; | ||
| using System.ComponentModel; | ||
| using System.Text.Json.Nodes; | ||
|
|
||
| namespace ModelContextProtocol.Tests.Server; | ||
|
|
||
| /// <summary> | ||
| /// Round-trip integration tests for the MCP Apps <c>_meta.ui</c> metadata: tools registered | ||
| /// with <see cref="McpAppUiAttribute"/> and processed by <c>WithMcpApps()</c> are listed through | ||
| /// <see cref="McpClient.ListToolsAsync"/> and the structured <c>_meta.ui</c> object is verified | ||
| /// after serializing across the client-server transport. | ||
| /// </summary> | ||
| public class McpAppsIntegrationTests : ClientServerTestBase | ||
| { | ||
| public McpAppsIntegrationTests(ITestOutputHelper testOutputHelper) | ||
| : base(testOutputHelper) | ||
| { | ||
| } | ||
|
|
||
| protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder) | ||
| { | ||
| mcpServerBuilder | ||
| .WithTools<AppUiTools>() | ||
| .WithMcpApps(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ListToolsAsync_RoundTripsMetaUi_ForToolWithAppUiAttribute() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken); | ||
|
|
||
| var weatherTool = Assert.Single(tools, t => t.Name == "weather_tool"); | ||
|
|
||
| JsonObject? uiNode = weatherTool.ProtocolTool.Meta?["ui"]?.AsObject(); | ||
| Assert.NotNull(uiNode); | ||
| Assert.Equal("ui://weather/view.html", uiNode["resourceUri"]?.GetValue<string>()); | ||
|
|
||
| // Visibility was not restricted, so the attribute must not add the property. | ||
| Assert.Null(uiNode["visibility"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ListToolsAsync_RoundTripsMetaUiVisibility_ForRestrictedTool() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken); | ||
|
|
||
| var modelOnlyTool = Assert.Single(tools, t => t.Name == "model_only_tool"); | ||
|
|
||
| JsonObject? uiNode = modelOnlyTool.ProtocolTool.Meta?["ui"]?.AsObject(); | ||
| Assert.NotNull(uiNode); | ||
| Assert.Equal("ui://model-only/view.html", uiNode["resourceUri"]?.GetValue<string>()); | ||
|
|
||
| JsonArray? visibility = uiNode["visibility"]?.AsArray(); | ||
| Assert.NotNull(visibility); | ||
| JsonNode? single = Assert.Single(visibility); | ||
| Assert.Equal(McpUiToolVisibility.Model, single?.GetValue<string>()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ListToolsAsync_HasNoUiMeta_ForToolWithoutAppUiAttribute() | ||
| { | ||
| await using McpClient client = await CreateMcpClientForServer(); | ||
|
|
||
| var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken); | ||
|
|
||
| var plainTool = Assert.Single(tools, t => t.Name == "plain_tool"); | ||
|
|
||
| Assert.Null(plainTool.ProtocolTool.Meta?["ui"]); | ||
|
jeffhandley marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public sealed class AppUiTools | ||
| { | ||
| [McpServerTool(Name = "weather_tool")] | ||
| [McpAppUi(ResourceUri = "ui://weather/view.html")] | ||
| [Description("Get weather")] | ||
| public static string WeatherTool(string location) => $"Weather for {location}"; | ||
|
|
||
| [McpServerTool(Name = "model_only_tool")] | ||
| [McpAppUi(ResourceUri = "ui://model-only/view.html", Visibility = [McpUiToolVisibility.Model])] | ||
| [Description("Model only")] | ||
| public static string ModelOnlyTool(string location) => $"Model only for {location}"; | ||
|
|
||
| [McpServerTool(Name = "plain_tool")] | ||
| [Description("Plain tool without app UI")] | ||
| public static string PlainTool(string input) => input; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.