SC

sannlynnhtun-coding/csharp-mcp-sample

开发工具

本页由 AI 翻译,技术细节请以原文为准。 查看原文

5 stars 3 forks 质量 53 趋势 17

This project demonstrates how to set up and use the Model Context Protocol (MCP) in C# .NET 8. It includes both an MCP Server and an MCP Client to demonstrate communication over Stdio transport.

概览

This project demonstrates how to set up and use the in C# .NET 8. It includes both an MCP Server and an MCP Client to demonstrate communication over Stdio transport. The solution (CSharpMcpSample.slnx) contains two projects: An MCP server that exposes tools to manage simple "Notes" stored in a local SQLite database. * : ModelContextProtocol SDK, Dapper, Microsoft.Data.Sqlite. * : * add_note: Adds a new note with a title and content. * get_notes: Retrieves all notes from the database. A console application acting as an MCP client. It launches the server as a background process, connects to it via Stdio transport, lists the available tools, and calls them. * : ModelContextProtocol SDK. The interaction between the client and server follows this flow: 1. : The CSharpMcpClientSample application starts. 2. : The client uses StdioClientTransport to launch the CSharpMcpServerSample as a child process using dotnet run. 3.

README

以下为原文 README 节选,完整内容请查看原文。

C# MCP Sample

This project demonstrates how to set up and use the Model Context Protocol (MCP) in C# .NET 8. It includes both an MCP Server and an MCP Client to demonstrate communication over Stdio transport.

Project Structure

The solution (CSharpMcpSample.slnx) contains two projects:

1. CSharpMcpServerSample

An MCP server that exposes tools to manage simple “Notes” stored in a local SQLite database.

  • Technologies: ModelContextProtocol SDK, Dapper, Microsoft.Data.Sqlite.
  • Tools Exposed:
    • add_note: Adds a new note with a title and content.
    • get_notes: Retrieves all notes from the database.

2. CSharpMcpClientSample

A console application acting as an MCP client. It launches the server as a background process, connects to it via Stdio transport, lists the available tools, and calls them.

  • Technologies: ModelContextProtocol SDK.

Project Flow

The interaction between the client and server follows this flow:

  1. Client Starts: The CSharpMcpClientSample application starts.
  2. Server Spawns: The client uses StdioClientTransport to launch the CSharpMcpServerSample as a child process using dotnet run.
  3. Connection: The client and server establish a JSON-RPC connection over the server’s standard input (stdin) and standard output (stdout).
  4. Discovery: The client calls ListToolsAsync(), sending a tools/list request to the server. The server responds with the schemas of add_note and get_notes.
  5. Execution:
    • The client calls add_note with arguments. The server inserts the note into the SQLite database and returns the result.
    • The client calls get_notes. The server queries the database and returns the list of notes.

Prerequisites

How to Run

Run the Client (Automated Test)

The easiest way to test the setup is to run the client project. The client is configured to automatically launch the server project and interact with it.

  1. Open a terminal and navigate to the client directory:
    cd CSharpMcpClientSample
    
  2. Run the project:
    dotnet run
    

You should see output showing the connection, the listed tools, and the results of calling add_note and get_notes.

Manual Testing of the Server

If you want to test the server directly without the client:

  1. Navigate to the server directory:
    cd CSharpMcpServerSample
    
  2. Run the server:
    dotnet run
    
    The console will appear to hang because it is waiting for JSON-RPC input on standard input.
  3. Paste a JSON-RPC request to list tools:
    {"jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {}}
    
    Press Enter. You should see a JSON response listing the tools.

Key Learning Points

在 GitHub 查看完整 README

安装

This server does not publish a one-line install command.

Open the repository installation guide 新手?先看 mcpskill.org 教程 →