|
2 | 2 |
|
3 | 3 | [](https://www.nuget.org/packages/ModelContextProtocol) |
4 | 4 |
|
5 | | -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. This is the main package with hosting and dependency injection extensions. Please visit our [API documentation](https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.html) for more details on available functionality. |
6 | | - |
7 | | -## About MCP |
8 | | - |
9 | | -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. |
10 | | - |
11 | | -For more information about MCP: |
12 | | - |
13 | | -- [Official Documentation](https://modelcontextprotocol.io/) |
14 | | -- [Protocol Specification](https://modelcontextprotocol.io/specification/) |
15 | | -- [GitHub Organization](https://github.com/modelcontextprotocol) |
| 5 | +The official C# SDK for the [Model Context Protocol](https://modelcontextprotocol.io/), with hosting and dependency injection extensions. References `ModelContextProtocol.Core`. Please visit the [API documentation](https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.html) for more details on available functionality. |
16 | 6 |
|
17 | 7 | ## Installation |
18 | 8 |
|
19 | | -To get started, install the package from NuGet |
20 | | - |
21 | 9 | ``` |
22 | 10 | dotnet add package ModelContextProtocol |
23 | 11 | ``` |
24 | 12 |
|
25 | | -## Getting Started (Server) |
| 13 | +## Getting Started |
26 | 14 |
|
27 | | -Create a new console app, add the required packages, and replace `Program.cs` with the code below to get a working MCP server that exposes a single tool over stdio: |
| 15 | +To get started, see the [Getting Started](https://modelcontextprotocol.github.io/csharp-sdk/concepts/getting-started.html) guide for installation instructions, package-selection guidance, and complete examples for both clients and servers. |
28 | 16 |
|
29 | | -``` |
30 | | -dotnet new console |
31 | | -dotnet add package ModelContextProtocol |
32 | | -dotnet add package Microsoft.Extensions.Hosting |
33 | | -``` |
34 | | - |
35 | | -```csharp |
36 | | -using Microsoft.Extensions.DependencyInjection; |
37 | | -using Microsoft.Extensions.Hosting; |
38 | | -using Microsoft.Extensions.Logging; |
39 | | -using ModelContextProtocol.Server; |
40 | | -using System.ComponentModel; |
41 | | - |
42 | | -var builder = Host.CreateApplicationBuilder(args); |
43 | | -builder.Logging.AddConsole(consoleLogOptions => |
44 | | -{ |
45 | | - // Configure all logs to go to stderr |
46 | | - consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace; |
47 | | -}); |
48 | | -builder.Services |
49 | | - .AddMcpServer() |
50 | | - .WithStdioServerTransport() |
51 | | - .WithToolsFromAssembly(); |
52 | | -await builder.Build().RunAsync(); |
53 | | - |
54 | | -[McpServerToolType] |
55 | | -public static class EchoTool |
56 | | -{ |
57 | | - [McpServerTool, Description("Echoes the message back to the client.")] |
58 | | - public static string Echo(string message) => $"hello {message}"; |
59 | | -} |
60 | | -``` |
61 | | - |
62 | | -The call to `WithToolsFromAssembly` discovers every class marked with `[McpServerToolType]` in the assembly and registers every `[McpServerTool]` method as a tool. Prompts and resources work the same way with `[McpServerPromptType]` / `[McpServerPrompt]` and `[McpServerResourceType]` / `[McpServerResource]`. |
63 | | - |
64 | | -## Getting Started (Client) |
65 | | - |
66 | | -To get started writing a client, the `McpClient.CreateAsync` method is used to instantiate and connect an `McpClient` |
67 | | -to a server. Once you have an `McpClient`, you can interact with it, such as to enumerate all available tools and invoke tools. |
68 | | - |
69 | | -```csharp |
70 | | -var clientTransport = new StdioClientTransport(new StdioClientTransportOptions |
71 | | -{ |
72 | | - Name = "Everything", |
73 | | - Command = "npx", |
74 | | - Arguments = ["-y", "@modelcontextprotocol/server-everything"], |
75 | | -}); |
76 | | - |
77 | | -var client = await McpClient.CreateAsync(clientTransport); |
78 | | - |
79 | | -// Print the list of tools available from the server. |
80 | | -foreach (var tool in await client.ListToolsAsync()) |
81 | | -{ |
82 | | - Console.WriteLine($"{tool.Name} ({tool.Description})"); |
83 | | -} |
84 | | - |
85 | | -// Execute a tool (this would normally be driven by LLM tool invocations). |
86 | | -var result = await client.CallToolAsync( |
87 | | - "echo", |
88 | | - new Dictionary<string, object?>() { ["message"] = "Hello MCP!" }, |
89 | | - cancellationToken: CancellationToken.None); |
90 | | - |
91 | | -// echo always returns one and only one text content object |
92 | | -Console.WriteLine(result.Content.OfType<TextContentBlock>().First().Text); |
93 | | -``` |
94 | | - |
95 | | -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. |
| 17 | +## About MCP |
96 | 18 |
|
97 | | -Tools can be easily exposed for immediate use by `IChatClient`s, because `McpClientTool` inherits from `AIFunction`. |
| 19 | +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. |
98 | 20 |
|
99 | | -```csharp |
100 | | -// Get available functions. |
101 | | -IList<McpClientTool> tools = await client.ListToolsAsync(); |
| 21 | +For more information about MCP: |
102 | 22 |
|
103 | | -// Call the chat client using the tools. |
104 | | -IChatClient chatClient = ...; |
105 | | -var response = await chatClient.GetResponseAsync( |
106 | | - "your prompt here", |
107 | | - new() { Tools = [.. tools] }); |
108 | | -``` |
| 23 | +- [Official Documentation](https://modelcontextprotocol.io/) |
| 24 | +- [Protocol Specification](https://modelcontextprotocol.io/specification/) |
| 25 | +- [GitHub Organization](https://github.com/modelcontextprotocol) |
0 commit comments