Skip to content

Commit a9427f9

Browse files
Tweak the file-based MCP server sample (#1019)
1 parent 6030a91 commit a9427f9

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

samples/FileBasedMcpServer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env -S dotnet run --
1+
#!/usr/bin/env dotnet
22
#:package Microsoft.Extensions.Hosting
33
#:project ../../src/ModelContextProtocol/ModelContextProtocol.csproj
44

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,59 @@
11
# File-Based MCP Server Sample
22

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

105
## Running the Sample
116

127
Simply run the Program.cs file directly:
138

149
```bash
15-
dotnet run Program.cs
10+
./Program.cs
1611
```
1712

1813
The server will start and listen for MCP messages on stdin/stdout (stdio transport).
1914

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

22-
On Unix-like systems, you can make the file executable:
19+
### Using the Inspector
2320

2421
```bash
25-
chmod +x Program.cs
26-
./Program.cs
22+
npx @modelcontextprotocol/inspector ./Program.cs
2723
```
2824

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
3226

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
3428

35-
### Initialize the server:
3629
```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
3831
```
3932

40-
### List available tools:
33+
#### List available tools
34+
4135
```bash
4236
(
4337
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0"}}}'
4438
sleep 0.5
4539
echo '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}'
4640
sleep 1
47-
) | dotnet run Program.cs 2>/dev/null | grep '^{' | jq .
41+
) | ./Program.cs 2>/dev/null | grep '^{' | jq .
4842
```
4943

50-
### Call the echo tool:
44+
#### Call the echo tool
45+
5146
```bash
5247
(
5348
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0"}}}'
5449
sleep 0.5
5550
echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"echo","arguments":{"message":"Hello, MCP!"}}}'
5651
sleep 1
57-
) | dotnet run Program.cs 2>/dev/null | grep '^{' | jq .
52+
) | ./Program.cs 2>/dev/null | grep '^{' | jq .
5853
```
5954

6055
## Reference
6156

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)
6459
- [Model Context Protocol Specification](https://modelcontextprotocol.io/specification/)

0 commit comments

Comments
 (0)