@@ -83,13 +83,24 @@ It includes a simple echo tool as an example (this is included in the same file
8383the employed overload of `WithTools ` examines the current assembly for classes with the `McpServerToolType ` attribute , and registers all methods with the
8484`McpTool ` attribute as tools .)
8585
86+ ```
87+ dotnet add package ModelContextProtocol --prerelease
88+ dotnet add package Microsoft .Extensions .Hosting
89+ ```
90+
8691```csharp
87- using ModelContextProtocol ;
88- using ModelContextProtocol .Server ;
92+ using Microsoft .Extensions .DependencyInjection ;
8993using Microsoft .Extensions .Hosting ;
94+ using Microsoft .Extensions .Logging ;
95+ using ModelContextProtocol .Server ;
9096using System .ComponentModel ;
9197
92- var builder = Host .CreateEmptyApplicationBuilder (settings : null );
98+ var builder = Host .CreateApplicationBuilder (args );
99+ builder .Logging .AddConsole (consoleLogOptions =>
100+ {
101+ // Configure all logs to go to stderr
102+ consoleLogOptions .LogToStandardErrorThreshold = LogLevel .Trace ;
103+ });
93104builder .Services
94105 .AddMcpServer ()
95106 .WithStdioServerTransport ()
@@ -109,7 +120,7 @@ the connected client. Similarly, arguments may be injected via dependency inject
109120`IMcpServer` to make sampling requests back to the client in order to summarize content it downloads from the specified url via
110121an `HttpClient` injected via dependency injection.
111122```csharp
112- [McpServerTool("SummarizeContentFromUrl"), Description("Summarizes content downloaded from a specific URI")]
123+ [McpServerTool(Name = "SummarizeContentFromUrl"), Description("Summarizes content downloaded from a specific URI")]
113124public static async Task<string> SummarizeDownloadedContent(
114125 IMcpServer thisServer,
115126 HttpClient httpClient,
@@ -122,8 +133,8 @@ public static async Task<string> SummarizeDownloadedContent(
122133 [
123134 new (ChatRole .User , " Briefly summarize the following downloaded content:" ),
124135 new (ChatRole .User , content ),
125- ]
126-
136+ ];
137+
127138 ChatOptions options = new ()
128139 {
129140 MaxOutputTokens = 256 ,
@@ -134,13 +145,24 @@ public static async Task<string> SummarizeDownloadedContent(
134145}
135146```
136147
148+ Prompts can be exposed in a similar manner , using `[McpServerPrompt ]`, e .g .
149+ ```csharp
150+ [McpServerPromptType ]
151+ public static class MyPrompts
152+ {
153+ [McpServerPrompt , Description (" Creates a prompt to summarize the provided message." )]
154+ public static ChatMessage Summarize ([Description (" The content to summarize" )] string content ) =>
155+ new (ChatRole .User , $" Please summarize this content into a single sentence: {content }" );
156+ }
157+ ```
158+
137159More control is also available , with fine - grained control over configuring the server and how it should handle client requests . For example :
138160
139161```csharp
140162using ModelContextProtocol .Protocol .Transport ;
141163using ModelContextProtocol .Protocol .Types ;
142164using ModelContextProtocol .Server ;
143- using Microsoft . Extensions . Logging . Abstractions ;
165+ using System . Text . Json ;
144166
145167McpServerOptions options = new ()
146168{
@@ -149,9 +171,8 @@ McpServerOptions options = new()
149171 {
150172 Tools = new ()
151173 {
152- ListToolsHandler = async (request , cancellationToken ) =>
153- {
154- return new ListToolsResult ()
174+ ListToolsHandler = (request , cancellationToken ) =>
175+ Task .FromResult (new ListToolsResult ()
155176 {
156177 Tools =
157178 [
@@ -173,10 +194,9 @@ McpServerOptions options = new()
173194 """ ),
174195 }
175196 ]
176- };
177- },
197+ }),
178198
179- CallToolHandler = async (request , cancellationToken ) =>
199+ CallToolHandler = (request , cancellationToken ) =>
180200 {
181201 if (request .Params ? .Name == " echo" )
182202 {
@@ -185,10 +205,10 @@ McpServerOptions options = new()
185205 throw new McpServerException (" Missing required argument 'message'" );
186206 }
187207
188- return new CallToolResponse ()
208+ return Task . FromResult ( new CallToolResponse ()
189209 {
190210 Content = [new Content () { Text = $" Echo: {message }" , Type = " text" }]
191- };
211+ }) ;
192212 }
193213
194214 throw new McpServerException ($" Unknown tool: '{request .Params ? .Name }'" );
0 commit comments