Skip to content

Commit 831cf4b

Browse files
Simplify and update README for stable release (#1387)
Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com>
1 parent 80044da commit 831cf4b

File tree

4 files changed

+169
-220
lines changed

4 files changed

+169
-220
lines changed

README.md

Lines changed: 10 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
# MCP C# SDK
22

3-
[![NuGet preview version](https://img.shields.io/nuget/vpre/ModelContextProtocol.svg)](https://www.nuget.org/packages/ModelContextProtocol/absoluteLatest)
3+
[![NuGet version](https://img.shields.io/nuget/v/ModelContextProtocol.svg)](https://www.nuget.org/packages/ModelContextProtocol)
44

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. Please visit our [API documentation](https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.html) for more details on available functionality.
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. Please visit the [API documentation](https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.html) for more details on available functionality.
66

77
## Packages
88

99
This SDK consists of three main packages:
1010

11-
- **[ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol/absoluteLatest)** [![NuGet preview version](https://img.shields.io/nuget/vpre/ModelContextProtocol.svg)](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.
11+
- **[ModelContextProtocol.Core](https://www.nuget.org/packages/ModelContextProtocol.Core)** [![NuGet version](https://img.shields.io/nuget/v/ModelContextProtocol.Core.svg)](https://www.nuget.org/packages/ModelContextProtocol.Core) - For projects that only need to use the client or low-level server APIs and want the minimum number of dependencies. [Documentation](src/ModelContextProtocol.Core/README.md)
1212

13-
- **[ModelContextProtocol.AspNetCore](https://www.nuget.org/packages/ModelContextProtocol.AspNetCore/absoluteLatest)** [![NuGet preview version](https://img.shields.io/nuget/vpre/ModelContextProtocol.AspNetCore.svg)](https://www.nuget.org/packages/ModelContextProtocol.AspNetCore/absoluteLatest) - The library for HTTP-based MCP servers. [Documentation](src/ModelContextProtocol.AspNetCore/README.md)
13+
- **[ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol)** [![NuGet version](https://img.shields.io/nuget/v/ModelContextProtocol.svg)](https://www.nuget.org/packages/ModelContextProtocol) - The main package with hosting and dependency injection extensions. References `ModelContextProtocol.Core`. This is the right fit for most projects that don't need HTTP server capabilities. [Documentation](src/ModelContextProtocol/README.md)
1414

15-
- **[ModelContextProtocol.Core](https://www.nuget.org/packages/ModelContextProtocol.Core/absoluteLatest)** [![NuGet preview version](https://img.shields.io/nuget/vpre/ModelContextProtocol.Core.svg)](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)
15+
- **[ModelContextProtocol.AspNetCore](https://www.nuget.org/packages/ModelContextProtocol.AspNetCore)** [![NuGet version](https://img.shields.io/nuget/v/ModelContextProtocol.AspNetCore.svg)](https://www.nuget.org/packages/ModelContextProtocol.AspNetCore) - The library for HTTP-based MCP servers. References `ModelContextProtocol`. [Documentation](src/ModelContextProtocol.AspNetCore/README.md)
1616

17-
> [!NOTE]
18-
> This project is in preview; breaking changes can be introduced without prior notice.
17+
## Getting Started
18+
19+
To get started, see the [Getting Started](https://modelcontextprotocol.github.io/csharp-sdk/concepts/getting-started.html) guide in the conceptual documentation for installation instructions, package-selection guidance, and complete examples for both clients and servers.
20+
21+
You can also browse the [samples](samples) directory and the [API documentation](https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.html) for more details on available functionality.
1922

2023
## About MCP
2124

@@ -27,219 +30,6 @@ For more information about MCP:
2730
- [Protocol Specification](https://modelcontextprotocol.io/specification/)
2831
- [GitHub Organization](https://github.com/modelcontextprotocol)
2932

30-
## Installation
31-
32-
To get started, install the package from NuGet
33-
34-
```
35-
dotnet add package ModelContextProtocol --prerelease
36-
```
37-
38-
## Getting Started (Client)
39-
40-
To get started writing a client, the `McpClient.CreateAsync` method is used to instantiate and connect an `McpClient`
41-
to a server. Once you have an `McpClient`, you can interact with it, such as to enumerate all available tools and invoke tools.
42-
43-
```csharp
44-
using ModelContextProtocol.Client;
45-
using ModelContextProtocol.Protocol;
46-
47-
var clientTransport = new StdioClientTransport(new StdioClientTransportOptions
48-
{
49-
Name = "Everything",
50-
Command = "npx",
51-
Arguments = ["-y", "@modelcontextprotocol/server-everything"],
52-
});
53-
54-
var client = await McpClient.CreateAsync(clientTransport);
55-
56-
// Print the list of tools available from the server.
57-
foreach (var tool in await client.ListToolsAsync())
58-
{
59-
Console.WriteLine($"{tool.Name} ({tool.Description})");
60-
}
61-
62-
// Execute a tool (this would normally be driven by LLM tool invocations).
63-
var result = await client.CallToolAsync(
64-
"echo",
65-
new Dictionary<string, object?>() { ["message"] = "Hello MCP!" },
66-
cancellationToken:CancellationToken.None);
67-
68-
// echo always returns one and only one text content object
69-
Console.WriteLine(result.Content.OfType<TextContentBlock>().First().Text);
70-
```
71-
72-
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.
73-
74-
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.
75-
76-
Tools can be easily exposed for immediate use by `IChatClient`s, because `McpClientTool` inherits from `AIFunction`.
77-
78-
```csharp
79-
// Get available functions.
80-
IList<McpClientTool> tools = await client.ListToolsAsync();
81-
82-
// Call the chat client using the tools.
83-
IChatClient chatClient = ...;
84-
var response = await chatClient.GetResponseAsync(
85-
"your prompt here",
86-
new() { Tools = [.. tools] });
87-
```
88-
89-
## Getting Started (Server)
90-
91-
> [!TIP]
92-
> You can use the [MCP Server project template](https://learn.microsoft.com/dotnet/ai/quickstarts/build-mcp-server?pivots=visualstudio) to quickly get started with creating your own MCP server.
93-
94-
Here is an example of how to create an MCP server and register all tools from the current application.
95-
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...
96-
the employed overload of `WithTools` examines the current assembly for classes with the `McpServerToolType` attribute, and registers all methods with the
97-
`McpServerTool` attribute as tools.)
98-
99-
```
100-
dotnet add package ModelContextProtocol --prerelease
101-
dotnet add package Microsoft.Extensions.Hosting
102-
```
103-
104-
```csharp
105-
using Microsoft.Extensions.DependencyInjection;
106-
using Microsoft.Extensions.Hosting;
107-
using Microsoft.Extensions.Logging;
108-
using ModelContextProtocol.Server;
109-
using System.ComponentModel;
110-
111-
var builder = Host.CreateApplicationBuilder(args);
112-
builder.Logging.AddConsole(consoleLogOptions =>
113-
{
114-
// Configure all logs to go to stderr
115-
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
116-
});
117-
builder.Services
118-
.AddMcpServer()
119-
.WithStdioServerTransport()
120-
.WithToolsFromAssembly();
121-
await builder.Build().RunAsync();
122-
123-
[McpServerToolType]
124-
public static class EchoTool
125-
{
126-
[McpServerTool, Description("Echoes the message back to the client.")]
127-
public static string Echo(string message) => $"hello {message}";
128-
}
129-
```
130-
131-
Tools can have the `McpServer` representing the server injected via a parameter to the method, and can use that for interaction with
132-
the connected client. Similarly, arguments may be injected via dependency injection. For example, this tool will use the supplied
133-
`McpServer` to make sampling requests back to the client in order to summarize content it downloads from the specified url via
134-
an `HttpClient` injected via dependency injection.
135-
```csharp
136-
[McpServerTool(Name = "SummarizeContentFromUrl"), Description("Summarizes content downloaded from a specific URI")]
137-
public static async Task<string> SummarizeDownloadedContent(
138-
McpServer thisServer,
139-
HttpClient httpClient,
140-
[Description("The url from which to download the content to summarize")] string url,
141-
CancellationToken cancellationToken)
142-
{
143-
string content = await httpClient.GetStringAsync(url);
144-
145-
ChatMessage[] messages =
146-
[
147-
new(ChatRole.User, "Briefly summarize the following downloaded content:"),
148-
new(ChatRole.User, content),
149-
];
150-
151-
ChatOptions options = new()
152-
{
153-
MaxOutputTokens = 256,
154-
Temperature = 0.3f,
155-
};
156-
157-
return $"Summary: {await thisServer.AsSamplingChatClient().GetResponseAsync(messages, options, cancellationToken)}";
158-
}
159-
```
160-
161-
Prompts can be exposed in a similar manner, using `[McpServerPrompt]`, e.g.
162-
```csharp
163-
[McpServerPromptType]
164-
public static class MyPrompts
165-
{
166-
[McpServerPrompt, Description("Creates a prompt to summarize the provided message.")]
167-
public static ChatMessage Summarize([Description("The content to summarize")] string content) =>
168-
new(ChatRole.User, $"Please summarize this content into a single sentence: {content}");
169-
}
170-
```
171-
172-
More control is also available, with fine-grained control over configuring the server and how it should handle client requests. For example:
173-
174-
```csharp
175-
using ModelContextProtocol;
176-
using ModelContextProtocol.Protocol;
177-
using ModelContextProtocol.Server;
178-
using System.Text.Json;
179-
180-
McpServerOptions options = new()
181-
{
182-
ServerInfo = new Implementation { Name = "MyServer", Version = "1.0.0" },
183-
Handlers = new McpServerHandlers()
184-
{
185-
ListToolsHandler = (request, cancellationToken) =>
186-
ValueTask.FromResult(new ListToolsResult
187-
{
188-
Tools =
189-
[
190-
new Tool
191-
{
192-
Name = "echo",
193-
Description = "Echoes the input back to the client.",
194-
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
195-
{
196-
"type": "object",
197-
"properties": {
198-
"message": {
199-
"type": "string",
200-
"description": "The input to echo back"
201-
}
202-
},
203-
"required": ["message"]
204-
}
205-
"""),
206-
}
207-
]
208-
}),
209-
210-
CallToolHandler = (request, cancellationToken) =>
211-
{
212-
if (request.Params?.Name == "echo")
213-
{
214-
if (request.Params.Arguments?.TryGetValue("message", out var message) is not true)
215-
{
216-
throw new McpProtocolException("Missing required argument 'message'", McpErrorCode.InvalidParams);
217-
}
218-
219-
return ValueTask.FromResult(new CallToolResult
220-
{
221-
Content = [new TextContentBlock { Text = $"Echo: {message}" }]
222-
});
223-
}
224-
225-
throw new McpProtocolException($"Unknown tool: '{request.Params?.Name}'", McpErrorCode.InvalidRequest);
226-
}
227-
}
228-
};
229-
230-
await using McpServer server = McpServer.Create(new StdioServerTransport("MyServer"), options);
231-
await server.RunAsync();
232-
```
233-
234-
Descriptions can be added to tools, prompts, and resources in a variety of ways, including via the `[Description]` attribute from `System.ComponentModel`.
235-
This attribute may be placed on a method to provide for the tool, prompt, or resource, or on individual parameters to describe each's purpose.
236-
XML comments may also be used; if an `[McpServerTool]`, `[McpServerPrompt]`, or `[McpServerResource]`-attributed method is marked as `partial`,
237-
XML comments placed on the method will be used automatically to generate `[Description]` attributes for the method and its parameters.
238-
239-
## Acknowledgements
240-
241-
The starting point for this library was a project called [mcpdotnet](https://github.com/PederHP/mcpdotnet), initiated by [Peder Holdgaard Pedersen](https://github.com/PederHP). We are grateful for the work done by Peder and other contributors to that repository, which created a solid foundation for this library.
242-
24333
## License
24434

24535
This project is licensed under the [Apache License 2.0](LICENSE).

0 commit comments

Comments
 (0)