지속적으로 ModelContextProtocol SDK의 버전이 업데이트되면서, 이제는 Native AOT 시나리오로도 사용할 수 있게 개선되었습니다. 그래서 아래와 같이 매우 간결한 .NET 10의 File-based app 스타일의 MCP 서버 코드를 빌드 옵션 변경 없이 작성할 수 있었고 매우 잘 작동합니다.
다만 Virtual Project로 빌드가 이루어지다보니, 코드를 다시 빌드할 일이 발생할 경우 여러 MCP 서버를 동시에 사용하려 할 때 stdout에 msbuild의 출력이 나타나다보니 첫 로딩 이후 후속으로 로딩하려는 MCP 클라이언트에서는 오류가 발생할 수 있다는 known-issue를 고려해야 하는 점이 있었습니다.
#!/usr/bin/env dotnet
#:sdk Microsoft.NET.Sdk.Web
#:package ModelContextProtocol@0.3.0-preview.4
// 노트: dotnet run .cs 명령으로는 이 MCP 서버를 동시에 여러 클라이언트에서 사용하지 못할 수 있습니다.
using System.ComponentModel;
using ModelContextProtocol.Server;
var builder = Host.CreateEmptyApplicationBuilder(default);
builder.Configuration.AddCommandLine(args);
builder.Configuration.AddEnvironmentVariables();
builder.Services.AddHttpClient();
builder.Services.AddMcpServer()
.WithStdioServerTransport()
.WithTools([
McpServerTool.Create(IpAddressTool),
]);
var app = builder.Build();
app.Run();
[Description("Get the public IP address of this machine.")]
async Task<string> IpAddressTool(
IServiceProvider services,
[Description("Get IPv6 address instead of IPv4 address")] bool ipv6)
{
try
{
var client = services.GetRequiredService<HttpClient>();
return await client.GetStringAsync(
ipv6 ? "https://api6.ipify.org" : "https://api.ipify.org"
).ConfigureAwait(false);
}
catch (Exception ex)
{
return $"Error occurred: {ex.Message}";
}
}
그리고 다음과 같이 MCP 서버 설정 JSON을 추가하여 연동할 수 있습니다.
...
"servers": {
"my-mcp-server-a983fe19": {
"type": "stdio",
"command": "dotnet",
"args": [
"run",
"--arch",
"x64",
"C:\\Users\\rkttu\\Desktop\\simpleweb\\McpTest.cs"
]
}
},
...
좀 더 안정적인 환경을 구현하려면, dotnet publish
명령으로 EXE 파일을 만들어서 MCP 서버를 사용하는 것이 유용한 것 같습니다.