forked from microsoft/mcp-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_example_csharp.cs
More file actions
150 lines (131 loc) · 4.75 KB
/
client_example_csharp.cs
File metadata and controls
150 lines (131 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
using System.Text.Json;
/**
* Complete C# MCP Client Example
*
* This client demonstrates how to:
* 1. Connect to an MCP server using stdio transport
* 2. List available tools and resources
* 3. Call calculator tools
* 4. Handle responses from the server
*/
Console.WriteLine("🚀 Starting MCP C# Client...");
try
{
// Create configuration builder
var builder = Host.CreateApplicationBuilder(args);
builder.Configuration
.AddEnvironmentVariables()
.AddUserSecrets<Program>();
// Create stdio transport to connect to the MCP server
var clientTransport = new StdioClientTransport(new()
{
Name = "Calculator Server",
Command = "dotnet",
Arguments = ["run", "--project", "../01-first-server/solution/csharp/calculator-server.csproj"],
});
Console.WriteLine("📡 Connecting to MCP server...");
// Create and connect the MCP client
await using var mcpClient = await McpClientFactory.CreateAsync(clientTransport);
Console.WriteLine("✅ Connected to MCP server successfully!");
// List available tools
Console.WriteLine("\n📋 Listing available tools:");
var tools = await mcpClient.ListToolsAsync();
foreach (var tool in tools)
{
Console.WriteLine($" - {tool.Name}: {tool.Description}");
}
// Test calculator operations
Console.WriteLine("\n🧮 Testing Calculator Operations:");
// Addition
var addResult = await mcpClient.CallToolAsync(
"Add",
new Dictionary<string, object?>() { ["a"] = 5, ["b"] = 3 },
cancellationToken: CancellationToken.None
);
Console.WriteLine($"Add 5 + 3 = {ExtractTextResult(addResult)}");
// Subtraction
var subtractResult = await mcpClient.CallToolAsync(
"Subtract",
new Dictionary<string, object?>() { ["a"] = 10, ["b"] = 4 },
cancellationToken: CancellationToken.None
);
Console.WriteLine($"Subtract 10 - 4 = {ExtractTextResult(subtractResult)}");
// Multiplication
var multiplyResult = await mcpClient.CallToolAsync(
"Multiply",
new Dictionary<string, object?>() { ["a"] = 6, ["b"] = 7 },
cancellationToken: CancellationToken.None
);
Console.WriteLine($"Multiply 6 × 7 = {ExtractTextResult(multiplyResult)}");
// Division
var divideResult = await mcpClient.CallToolAsync(
"Divide",
new Dictionary<string, object?>() { ["a"] = 20, ["b"] = 4 },
cancellationToken: CancellationToken.None
);
Console.WriteLine($"Divide 20 ÷ 4 = {ExtractTextResult(divideResult)}");
// Help
var helpResult = await mcpClient.CallToolAsync(
"Help",
new Dictionary<string, object?>(),
cancellationToken: CancellationToken.None
);
Console.WriteLine($"\n📖 Help Information:");
Console.WriteLine(ExtractTextResult(helpResult));
// List resources if available
try
{
Console.WriteLine("\n📄 Listing available resources:");
var resources = await mcpClient.ListResourcesAsync();
foreach (var resource in resources)
{
Console.WriteLine($" - {resource.Name}: {resource.Description}");
}
}
catch (Exception ex)
{
Console.WriteLine(" No resources available or error listing resources: " + ex.Message);
}
Console.WriteLine("\n✨ Client operations completed successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"❌ Error running MCP client: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
}
/// <summary>
/// Extracts the text result from a tool call response object.
/// </summary>
/// <param name="result">The result object, which may contain text content or other data.</param>
/// <returns>
/// A string containing the extracted text if found, a serialized representation of the result if no text is found,
/// or a fallback string if serialization fails.
/// </returns>
static string ExtractTextResult(object result)
{
try
{
if (result is IEnumerable<object> contentList)
{
foreach (var content in contentList)
{
if (content is IDictionary<string, object> contentDict &&
contentDict.TryGetValue("text", out var text))
{
return text?.ToString() ?? "No text content";
}
}
}
// Fallback: try to serialize the entire result
return JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true });
}
catch
{
return result?.ToString() ?? "No result";
}
}