Skip to content

Commit 5457603

Browse files
authored
Add file-based MCP server sample for .NET 10 (#3) (#931)
1 parent a609aa2 commit 5457603

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env -S dotnet run --
2+
#:package Microsoft.Extensions.Hosting
3+
#:project ../../src/ModelContextProtocol/ModelContextProtocol.csproj
4+
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Logging;
8+
using ModelContextProtocol.Server;
9+
using System.ComponentModel;
10+
11+
var builder = Host.CreateApplicationBuilder(args);
12+
13+
builder.Services.AddMcpServer()
14+
.WithStdioServerTransport()
15+
.WithTools<EchoTool>();
16+
17+
builder.Logging.AddConsole(options =>
18+
{
19+
options.LogToStandardErrorThreshold = LogLevel.Trace;
20+
});
21+
22+
await builder.Build().RunAsync();
23+
24+
// File-scoped tool class
25+
[McpServerToolType]
26+
file class EchoTool
27+
{
28+
[McpServerTool(Name = "echo"), Description("Echoes the message back to the client.")]
29+
public static string Echo([Description("The message to echo back.")] string message) => $"Echo: {message}";
30+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# File-Based MCP Server Sample
2+
3+
This sample demonstrates how to create a complete MCP (Model Context Protocol) server using .NET 10's file-based programs feature. Unlike traditional .NET projects that require a `.csproj` file, file-based programs allow you to write and run complete applications in a single `.cs` file.
4+
5+
## Requirements
6+
7+
- .NET 10 SDK (RC2 or later)
8+
- No project file required!
9+
10+
## Running the Sample
11+
12+
Simply run the Program.cs file directly:
13+
14+
```bash
15+
dotnet run Program.cs
16+
```
17+
18+
The server will start and listen for MCP messages on stdin/stdout (stdio transport).
19+
20+
### Making it Executable (Unix/Linux/macOS)
21+
22+
On Unix-like systems, you can make the file executable:
23+
24+
```bash
25+
chmod +x Program.cs
26+
./Program.cs
27+
```
28+
29+
Note: The shebang line uses `/usr/bin/env` to locate `dotnet`, so ensure it's in your PATH.
30+
31+
## Testing the Server
32+
33+
You can test the server by using `@modelcontextprotocol/inspector`, any stdio-compatible client, or sending JSON-RPC messages to stdin. Here's an example:
34+
35+
### Initialize the server:
36+
```bash
37+
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0"}}}' | dotnet run Program.cs
38+
```
39+
40+
### List available tools:
41+
```bash
42+
(
43+
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0"}}}'
44+
sleep 0.5
45+
echo '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}'
46+
sleep 1
47+
) | dotnet run Program.cs 2>/dev/null | grep '^{' | jq .
48+
```
49+
50+
### Call the echo tool:
51+
```bash
52+
(
53+
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0"}}}'
54+
sleep 0.5
55+
echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"echo","arguments":{"message":"Hello, MCP!"}}}'
56+
sleep 1
57+
) | dotnet run Program.cs 2>/dev/null | grep '^{' | jq .
58+
```
59+
60+
## Reference
61+
62+
- [File-Based Programs Tutorial](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/file-based-programs)
63+
- [C# Preprocessor Directives for File-Based Apps](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#file-based-apps)
64+
- [Model Context Protocol Specification](https://spec.modelcontextprotocol.io/)

0 commit comments

Comments
 (0)