@@ -44,7 +44,7 @@ await foreach (var tool in client.ListToolsAsync())
4444// Execute a tool (this would normally be driven by LLM tool invocations).
4545var result = await client .CallToolAsync (
4646 " echo" ,
47- new () { [" message" ] = " Hello MCP!" },
47+ new Dictionary < string , object ?> () { [" message" ] = " Hello MCP!" },
4848 CancellationToken .None );
4949
5050// echo always returns one and only one text content object
@@ -59,16 +59,13 @@ Tools can be exposed easily as `AIFunction` instances so that they are immediate
5959
6060``` csharp
6161// Get available functions.
62- IList < AIFunction > tools = await client .GetAIFunctionsAsync ();
62+ IList < McpClientTool > tools = await client .ListToolsAsync ();
6363
6464// Call the chat client using the tools.
6565IChatClient chatClient = .. .;
6666var response = await chatClient .GetResponseAsync (
6767 " your prompt here" ,
68- new ()
69- {
70- Tools = [.. tools ],
71- });
68+ new () { Tools = [.. tools ] },
7269```
7370
7471## Getting Started (Server)
@@ -88,17 +85,47 @@ var builder = Host.CreateEmptyApplicationBuilder(settings: null);
8885builder .Services
8986 .AddMcpServer ()
9087 .WithStdioServerTransport ()
91- .WithTools ();
88+ .WithToolsFromAssembly ();
9289await builder .Build ().RunAsync ();
9390
94- [McpToolType ]
91+ [McpServerToolType ]
9592public static class EchoTool
9693{
97- [McpTool , Description (" Echoes the message back to the client." )]
94+ [McpServerTool , Description (" Echoes the message back to the client." )]
9895 public static string Echo (string message ) => $" hello {message }" ;
9996}
10097```
10198
99+ Tools can have the `IMcpServer ` representing the server injected via a parameter to the method , and can use that for interaction with
100+ the connected client. Similarly, arguments may be injected via dependency injection. For example, this tool will use the supplied
101+ `IMcpServer` to make sampling requests back to the client in order to summarize content it downloads from the specified url via
102+ an `HttpClient` injected via dependency injection.
103+ ```csharp
104+ [McpServerTool("SummarizeContentFromUrl"), Description("Summarizes content downloaded from a specific URI")]
105+ public static async Task<string> SummarizeDownloadedContent(
106+ IMcpServer thisServer,
107+ HttpClient httpClient,
108+ [Description("The url from which to download the content to summarize")] string url,
109+ CancellationToken cancellationToken)
110+ {
111+ string content = await httpClient .GetStringAsync (url );
112+
113+ ChatMessage [] messages =
114+ [
115+ new (ChatRole .User , " Briefly summarize the following downloaded content:" ),
116+ new (ChatRole .User , content ),
117+ ]
118+
119+ ChatOptions options = new ()
120+ {
121+ MaxOutputTokens = 256 ,
122+ Temperature = 0 . 3 f ,
123+ };
124+
125+ return $" Summary: {await thisServer .AsSamplingChatClient ().GetResponseAsync (messages , options , cancellationToken )}" ;
126+ }
127+ ```
128+
102129More control is also available , with fine - grained control over configuring the server and how it should handle client requests . For example :
103130
104131```csharp
@@ -124,14 +151,18 @@ McpServerOptions options = new()
124151 {
125152 Name = " echo" ,
126153 Description = " Echoes the input back to the client." ,
127- InputSchema = new JsonSchema ()
128- {
129- Type = " object" ,
130- Properties = new Dictionary <string , JsonSchemaProperty >()
154+ InputSchema = JsonSerializer .Deserialize <JsonElement >("""
131155 {
132- [" message" ] = new JsonSchemaProperty () { Type = " string" , Description = " The input to echo back." }
156+ "type": "object",
157+ "properties": {
158+ "message": {
159+ "type": "string",
160+ "description": "The input to echo back"
161+ }
162+ },
163+ "required": ["message"]
133164 }
134- } ,
165+ """ ) ,
135166 }
136167 ]
137168 };
0 commit comments