Skip to content

Commit 81941d1

Browse files
committed
Add and update tests
1 parent dba4ef8 commit 81941d1

File tree

17 files changed

+564
-2
lines changed

17 files changed

+564
-2
lines changed

test/ProjectTemplates/Microsoft.Extensions.AI.Templates.IntegrationTests/McpServerSnapshotTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ public async Task BasicTest()
4141
await TestTemplateCoreAsync(scenarioName: "Basic");
4242
}
4343

44+
[Fact]
45+
public async Task SelfContainedFalse()
46+
{
47+
await TestTemplateCoreAsync(scenarioName: "SelfContainedFalse", templateArgs: ["--self-contained", bool.FalseString]);
48+
}
49+
50+
[Fact]
51+
public async Task AotTrue()
52+
{
53+
await TestTemplateCoreAsync(scenarioName: "AotTrue", templateArgs: ["--aot", bool.TrueString]);
54+
}
55+
56+
[Fact]
57+
public async Task Net10()
58+
{
59+
await TestTemplateCoreAsync(scenarioName: "net10", templateArgs: ["--framework", "net10.0"]);
60+
}
61+
4462
private async Task TestTemplateCoreAsync(string scenarioName, IEnumerable<string>? templateArgs = null)
4563
{
4664
string workingDir = TestUtils.CreateTemporaryFolder();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://modelcontextprotocol.io/schemas/draft/2025-07-09/server.json",
3+
"description": "<your description here>",
4+
"name": "io.github.<your GitHub username here>/<your repo name>",
5+
"packages": [
6+
{
7+
"registry_name": "nuget",
8+
"name": "<your package ID here>",
9+
"version": "0.1.0-beta",
10+
"package_arguments": [],
11+
"environment_variables": []
12+
}
13+
],
14+
"repository": {
15+
"url": "https://github.com/<your GitHub username here>/<your repo name>",
16+
"source": "github"
17+
},
18+
"version_detail": {
19+
"version": "0.1.0-beta"
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
5+
var builder = Host.CreateApplicationBuilder(args);
6+
7+
// Configure all logs to go to stderr (stdout is used for the MCP protocol messages).
8+
builder.Logging.AddConsole(o => o.LogToStandardErrorThreshold = LogLevel.Trace);
9+
10+
// Add the MCP services: the transport to use (stdio) and the tools to register.
11+
builder.Services
12+
.AddMcpServer()
13+
.WithStdioServerTransport()
14+
.WithTools<RandomNumberTools>();
15+
16+
await builder.Build().RunAsync();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# MCP Server
2+
3+
This README was created using the C# MCP server project template. It demonstrates how you can easily create an MCP server using C# and publish it as a NuGet package.
4+
5+
See [aka.ms/nuget/mcp/guide](https://aka.ms/nuget/mcp/guide) for the full guide.
6+
7+
Please note that this template is currently in an early preview stage. If you have feedback, please take a [brief survey](http://aka.ms/dotnet-mcp-template-survey).
8+
9+
## Checklist before publishing to NuGet.org
10+
11+
- Test the MCP server locally using the steps below.
12+
- Update the package metadata in the .csproj file, in particular the `<PackageId>`.
13+
- Update `.mcp/server.json` to declare your MCP server's inputs.
14+
- See [configuring inputs](https://aka.ms/nuget/mcp/guide/configuring-inputs) for more details.
15+
- Pack the project using `dotnet pack`.
16+
17+
The `bin/Release` directory will contain the package file (.nupkg), which can be [published to NuGet.org](https://learn.microsoft.com/nuget/nuget-org/publish-a-package).
18+
19+
## Developing locally
20+
21+
To test this MCP server from source code (locally) without using a built MCP server package, you can configure your IDE to run the project directly using `dotnet run`.
22+
23+
```json
24+
{
25+
"servers": {
26+
"mcpserver": {
27+
"type": "stdio",
28+
"command": "dotnet",
29+
"args": [
30+
"run",
31+
"--project",
32+
"<PATH TO PROJECT DIRECTORY>"
33+
]
34+
}
35+
}
36+
}
37+
```
38+
39+
## Testing the MCP Server
40+
41+
Once configured, you can ask Copilot Chat for a random number, for example, `Give me 3 random numbers`. It should prompt you to use the `get_random_number` tool on the `mcpserver` MCP server and show you the results.
42+
43+
## Publishing to NuGet.org
44+
45+
1. Run `dotnet pack -c Release` to create the NuGet package
46+
2. Publish to NuGet.org with `dotnet nuget push bin/Release/*.nupkg --api-key <your-api-key> --source https://api.nuget.org/v3/index.json`
47+
48+
## Using the MCP Server from NuGet.org
49+
50+
Once the MCP server package is published to NuGet.org, you can configure it in your preferred IDE. Both VS Code and Visual Studio use the `dnx` command to download and install the MCP server package from NuGet.org.
51+
52+
- **VS Code**: Create a `<WORKSPACE DIRECTORY>/.vscode/mcp.json` file
53+
- **Visual Studio**: Create a `<SOLUTION DIRECTORY>\.mcp.json` file
54+
55+
For both VS Code and Visual Studio, the configuration file uses the following server definition:
56+
57+
```json
58+
{
59+
"servers": {
60+
"mcpserver": {
61+
"type": "stdio",
62+
"command": "dnx",
63+
"args": [
64+
"<your package ID here>",
65+
"--version",
66+
"<your package version here>",
67+
"--yes"
68+
]
69+
}
70+
}
71+
}
72+
```
73+
74+
## More information
75+
76+
.NET MCP servers use the [ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol) C# SDK. For more information about MCP:
77+
78+
- [Official Documentation](https://modelcontextprotocol.io/)
79+
- [Protocol Specification](https://spec.modelcontextprotocol.io/)
80+
- [GitHub Organization](https://github.com/modelcontextprotocol)
81+
82+
Refer to the VS Code or Visual Studio documentation for more information on configuring and using MCP servers:
83+
84+
- [Use MCP servers in VS Code (Preview)](https://code.visualstudio.com/docs/copilot/chat/mcp-servers)
85+
- [Use MCP servers in Visual Studio (Preview)](https://learn.microsoft.com/visualstudio/ide/mcp-servers)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.ComponentModel;
2+
using ModelContextProtocol.Server;
3+
4+
/// <summary>
5+
/// Sample MCP tools for demonstration purposes.
6+
/// These tools can be invoked by MCP clients to perform various operations.
7+
/// </summary>
8+
internal class RandomNumberTools
9+
{
10+
[McpServerTool]
11+
[Description("Generates a random number between the specified minimum and maximum values.")]
12+
public int GetRandomNumber(
13+
[Description("Minimum value (inclusive)")] int min = 0,
14+
[Description("Maximum value (exclusive)")] int max = 100)
15+
{
16+
return Random.Shared.Next(min, max);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<RuntimeIdentifiers>win-x64;win-arm64;osx-arm64;linux-x64;linux-arm64</RuntimeIdentifiers>
6+
<OutputType>Exe</OutputType>
7+
<Nullable>enable</Nullable>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
10+
<!-- Set up the NuGet package to be an MCP server -->
11+
<PackAsTool>true</PackAsTool>
12+
<PackageType>McpServer</PackageType>
13+
14+
<!-- Set up the MCP server to be a self-contained application that does not rely on a shared framework -->
15+
<SelfContained>true</SelfContained>
16+
<PublishSelfContained>true</PublishSelfContained>
17+
18+
<!-- Set up the MCP server to be a single file executable -->
19+
<PublishSingleFile>true</PublishSingleFile>
20+
21+
<!-- Configure the MCP server to be a native AOT application with invariant globalization -->
22+
<PublishAot>true</PublishAot>
23+
<InvariantGlobalization>true</InvariantGlobalization>
24+
25+
<!-- Set recommended package metadata -->
26+
<PackageReadmeFile>README.md</PackageReadmeFile>
27+
<PackageId>SampleMcpServer</PackageId>
28+
<PackageVersion>0.1.0-beta</PackageVersion>
29+
<PackageTags>AI; MCP; server; stdio</PackageTags>
30+
<Description>An MCP server using the MCP C# SDK.</Description>
31+
</PropertyGroup>
32+
33+
<!-- Include additional files for browsing the MCP server. -->
34+
<ItemGroup>
35+
<None Include=".mcp\server.json" Pack="true" PackagePath="/.mcp/" />
36+
<None Include="README.md" Pack="true" PackagePath="/" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
41+
<PackageReference Include="ModelContextProtocol" Version="0.3.0-preview.2" />
42+
</ItemGroup>
43+
44+
</Project>

test/ProjectTemplates/Microsoft.Extensions.AI.Templates.IntegrationTests/Snapshots/mcpserver.Basic.verified/mcpserver/mcpserver.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
5-
<RollForward>Major</RollForward>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<RuntimeIdentifiers>win-x64;win-arm64;osx-arm64;linux-x64;linux-arm64</RuntimeIdentifiers>
66
<OutputType>Exe</OutputType>
77
<Nullable>enable</Nullable>
88
<ImplicitUsings>enable</ImplicitUsings>
@@ -11,6 +11,13 @@
1111
<PackAsTool>true</PackAsTool>
1212
<PackageType>McpServer</PackageType>
1313

14+
<!-- Set up the MCP server to be a self-contained application that does not rely on a shared framework -->
15+
<SelfContained>true</SelfContained>
16+
<PublishSelfContained>true</PublishSelfContained>
17+
18+
<!-- Set up the MCP server to be a single file executable -->
19+
<PublishSingleFile>true</PublishSingleFile>
20+
1421
<!-- Set recommended package metadata -->
1522
<PackageReadmeFile>README.md</PackageReadmeFile>
1623
<PackageId>SampleMcpServer</PackageId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://modelcontextprotocol.io/schemas/draft/2025-07-09/server.json",
3+
"description": "<your description here>",
4+
"name": "io.github.<your GitHub username here>/<your repo name>",
5+
"packages": [
6+
{
7+
"registry_name": "nuget",
8+
"name": "<your package ID here>",
9+
"version": "0.1.0-beta",
10+
"package_arguments": [],
11+
"environment_variables": []
12+
}
13+
],
14+
"repository": {
15+
"url": "https://github.com/<your GitHub username here>/<your repo name>",
16+
"source": "github"
17+
},
18+
"version_detail": {
19+
"version": "0.1.0-beta"
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
5+
var builder = Host.CreateApplicationBuilder(args);
6+
7+
// Configure all logs to go to stderr (stdout is used for the MCP protocol messages).
8+
builder.Logging.AddConsole(o => o.LogToStandardErrorThreshold = LogLevel.Trace);
9+
10+
// Add the MCP services: the transport to use (stdio) and the tools to register.
11+
builder.Services
12+
.AddMcpServer()
13+
.WithStdioServerTransport()
14+
.WithTools<RandomNumberTools>();
15+
16+
await builder.Build().RunAsync();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# MCP Server
2+
3+
This README was created using the C# MCP server project template. It demonstrates how you can easily create an MCP server using C# and publish it as a NuGet package.
4+
5+
See [aka.ms/nuget/mcp/guide](https://aka.ms/nuget/mcp/guide) for the full guide.
6+
7+
Please note that this template is currently in an early preview stage. If you have feedback, please take a [brief survey](http://aka.ms/dotnet-mcp-template-survey).
8+
9+
## Checklist before publishing to NuGet.org
10+
11+
- Test the MCP server locally using the steps below.
12+
- Update the package metadata in the .csproj file, in particular the `<PackageId>`.
13+
- Update `.mcp/server.json` to declare your MCP server's inputs.
14+
- See [configuring inputs](https://aka.ms/nuget/mcp/guide/configuring-inputs) for more details.
15+
- Pack the project using `dotnet pack`.
16+
17+
The `bin/Release` directory will contain the package file (.nupkg), which can be [published to NuGet.org](https://learn.microsoft.com/nuget/nuget-org/publish-a-package).
18+
19+
## Developing locally
20+
21+
To test this MCP server from source code (locally) without using a built MCP server package, you can configure your IDE to run the project directly using `dotnet run`.
22+
23+
```json
24+
{
25+
"servers": {
26+
"mcpserver": {
27+
"type": "stdio",
28+
"command": "dotnet",
29+
"args": [
30+
"run",
31+
"--project",
32+
"<PATH TO PROJECT DIRECTORY>"
33+
]
34+
}
35+
}
36+
}
37+
```
38+
39+
## Testing the MCP Server
40+
41+
Once configured, you can ask Copilot Chat for a random number, for example, `Give me 3 random numbers`. It should prompt you to use the `get_random_number` tool on the `mcpserver` MCP server and show you the results.
42+
43+
## Publishing to NuGet.org
44+
45+
1. Run `dotnet pack -c Release` to create the NuGet package
46+
2. Publish to NuGet.org with `dotnet nuget push bin/Release/*.nupkg --api-key <your-api-key> --source https://api.nuget.org/v3/index.json`
47+
48+
## Using the MCP Server from NuGet.org
49+
50+
Once the MCP server package is published to NuGet.org, you can configure it in your preferred IDE. Both VS Code and Visual Studio use the `dnx` command to download and install the MCP server package from NuGet.org.
51+
52+
- **VS Code**: Create a `<WORKSPACE DIRECTORY>/.vscode/mcp.json` file
53+
- **Visual Studio**: Create a `<SOLUTION DIRECTORY>\.mcp.json` file
54+
55+
For both VS Code and Visual Studio, the configuration file uses the following server definition:
56+
57+
```json
58+
{
59+
"servers": {
60+
"mcpserver": {
61+
"type": "stdio",
62+
"command": "dnx",
63+
"args": [
64+
"<your package ID here>",
65+
"--version",
66+
"<your package version here>",
67+
"--yes"
68+
]
69+
}
70+
}
71+
}
72+
```
73+
74+
## More information
75+
76+
.NET MCP servers use the [ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol) C# SDK. For more information about MCP:
77+
78+
- [Official Documentation](https://modelcontextprotocol.io/)
79+
- [Protocol Specification](https://spec.modelcontextprotocol.io/)
80+
- [GitHub Organization](https://github.com/modelcontextprotocol)
81+
82+
Refer to the VS Code or Visual Studio documentation for more information on configuring and using MCP servers:
83+
84+
- [Use MCP servers in VS Code (Preview)](https://code.visualstudio.com/docs/copilot/chat/mcp-servers)
85+
- [Use MCP servers in Visual Studio (Preview)](https://learn.microsoft.com/visualstudio/ide/mcp-servers)

0 commit comments

Comments
 (0)