11// Copyright (c) .NET Foundation and Contributors. All rights reserved.
22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
4+ using System . Text . Json . Nodes ;
5+ using Microsoft . Build . Construction ;
6+ using Microsoft . Build . Framework ;
47using Nerdbank . GitVersioning ;
58using Xunit ;
69
@@ -15,6 +18,84 @@ public BuildIntegrationManagedTests(ITestOutputHelper logger)
1518 {
1619 }
1720
21+ /// <summary>
22+ /// Verifies that MCP server.json files get version stamping when PackageType=McpServer.
23+ /// </summary>
24+ [ Fact ]
25+ public async Task McpServerJson_VersionStamping ( )
26+ {
27+ // Create a sample server.json file based on the real MCP server template
28+ string serverJsonContent = /* lang=c#-test */ """
29+ {
30+ "$schema": "https://modelcontextprotocol.io/schemas/draft/2025-07-09/server.json",
31+ "description": "Test .NET MCP Server",
32+ "name": "io.github.test/testmcpserver",
33+ "version": "0.0.0-placeholder",
34+ "packages": [
35+ {
36+ "registry_type": "nuget",
37+ "identifier": "Test.McpServer",
38+ "version": "0.0.0-placeholder",
39+ "transport": {
40+ "type": "stdio"
41+ },
42+ "package_arguments": [],
43+ "environment_variables": []
44+ }
45+ ],
46+ "repository": {
47+ "url": "https://github.com/test/testmcpserver",
48+ "source": "github"
49+ }
50+ }
51+ """ ;
52+
53+ string serverJsonPath = Path . Combine ( this . projectDirectory , "server.json" ) ;
54+ File . WriteAllText ( serverJsonPath , serverJsonContent ) ;
55+
56+ // Set PackageType to McpServer
57+ ProjectPropertyGroupElement propertyGroup = this . testProject . CreatePropertyGroupElement ( ) ;
58+ this . testProject . AppendChild ( propertyGroup ) ;
59+ propertyGroup . AddProperty ( "PackageType" , "McpServer" ) ;
60+
61+ this . WriteVersionFile ( ) ;
62+ BuildResults result = await this . BuildAsync ( "NBGV_StampMcpServerJson" , logVerbosity : LoggerVerbosity . Detailed ) ;
63+
64+ // Verify the build succeeded
65+ Assert . Empty ( result . LoggedEvents . OfType < BuildErrorEventArgs > ( ) ) ;
66+
67+ // Verify the stamped server.json was created
68+ string stampedServerJsonPath = Path . Combine ( this . projectDirectory , result . BuildResult . ProjectStateAfterBuild . GetPropertyValue ( "IntermediateOutputPath" ) , "server.json" ) ;
69+ Assert . True ( File . Exists ( stampedServerJsonPath ) , $ "Expected stamped server.json at: { stampedServerJsonPath } ") ;
70+
71+ // Verify the version was correctly stamped
72+ string stampedContent = File . ReadAllText ( stampedServerJsonPath ) ;
73+ var stampedJson = JsonNode . Parse ( stampedContent ) as JsonObject ;
74+ Assert . NotNull ( stampedJson ) ;
75+
76+ string expectedVersion = result . BuildResult . ProjectStateAfterBuild . GetPropertyValue ( "Version" ) ;
77+
78+ // Verify root version was stamped
79+ Assert . Equal ( expectedVersion , stampedJson [ "version" ] ? . ToString ( ) ) ;
80+
81+ // Verify package version was also stamped
82+ JsonArray packages = stampedJson [ "packages" ] ? . AsArray ( ) ;
83+ Assert . NotNull ( packages ) ;
84+ Assert . Single ( packages ) ;
85+
86+ JsonObject package = packages [ 0 ] ? . AsObject ( ) ;
87+ Assert . NotNull ( package ) ;
88+ Assert . Equal ( expectedVersion , package [ "version" ] ? . ToString ( ) ) ;
89+
90+ // Verify other properties were preserved
91+ Assert . Equal ( "io.github.test/testmcpserver" , stampedJson [ "name" ] ? . ToString ( ) ) ;
92+ Assert . Equal ( "Test .NET MCP Server" , stampedJson [ "description" ] ? . ToString ( ) ) ;
93+ Assert . Equal ( "Test.McpServer" , package [ "identifier" ] ? . ToString ( ) ) ;
94+
95+ // Verify that no placeholder remain in the entire JSON
96+ Assert . DoesNotContain ( "0.0.0-placeholder" , stampedContent ) ;
97+ }
98+
1899 protected override GitContext CreateGitContext ( string path , string committish = null )
19100 => GitContext . Create ( path , committish , GitContext . Engine . ReadOnly ) ;
20101
0 commit comments