このクイック スタートでは、 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 サーバーを起動するホストをビルドして実行します。
- クライアントに値をエコーバックする 2 つの 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