Skip to content

Commit 9c1538f

Browse files
Copilotstephentoubeiriktsarpalis
authored
Add AOT compatibility publish test (#1246)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: Stephen Toub <stoub@microsoft.com> Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
1 parent bfd01b9 commit 9c1538f

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

.github/workflows/ci-build-test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ jobs:
6363
- name: 🧪 Test
6464
run: make test CONFIGURATION=${{ matrix.configuration }}
6565

66+
- name: 🧪 AOT Compatibility
67+
if: matrix.configuration == 'Release'
68+
run: make test-aot CONFIGURATION=${{ matrix.configuration }}
69+
6670
- name: 📦 Pack
6771
if: matrix.configuration == 'Release'
6872
run: make pack CONFIGURATION=${{ matrix.configuration }}

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ test: build
3030
-- \
3131
RunConfiguration.CollectSourceInformation=true
3232

33+
test-aot:
34+
dotnet publish tests/ModelContextProtocol.AotCompatibility.TestApp/ModelContextProtocol.AotCompatibility.TestApp.csproj --configuration $(CONFIGURATION) -o $(ARTIFACT_PATH)/aot-publish
35+
$(ARTIFACT_PATH)/aot-publish/ModelContextProtocol.AotCompatibility.TestApp
36+
3337
pack: restore
3438
dotnet pack --no-restore --configuration $(CONFIGURATION)
3539

ModelContextProtocol.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
</Folder>
7171
<Folder Name="/tests/">
7272
<Project Path="tests/ModelContextProtocol.Analyzers.Tests/ModelContextProtocol.Analyzers.Tests.csproj" />
73+
<Project Path="tests/ModelContextProtocol.AotCompatibility.TestApp/ModelContextProtocol.AotCompatibility.TestApp.csproj" />
7374
<Project Path="tests/ModelContextProtocol.AspNetCore.Tests/ModelContextProtocol.AspNetCore.Tests.csproj" />
7475
<Project Path="tests/ModelContextProtocol.ConformanceClient/ModelContextProtocol.ConformanceClient.csproj" />
7576
<Project Path="tests/ModelContextProtocol.ConformanceServer/ModelContextProtocol.ConformanceServer.csproj" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<!-- Ensure TargetFrameworks is not inherited -->
7+
<TargetFrameworks></TargetFrameworks>
8+
<PublishAot>true</PublishAot>
9+
<TrimmerSingleWarn>false</TrimmerSingleWarn>
10+
<!-- Suppress the experimental tasks warning -->
11+
<NoWarn>$(NoWarn);MCPEXP001</NoWarn>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\src\ModelContextProtocol.Core\ModelContextProtocol.Core.csproj" />
16+
<ProjectReference Include="..\..\src\ModelContextProtocol\ModelContextProtocol.csproj" />
17+
<ProjectReference Include="..\..\src\ModelContextProtocol.AspNetCore\ModelContextProtocol.AspNetCore.csproj" />
18+
19+
<TrimmerRootAssembly Include="ModelContextProtocol.Core" />
20+
<TrimmerRootAssembly Include="ModelContextProtocol" />
21+
<TrimmerRootAssembly Include="ModelContextProtocol.AspNetCore" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using ModelContextProtocol.Client;
2+
using ModelContextProtocol.Protocol;
3+
using ModelContextProtocol.Server;
4+
using System.IO.Pipelines;
5+
6+
Pipe clientToServerPipe = new(), serverToClientPipe = new();
7+
8+
// Create a server using a stream-based transport over an in-memory pipe.
9+
await using McpServer server = McpServer.Create(
10+
new StreamServerTransport(clientToServerPipe.Reader.AsStream(), serverToClientPipe.Writer.AsStream()),
11+
new McpServerOptions()
12+
{
13+
ToolCollection = [McpServerTool.Create((string arg) => $"Echo: {arg}", new() { Name = "Echo" })]
14+
});
15+
_ = server.RunAsync();
16+
17+
// Connect a client using a stream-based transport over the same in-memory pipe.
18+
await using McpClient client = await McpClient.CreateAsync(
19+
new StreamClientTransport(clientToServerPipe.Writer.AsStream(), serverToClientPipe.Reader.AsStream()));
20+
21+
// List all tools.
22+
var tools = await client.ListToolsAsync();
23+
if (tools.Count == 0)
24+
{
25+
throw new Exception("Expected at least one tool.");
26+
}
27+
28+
// Invoke a tool.
29+
var echo = tools.First(t => t.Name == "Echo");
30+
var result = await echo.InvokeAsync(new() { ["arg"] = "Hello World" });
31+
if (result is null || !result.ToString()!.Contains("Echo: Hello World"))
32+
{
33+
throw new Exception($"Unexpected result: {result}");
34+
}
35+
36+
Console.WriteLine("Success!");

0 commit comments

Comments
 (0)