csharp-sdk
Официальныйот modelcontextprotocol
The official C# SDK for Model Context Protocol servers and clients. Maintained in collaboration with Microsoft.
Установка
using ModelContextProtocol.Client;Описание
# MCP C# SDK [](https://www.nuget.org/packages/ModelContextProtocol/absoluteLatest) The official C# SDK for the [Model Context Protocol](https://modelcontextprotocol.io/), enabling .NET applications, services, and libraries to implement and interact with MCP clients and servers. Please visit our [API documentation](https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.html) for more details on available functionality. ## Packages This SDK consists of three main packages: - **[ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol/absoluteLatest)** [](https://www.nuget.org/packages/ModelContextProtocol/absoluteLatest) - The main package with hosting and dependency injection extensions. This is the right fit for most projects that don't need HTTP server capabilities. This README serves as documentation for this package. - **[ModelContextProtocol.AspNetCore](https://www.nuget.org/packages/ModelContextProtocol.AspNetCore/absoluteLatest)** [](https://www.nuget.org/packages/ModelContextProtocol.AspNetCore/absoluteLatest) - The library for HTTP-based MCP servers. [Documentation](src/ModelContextProtocol.AspNetCore/README.md) - **[ModelContextProtocol.Core](https://www.nuget.org/packages/ModelContextProtocol.Core/absoluteLatest)** [](https://www.nuget.org/packages/ModelContextProtocol.Core/absoluteLatest) - For people who only need to use the client or low-level server APIs and want the minimum number of dependencies. [Documentation](src/ModelContextProtocol.Core/README.md) > [!NOTE] > This project is in preview; breaking changes can be introduced without prior notice. ## About MCP The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to Large Language Models (LLMs). It enables secure integration between LLMs and various data sources and tools. For more information about MCP: - [Official Documentation](https://modelcontextprotocol.io/) - [Protocol Specification](https://modelcontextprotocol.io/specification/) - [GitHub Organization](https://github.com/modelcontextprotocol) ## Installation To get started, install the package from NuGet ``` dotnet add package ModelContextProtocol --prerelease ``` ## Getting Started (Client) To get started writing a client, the `McpClient.CreateAsync` method is used to instantiate and connect an `McpClient` to a server. Once you have an `McpClient`, you can interact with it, such as to enumerate all available tools and invoke tools. ```csharp using ModelContextProtocol.Client; using ModelContextProtocol.Protocol; var clientTransport = new StdioClientTransport(new StdioClientTransportOptions { Name = "Everything", Command = "npx", Arguments = ["-y", "@modelcontextprotocol/server-everything"], }); var client = await McpClient.CreateAsync(clientTransport); // Print the list of tools available from the server. foreach (var tool in await client.ListToolsAsync()) { Console.WriteLine($"{tool.Name} ({tool.Description})"); } // Execute a tool (this would normally be driven by LLM tool invocations). var result = await client.CallToolAsync( "echo", new Dictionary<string, object?>() { ["message"] = "Hello MCP!" }, cancellationToken:CancellationToken.None); // echo always returns one and only one text content object Console.WriteLine(result.Content.OfType<TextContentBlock>().First().Text); ``` You can find samples demonstrating how to use ModelContextProtocol with an LLM SDK in the [samples](samples) directory, and also refer to the [tests](tests/ModelContextProtocol.Tests) project for more examples. Additional examples and documentation will be added as in the near future. Clients can connect to any MCP server, not just ones created using this library. The protocol is designed to be server-agnostic, so you can use this library to connect to any compliant server. Tools can be easily exposed for immediate use by `IChatClient`s, because `McpClientTool` inherits from `AIFunction`. ```csharp // Get available functions. IList<McpClientTool> tools = await client.ListToolsAsync(); // Call the chat client using the tools. IChatClient chatClient = ...; var response = await chatClient.GetResponseAsync( "your prompt here", new() { Tools = [.. tools] }, ``` ## Getting Started (Server) Here is an example of how to create an MCP server and register all tools from the current application. It includes a simple echo tool as an example (this is included in the same file here for easy of copy and paste, but it needn't be in the same file... the employed overload of `WithTools` examines the current assembly for classes
Отзывы (0)
Пока нет отзывов. Будьте первым!
Статистика
Информация
Технологии
Похожие серверы
GitHub MCP
Полная интеграция с GitHub API: репозитории, issues, pull requests, actions и многое другое.
Filesystem MCP
Безопасный доступ к файловой системе для чтения, записи и управления файлами с настраиваемыми разрешениями.
Context7 MCP
Доступ к актуальной документации библиотек и фреймворков.
Serena MCP
Мощный MCP сервер для семантической навигации по коду и рефакторинга.