이 빠른 시작에서는 MCP용 C# SDK 를 사용하여 최소 MCP(모델 컨텍스트 프로토콜) 서버를 만들고 GitHub Copilot를 사용하여 연결합니다. MCP 서버는 MCP(모델 컨텍스트 프로토콜)를 통해 클라이언트에 기능을 노출하는 서비스입니다.
필수 조건
- .NET 8.0 SDK 이상
- Visual Studio Code
- Visual Studio Code용 GitHub Copilot 확장
프로젝트를 만듭니다.
터미널 창에서 앱을 만들 디렉터리로 이동하고 다음 명령을 사용하여 새 콘솔 앱을
dotnet new
만듭니다.dotnet new console -n MinimalMcpServer
MinimalMcpServer
디렉터리로 이동하십시오.cd MinimalMcpServer
앱에 다음 NuGet 패키지를 추가합니다.
dotnet add package ModelContextProtocol --prerelease dotnet add package Microsoft.Extensions.Hosting
- ModelContextProtocol 패키지는 모델 컨텍스트 프로토콜을 사용하기 위한 공식 C# SDK입니다.
-
Microsoft.Extensions.Hosting 패키지는 로깅 및 종속성 주입을 위한 일반 .NET
HostBuilder
및 서비스를 제공합니다.
앱 코드 추가
간단한 에코 도구를 노출하는 최소 MCP 서버를 구현하려면 다음 코드로 내용을 Program.cs
바꿉니다. AI 모델은 필요에 따라 이러한 도구를 호출하여 사용자 프롬프트에 대한 응답을 생성합니다.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;
// Create a generic host builder for
// dependency injection, logging, and configuration.
var builder = Host.CreateApplicationBuilder(args);
// Configure logging for better integration with MCP clients.
builder.Logging.AddConsole(consoleLogOptions =>
{
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});
// Register the MCP server and configure it to use stdio transport.
// Scan the assembly for tool definitions.
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
// Build and run the host. This starts the MCP server.
await builder.Build().RunAsync();
// Define a static class to hold MCP tools.
[McpServerToolType]
public static class EchoTool
{
// Expose a tool that echoes the input message back to the client.
[McpServerTool, Description("Echoes the message back to the client.")]
public static string Echo(string message) => $"Hello from C#: {message}";
// Expose a tool that returns the input message in reverse.
[McpServerTool, Description("Echoes in reverse the message sent by the client.")]
public static string ReverseEcho(string message) => new string(message.Reverse().ToArray());
}
앞의 코드는 다음과 같습니다.
- 종속성 주입, 로깅 및 구성을 위한 제네릭 호스트 작성기를 만듭니다.
- MCP 클라이언트와의 통합을 향상하기 위해 로깅을 구성합니다.
- MCP 서버를 등록하고, stdio 전송을 사용하도록 구성하고, 어셈블리에서 도구 정의를 검사합니다.
- MCP 서버를 시작하는 호스트를 빌드하고 실행합니다.
- 클라이언트에 값을 다시 에코하는 두 개의 MCP 도구를 보유하도록 정적 클래스를 정의합니다.
Visual Studio Code에서 MCP 서버 구성
사용자 지정 MCP 서버를 사용하도록 Visual Studio Code용 GitHub Copilot를 구성합니다.
아직 열려 있지 않은 경우 Visual Studio Code에서 프로젝트 폴더를 엽니다.
.vscode
프로젝트의 루트에 폴더를 만듭니다.mcp.json
폴더에 다음 내용이 포함된.vscode
파일을 추가합니다.{ "inputs": [], "servers": { "MinimalMcpServer": { "type": "stdio", "command": "dotnet", "args": [ "run", "--project", "${workspaceFolder}/MinimalMcpServer.csproj" ] } } }
파일을 저장합니다.
MCP 서버 테스트
Visual Studio Code에서 GitHub Copilot를 열고 에이전트 모드로 전환합니다.
도구 선택 아이콘을 선택하여 나열된 두 도구에서 MinimalMcpServer를 사용할 수 있는지 확인합니다.
ReverseEcho 도구를 실행하라는 프롬프트를 입력합니다.
Reverse the following: "Hello, minimal MCP server!"
GitHub Copilot는 프롬프트에 대한 ReverseEcho 도구를 실행할 수 있는 권한을 요청합니다. 계속을 선택하거나 화살표를 사용하여 보다 구체적인 동작을 선택합니다.
- 현재 세션 은 항상 현재 GitHub Copilot 에이전트 모드 세션에서 작업을 실행합니다.
- 현재 작업 영역은 항상 현재 Visual Studio Code 작업 영역에 대한 명령을 실행합니다.
- 항상 모든 GitHub Copilot 에이전트 모드 세션 또는 Visual Studio Code 작업 영역에 대해 작업을 실행하도록 항상 허용합니다.
서버가 반복된 메시지로 응답했는지 확인합니다.
!revres PCM laminim ,olleH
관련 콘텐츠
.NET