Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
<TargetFrameworks></TargetFrameworks>
<PublishAot>true</PublishAot>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<!-- Suppress the experimental tasks warning -->
<NoWarn>$(NoWarn);MCPEXP001</NoWarn>
<!-- Suppress warnings for the experimental tasks and Apps APIs exercised by this test app. -->
<NoWarn>$(NoWarn);MCPEXP001;MCPEXP003</NoWarn>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\ModelContextProtocol.Core\ModelContextProtocol.Core.csproj" />
<ProjectReference Include="..\..\src\ModelContextProtocol\ModelContextProtocol.csproj" />
<ProjectReference Include="..\..\src\ModelContextProtocol.AspNetCore\ModelContextProtocol.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\ModelContextProtocol.Extensions.Apps\ModelContextProtocol.Extensions.Apps.csproj" />

<TrimmerRootAssembly Include="ModelContextProtocol.Core" />
<TrimmerRootAssembly Include="ModelContextProtocol" />
<TrimmerRootAssembly Include="ModelContextProtocol.AspNetCore" />
<TrimmerRootAssembly Include="ModelContextProtocol.Extensions.Apps" />
</ItemGroup>

</Project>
38 changes: 27 additions & 11 deletions tests/ModelContextProtocol.AotCompatibility.TestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol.Client;
using ModelContextProtocol.Extensions.Apps;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using System.IO.Pipelines;

Pipe clientToServerPipe = new(), serverToClientPipe = new();

// Create a server using a stream-based transport over an in-memory pipe.
await using McpServer server = McpServer.Create(
new StreamServerTransport(clientToServerPipe.Reader.AsStream(), serverToClientPipe.Writer.AsStream()),
new McpServerOptions()
{
ToolCollection = [McpServerTool.Create((string arg) => $"Echo: {arg}", new() { Name = "Echo" })]
});
var services = new ServiceCollection();
services.AddMcpServer()
.WithStreamServerTransport(clientToServerPipe.Reader.AsStream(), serverToClientPipe.Writer.AsStream())
.WithTools<AotTools>()
.WithMcpApps();

await using var serviceProvider = services.BuildServiceProvider();
var server = serviceProvider.GetRequiredService<McpServer>();
_ = server.RunAsync();

// Connect a client using a stream-based transport over the same in-memory pipe.
Expand All @@ -20,17 +23,30 @@

// List all tools.
var tools = await client.ListToolsAsync();
if (tools.Count == 0)
var echo = tools.FirstOrDefault(t => t.Name == "Echo");
if (echo is null)
{
throw new Exception("Expected the Echo tool.");
}

var ui = echo.ProtocolTool.Meta?["ui"]?.AsObject();
if (ui?["resourceUri"]?.GetValue<string>() != "ui://aot/echo")
{
throw new Exception("Expected at least one tool.");
throw new Exception($"Unexpected app UI metadata: {ui}");
}

// Invoke a tool.
var echo = tools.First(t => t.Name == "Echo");
var result = await echo.InvokeAsync(new() { ["arg"] = "Hello World" });
if (result is null || !result.ToString()!.Contains("Echo: Hello World"))
{
throw new Exception($"Unexpected result: {result}");
}

Console.WriteLine("Success!");

[McpServerToolType]
internal sealed class AotTools
{
[McpServerTool(Name = "Echo")]
[McpAppUi(ResourceUri = "ui://aot/echo")]
public static string Echo(string arg) => $"Echo: {arg}";
}