|
1 | 1 | # File-Based MCP Server Sample |
2 | 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! |
| 3 | +This sample demonstrates how to create a complete MCP (Model Context Protocol) server using [.NET 10's file-based programs feature](https://learn.microsoft.com/dotnet/csharp/fundamentals/tutorials/file-based-programs). 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. |
9 | 4 |
|
10 | 5 | ## Running the Sample |
11 | 6 |
|
12 | 7 | Simply run the Program.cs file directly: |
13 | 8 |
|
14 | 9 | ```bash |
15 | | -dotnet run Program.cs |
| 10 | +./Program.cs |
16 | 11 | ``` |
17 | 12 |
|
18 | 13 | The server will start and listen for MCP messages on stdin/stdout (stdio transport). |
19 | 14 |
|
20 | | -### Making it Executable (Unix/Linux/macOS) |
| 15 | +## Testing the Server |
| 16 | + |
| 17 | +You can test the server by using `@modelcontextprotocol/inspector`, any stdio-compatible client, or sending JSON-RPC messages to stdin. |
21 | 18 |
|
22 | | -On Unix-like systems, you can make the file executable: |
| 19 | +### Using the Inspector |
23 | 20 |
|
24 | 21 | ```bash |
25 | | -chmod +x Program.cs |
26 | | -./Program.cs |
| 22 | +npx @modelcontextprotocol/inspector ./Program.cs |
27 | 23 | ``` |
28 | 24 |
|
29 | | -Note: The shebang line uses `/usr/bin/env` to locate `dotnet`, so ensure it's in your PATH. |
30 | | - |
31 | | -## Testing the Server |
| 25 | +### Using STDIN |
32 | 26 |
|
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: |
| 27 | +#### Initialize the server |
34 | 28 |
|
35 | | -### Initialize the server: |
36 | 29 | ```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 |
| 30 | +echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0"}}}' | ./Program.cs |
38 | 31 | ``` |
39 | 32 |
|
40 | | -### List available tools: |
| 33 | +#### List available tools |
| 34 | + |
41 | 35 | ```bash |
42 | 36 | ( |
43 | 37 | echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0"}}}' |
44 | 38 | sleep 0.5 |
45 | 39 | echo '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' |
46 | 40 | sleep 1 |
47 | | -) | dotnet run Program.cs 2>/dev/null | grep '^{' | jq . |
| 41 | +) | ./Program.cs 2>/dev/null | grep '^{' | jq . |
48 | 42 | ``` |
49 | 43 |
|
50 | | -### Call the echo tool: |
| 44 | +#### Call the echo tool |
| 45 | + |
51 | 46 | ```bash |
52 | 47 | ( |
53 | 48 | echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0"}}}' |
54 | 49 | sleep 0.5 |
55 | 50 | echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"echo","arguments":{"message":"Hello, MCP!"}}}' |
56 | 51 | sleep 1 |
57 | | -) | dotnet run Program.cs 2>/dev/null | grep '^{' | jq . |
| 52 | +) | ./Program.cs 2>/dev/null | grep '^{' | jq . |
58 | 53 | ``` |
59 | 54 |
|
60 | 55 | ## Reference |
61 | 56 |
|
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) |
| 57 | +- [File-Based Programs Tutorial](https://learn.microsoft.com/dotnet/csharp/fundamentals/tutorials/file-based-programs) |
| 58 | +- [C# Preprocessor Directives for File-Based Apps](https://learn.microsoft.com/dotnet/csharp/language-reference/preprocessor-directives#file-based-apps) |
64 | 59 | - [Model Context Protocol Specification](https://modelcontextprotocol.io/specification/) |
0 commit comments