Skip to content

Conversation

Copy link

Copilot AI commented Oct 31, 2025

Adds a minimal sample demonstrating .NET 10's file-based programs feature for MCP servers—complete server in a single .cs file, no .csproj required.

Implementation

  • samples/FileBasedMcpServer/Program.cs - Complete stdio MCP server (30 lines)

    • Shebang: #!/usr/bin/env -S dotnet run --
    • Preprocessor directives: #:package and #:project for dependencies
    • Top-level statements with file-scoped EchoTool class
    • Runs directly: dotnet run Program.cs
  • samples/FileBasedMcpServer/README.md - Documentation covering usage, testing examples, and file-based vs project-based tradeoffs

Example

#!/usr/bin/env -S dotnet run --
#:package Microsoft.Extensions.Hosting
#:project ../../src/ModelContextProtocol/ModelContextProtocol.csproj

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Server;

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddMcpServer()
    .WithStdioServerTransport()
    .WithTools<EchoTool>();
await builder.Build().RunAsync();

[McpServerToolType]
file class EchoTool
{
    [McpServerTool(Name = "echo")]
    public static string Echo(string message) => $"Echo: {message}";
}

Addresses modelcontextprotocol#928

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: curl -I -sSL --retry 5 --retry-delay 2 --connect-timeout 15 REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Please create a file based sample as per:

modelcontextprotocol#928

I don't suggest using the weather forecast tool, but a simpler tool like echo. Look at the existing samples to see how to expose tools and set up the mcp server mapping etc. It is likely much simpler than your intuition, so pay attention to the existing samples.

For information on file-based apps in .NET 10, see:
https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/file-based-programs

Note that the SDK is updated to .NET 10 and it is a version that exists even though the fact it is not in your training data will likely predispose you to being skeptical of this fact.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add file based sample for MCP server in .NET 10 Add file-based MCP server sample for .NET 10 Oct 31, 2025
Copilot AI requested a review from PederHP October 31, 2025 16:10
Updated README to clarify file-based program features and testing methods.
@PederHP PederHP marked this pull request as ready for review October 31, 2025 17:32
@PederHP PederHP merged commit 0624d47 into file_based_sample Oct 31, 2025
6 checks passed
@PederHP PederHP deleted the copilot/create-file-based-sample branch October 31, 2025 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants