Skip to content
Merged
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
@@ -1,7 +1,24 @@
{
"$schema": "https://json.schemastore.org/dotnetcli.host",
"symbolInfo": {},
"symbolInfo": {
"TargetFrameworkOverride": {
"isHidden": "true",
"longName": "target-framework-override",
"shortName": ""
},
"Framework": {
"longName": "framework"
},
"NativeAot": {
"longName": "aot",
"shortName": ""
},
"SelfContained": {
"longName": "self-contained",
"shortName": ""
}
},
"usageExamples": [
""
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@
"$schema": "https://json.schemastore.org/ide.host",
"order": 0,
"icon": "ide/icon.ico",
"symbolInfo": []
"symbolInfo": [
{
"id": "NativeAot",
"isVisible": true
},
{
"id": "SelfContained",
"isVisible": true
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,53 @@
"type": "project"
},
"symbols": {
"TargetFrameworkOverride": {
"type": "parameter",
"description": "Overrides the target framework",
"displayName": "Target framework override",
"replaces": "TargetFrameworkOverride",
"datatype": "string",
"defaultValue": ""
},
"Framework": {
"type": "parameter",
"description": "The target framework for the project.",
"displayName": "Framework",
"datatype": "choice",
"choices": [
{
"choice": "net10.0",
"description": ".NET 10"
},
{
"choice": "net9.0",
"description": ".NET 9"
},
{
"choice": "net8.0",
"description": ".NET 8"
}
],
"replaces": "net9.0",
"defaultValue": "net9.0"
},
"hostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
},
"NativeAot": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"displayName": "Enable _native AOT publish",
"description": "Whether to enable the MCP server for publishing as a native AOT application."
},
"SelfContained": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "true",
"displayName": "Enable _self-contained publish",
"description": "Whether to enable the MCP server for publishing as a self-contained application."
}
},
"primaryOutputs": [
Expand All @@ -42,5 +86,23 @@
},
"continueOnError": true
}
]
],
"SpecialCustomOperations": {
"**/*.md": {
"operations": [
{
"type": "conditional",
"configuration": {
"if": [ "#### ---#if" ],
"else": [ "#### ---#else" ],
"elseif": [ "#### ---#elseif", "#### ---#elif" ],
"endif": [ "#### ---#endif" ],
"trim": "true",
"wholeLine": "true",
"evaluator": "C++"
}
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework Condition="'$(TargetFrameworkOverride)' == ''">net9.0</TargetFramework>
<TargetFramework Condition="'$(TargetFrameworkOverride)' != ''">TargetFrameworkOverride</TargetFramework>
<!--#if (SelfContained || NativeAot)-->
<RuntimeIdentifiers>win-x64;win-arm64;osx-arm64;linux-x64;linux-arm64;linux-musl-x64</RuntimeIdentifiers>
<!--#else-->
<RollForward>Major</RollForward>
<!--#endif -->
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>

<!-- Set up the NuGet package to be an MCP server -->
<PackAsTool>true</PackAsTool>
<PackageType>McpServer</PackageType>
<!--#if (SelfContained || NativeAot)-->

<!-- Set up the MCP server to be a self-contained application that does not rely on a shared framework -->
<SelfContained>true</SelfContained>
<PublishSelfContained>true</PublishSelfContained>

<!-- Set up the MCP server to be a single file executable -->
<PublishSingleFile>true</PublishSingleFile>
<!--#endif -->
<!--#if (NativeAot)-->

<!-- Configure the MCP server to be a native AOT application with invariant globalization -->
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<!--#endif -->

<!-- Set recommended package metadata -->
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
# MCP Server

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.
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.

#### ---#if (SelfContained)
The MCP server is built as a self-contained application and does not require the .NET runtime to be installed on the target machine.
However, since it is self-contained, it must be built for each target platform separately.
By default, the template is configured to build for:
* `win-x64`
* `win-arm64`
* `osx-arm64`
* `linux-x64`
* `linux-arm64`
* `linux-musl-x64`

If your users require more platforms to be supported, update the list of runtime identifiers in the project's `<RuntimeIdentifiers />` element.
#### ---#else
The MCP server is built as a framework-dependent application and requires the .NET runtime to be installed on the target machine.
The application is configured to roll-forward to the next highest major version of the runtime if one is available on the target machine.
If an applicable .NET runtime is not available, the MCP server will not start.
Consider building the MCP server as a self-contained application if you want to avoid this dependency.
#### ---#endif

See [aka.ms/nuget/mcp/guide](https://aka.ms/nuget/mcp/guide) for the full guide.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ public async Task BasicTest()
await TestTemplateCoreAsync(scenarioName: "Basic");
}

[Fact]
public async Task SelfContainedFalse()
{
await TestTemplateCoreAsync(scenarioName: "SelfContainedFalse", templateArgs: ["--self-contained", bool.FalseString]);
}

[Fact]
public async Task AotTrue()
{
await TestTemplateCoreAsync(scenarioName: "AotTrue", templateArgs: ["--aot", bool.TrueString]);
}

[Fact]
public async Task Net10()
{
await TestTemplateCoreAsync(scenarioName: "net10", templateArgs: ["--framework", "net10.0"]);
}

private async Task TestTemplateCoreAsync(string scenarioName, IEnumerable<string>? templateArgs = null)
{
string workingDir = TestUtils.CreateTemporaryFolder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://modelcontextprotocol.io/schemas/draft/2025-07-09/server.json",
"description": "<your description here>",
"name": "io.github.<your GitHub username here>/<your repo name>",
"packages": [
{
"registry_name": "nuget",
"name": "<your package ID here>",
"version": "0.1.0-beta",
"package_arguments": [],
"environment_variables": []
}
],
"repository": {
"url": "https://github.com/<your GitHub username here>/<your repo name>",
"source": "github"
},
"version_detail": {
"version": "0.1.0-beta"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var builder = Host.CreateApplicationBuilder(args);

// Configure all logs to go to stderr (stdout is used for the MCP protocol messages).
builder.Logging.AddConsole(o => o.LogToStandardErrorThreshold = LogLevel.Trace);

// Add the MCP services: the transport to use (stdio) and the tools to register.
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithTools<RandomNumberTools>();

await builder.Build().RunAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# MCP Server

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.

The MCP server is built as a self-contained application and does not require the .NET runtime to be installed on the target machine.
However, since it is self-contained, it must be built for each target platform separately.
By default, the template is configured to build for:
* `win-x64`
* `win-arm64`
* `osx-arm64`
* `linux-x64`
* `linux-arm64`
* `linux-musl-x64`

If your users require more platforms to be supported, update the list of runtime identifiers in the project's `<RuntimeIdentifiers />` element.

See [aka.ms/nuget/mcp/guide](https://aka.ms/nuget/mcp/guide) for the full guide.

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).

## Checklist before publishing to NuGet.org

- Test the MCP server locally using the steps below.
- Update the package metadata in the .csproj file, in particular the `<PackageId>`.
- Update `.mcp/server.json` to declare your MCP server's inputs.
- See [configuring inputs](https://aka.ms/nuget/mcp/guide/configuring-inputs) for more details.
- Pack the project using `dotnet pack`.

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).

## Developing locally

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`.

```json
{
"servers": {
"mcpserver": {
"type": "stdio",
"command": "dotnet",
"args": [
"run",
"--project",
"<PATH TO PROJECT DIRECTORY>"
]
}
}
}
```

## Testing the MCP Server

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.

## Publishing to NuGet.org

1. Run `dotnet pack -c Release` to create the NuGet package
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`

## Using the MCP Server from NuGet.org

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.

- **VS Code**: Create a `<WORKSPACE DIRECTORY>/.vscode/mcp.json` file
- **Visual Studio**: Create a `<SOLUTION DIRECTORY>\.mcp.json` file

For both VS Code and Visual Studio, the configuration file uses the following server definition:

```json
{
"servers": {
"mcpserver": {
"type": "stdio",
"command": "dnx",
"args": [
"<your package ID here>",
"--version",
"<your package version here>",
"--yes"
]
}
}
}
```

## More information

.NET MCP servers use the [ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol) C# SDK. For more information about MCP:

- [Official Documentation](https://modelcontextprotocol.io/)
- [Protocol Specification](https://spec.modelcontextprotocol.io/)
- [GitHub Organization](https://github.com/modelcontextprotocol)

Refer to the VS Code or Visual Studio documentation for more information on configuring and using MCP servers:

- [Use MCP servers in VS Code (Preview)](https://code.visualstudio.com/docs/copilot/chat/mcp-servers)
- [Use MCP servers in Visual Studio (Preview)](https://learn.microsoft.com/visualstudio/ide/mcp-servers)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel;
using ModelContextProtocol.Server;

/// <summary>
/// Sample MCP tools for demonstration purposes.
/// These tools can be invoked by MCP clients to perform various operations.
/// </summary>
internal class RandomNumberTools
{
[McpServerTool]
[Description("Generates a random number between the specified minimum and maximum values.")]
public int GetRandomNumber(
[Description("Minimum value (inclusive)")] int min = 0,
[Description("Maximum value (exclusive)")] int max = 100)
{
return Random.Shared.Next(min, max);
}
}
Loading
Loading