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