Skip to content

Commit 687c045

Browse files
committed
working on .NET pivot
1 parent a8dce24 commit 687c045

File tree

3 files changed

+63
-22
lines changed

3 files changed

+63
-22
lines changed

03-GettingStarted/03-llm-client/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,31 @@ In the preceding code we've:
120120

121121
</details>
122122

123+
<details>
124+
<summary>.NET</summary>
125+
126+
```csharp
127+
using Azure;
128+
using Azure.AI.Inference;
129+
using Azure.Identity;
130+
using System.Text.Json;
131+
using ModelContextProtocol.Client;
132+
using ModelContextProtocol.Protocol.Transport;
133+
using System.Text.Json;
134+
135+
var clientTransport = new StdioClientTransport(new()
136+
{
137+
Name = "Demo Server",
138+
Command = "/workspaces/mcp-for-beginners/03-GettingStarted/02-client/solution/server/bin/Debug/net8.0/server",
139+
Arguments = [],
140+
});
141+
142+
await using var mcpClient = await McpClientFactory.CreateAsync(clientTransport);
143+
```
144+
145+
</details>
146+
147+
123148
Great, for our next step, let's list the capbilities on the server.
124149

125150
### -2 List server capabilities
@@ -175,6 +200,36 @@ Here's what we added:
175200

176201
- Listing resources and tools and printed them. For tools we also list `inputSchema` which we use later.
177202

203+
</details>
204+
205+
<details>
206+
<summary>.NET</summary>
207+
208+
```csharp
209+
async Task<List<ChatCompletionsToolDefinition>> GetMcpTools()
210+
{
211+
Console.WriteLine("Listing tools");
212+
var tools = await mcpClient.ListToolsAsync();
213+
214+
List<ChatCompletionsToolDefinition> toolDefinitions = new List<ChatCompletionsToolDefinition>();
215+
216+
foreach (var tool in tools)
217+
{
218+
Console.WriteLine($"Connected to server with tools: {tool.Name}");
219+
Console.WriteLine($"Tool description: {tool.Description}");
220+
Console.WriteLine($"Tool parameters: {tool.JsonSchema}");
221+
222+
// TODO: convert tool defintion from MCP tool to LLm tool
223+
}
224+
225+
return toolDefinitions;
226+
}
227+
```
228+
229+
In the preceding code we've:
230+
231+
232+
178233
</details>
179234

180235

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Here's the solutions for each runtime:
22

33
- [TypeScript](./typescript/README.md)
4-
- [Python](./python/README.md)
4+
- [Python](./python/README.md)
5+
- [.NET](./dotnet/README.md)

03-GettingStarted/03-llm-client/solution/dotnet/Program.cs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
};
1616

1717
var clientTransport = new StdioClientTransport(new()
18-
{
19-
Name = "Demo Server",
20-
Command = "/workspaces/mcp-for-beginners/03-GettingStarted/02-client/solution/server/bin/Debug/net8.0/server",
21-
Arguments = [],
22-
});
18+
{
19+
Name = "Demo Server",
20+
Command = "/workspaces/mcp-for-beginners/03-GettingStarted/02-client/solution/server/bin/Debug/net8.0/server",
21+
Arguments = [],
22+
});
2323

2424
Console.WriteLine("Setting up stdio transport");
2525

@@ -77,8 +77,6 @@ ChatCompletionsToolDefinition CreateToolDefinition()
7777

7878
async Task<List<ChatCompletionsToolDefinition>> GetMcpTools()
7979
{
80-
81-
8280
Console.WriteLine("Listing tools");
8381
var tools = await mcpClient.ListToolsAsync();
8482

@@ -97,21 +95,10 @@ async Task<List<ChatCompletionsToolDefinition>> GetMcpTools()
9795
Console.WriteLine($"Tool definition: {def}");
9896
toolDefinitions.Add(def);
9997

100-
// foreach (var property in tool.JsonSchema.EnumerateObject())
101-
// {
102-
// Console.WriteLine($"Property: {property.Name}, Type: {property.Value.ValueKind}");
103-
104-
// }
105-
106-
Console.WriteLine($"Properties: {propertiesElement}");
107-
108-
98+
Console.WriteLine($"Properties: {propertiesElement}");
10999
}
110100

111-
// call mcp server
112-
// convert each tool to a function definition
113101
return toolDefinitions;
114-
115102
}
116103

117104
// -1. List tools on mcp server
@@ -152,8 +139,6 @@ async Task<List<ChatCompletionsToolDefinition>> GetMcpTools()
152139
Console.WriteLine($"Tool call {i}: {call.Name} with arguments {call.Arguments}");
153140
//Tool call 0: add with arguments {"a":2,"b":4}
154141

155-
// new Dictionary<string, object?>() { ["a"] = 1, ["b"] = 3 },
156-
157142
var dict = JsonSerializer.Deserialize<Dictionary<string, object>>(call.Arguments);
158143
var result = await mcpClient.CallToolAsync(
159144
call.Name,

0 commit comments

Comments
 (0)