Skip to content

Commit 50ba747

Browse files
authored
Merge branch 'modelcontextprotocol:main' into main
2 parents f4c7555 + 4fd3ebf commit 50ba747

File tree

126 files changed

+4326
-1842
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+4326
-1842
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ root = true
44
# C# files
55
[*.cs]
66

7+
# Compiler
8+
dotnet_diagnostic.CS1998.severity = suggestion # CS1998: Missing awaits
9+
710
# Code Analysis
811
dotnet_diagnostic.CA1002.severity = none # CA1002: Do not expose generic lists
912
dotnet_diagnostic.CA1031.severity = none # CA1031: Do not catch general exception types

Directory.Packages.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
<PackageVersion Include="Moq" Version="4.20.72" />
5959
<PackageVersion Include="OpenTelemetry" Version="1.11.2" />
6060
<PackageVersion Include="OpenTelemetry.Exporter.InMemory" Version="1.11.2" />
61+
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.11.2" />
62+
<PackageVersion Include="OpenTelemetry.Instrumentation.Http " Version="1.11.0" />
63+
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.11.2" />
64+
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.11.1" />
6165
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
6266
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.0" />
6367
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ To get started writing a client, the `McpClientFactory.CreateAsync` method is us
3131
to a server. Once you have an `IMcpClient`, you can interact with it, such as to enumerate all available tools and invoke tools.
3232

3333
```csharp
34-
var clientTransport = new StdioClientTransport(new()
34+
var clientTransport = new StdioClientTransport(new StdioClientTransportOptions
3535
{
3636
Name = "Everything",
3737
Command = "npx",
@@ -50,7 +50,7 @@ foreach (var tool in await client.ListToolsAsync())
5050
var result = await client.CallToolAsync(
5151
"echo",
5252
new Dictionary<string, object?>() { ["message"] = "Hello MCP!" },
53-
CancellationToken.None);
53+
cancellationToken:CancellationToken.None);
5454

5555
// echo always returns one and only one text content object
5656
Console.WriteLine(result.Content.First(c => c.Type == "text").Text);
@@ -60,7 +60,7 @@ You can find samples demonstrating how to use ModelContextProtocol with an LLM S
6060

6161
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.
6262

63-
Tools can be exposed easily as `AIFunction` instances so that they are immediately usable with `IChatClient`s.
63+
Tools can be easily exposed for immediate use by `IChatClient`s, because `McpClientTool` inherits from `AIFunction`.
6464

6565
```csharp
6666
// Get available functions.
@@ -163,10 +163,10 @@ using System.Text.Json;
163163

164164
McpServerOptions options = new()
165165
{
166-
ServerInfo = new() { Name = "MyServer", Version = "1.0.0" },
167-
Capabilities = new()
166+
ServerInfo = new Implementation() { Name = "MyServer", Version = "1.0.0" },
167+
Capabilities = new ServerCapabilities()
168168
{
169-
Tools = new()
169+
Tools = new ToolsCapability()
170170
{
171171
ListToolsHandler = (request, cancellationToken) =>
172172
Task.FromResult(new ListToolsResult()
@@ -199,7 +199,7 @@ McpServerOptions options = new()
199199
{
200200
if (request.Params.Arguments?.TryGetValue("message", out var message) is not true)
201201
{
202-
throw new McpServerException("Missing required argument 'message'");
202+
throw new McpException("Missing required argument 'message'");
203203
}
204204

205205
return Task.FromResult(new CallToolResponse()
@@ -208,7 +208,7 @@ McpServerOptions options = new()
208208
});
209209
}
210210

211-
throw new McpServerException($"Unknown tool: '{request.Params?.Name}'");
211+
throw new McpException($"Unknown tool: '{request.Params?.Name}'");
212212
},
213213
}
214214
},
@@ -220,7 +220,7 @@ await server.RunAsync();
220220

221221
## Acknowledgements
222222

223-
The starting point for this library was a project called [mcpdotnet](https://github.com/PederHP/mcpdotnet), initiated by [Peder Holdgaard Pederson](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.
223+
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.
224224
225225
## License
226226

samples/AspNetCoreSseServer/AspNetCoreSseServer.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@
1212
<ProjectReference Include="..\..\src\ModelContextProtocol.AspNetCore\ModelContextProtocol.AspNetCore.csproj" />
1313
</ItemGroup>
1414

15+
<ItemGroup>
16+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
17+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
18+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
19+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
20+
</ItemGroup>
21+
1522
</Project>

samples/AspNetCoreSseServer/Program.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
using TestServerWithHosting.Tools;
2+
using OpenTelemetry.Metrics;
3+
using OpenTelemetry.Trace;
4+
using OpenTelemetry;
25

36
var builder = WebApplication.CreateBuilder(args);
47
builder.Services.AddMcpServer()
8+
.WithHttpTransport()
59
.WithTools<EchoTool>()
610
.WithTools<SampleLlmTool>();
711

12+
builder.Services.AddOpenTelemetry()
13+
.WithTracing(b => b.AddSource("*")
14+
.AddAspNetCoreInstrumentation()
15+
.AddHttpClientInstrumentation())
16+
.WithMetrics(b => b.AddMeter("*")
17+
.AddAspNetCoreInstrumentation()
18+
.AddHttpClientInstrumentation())
19+
.WithLogging()
20+
.UseOtlpExporter();
21+
822
var app = builder.Build();
923

1024
app.MapMcp();

samples/AspNetCoreSseServer/Properties/launchSettings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
"dotnetRunMessages": true,
77
"applicationUrl": "http://localhost:3001",
88
"environmentVariables": {
9-
"ASPNETCORE_ENVIRONMENT": "Development"
9+
"ASPNETCORE_ENVIRONMENT": "Development",
10+
"OTEL_SERVICE_NAME": "sse-server",
1011
}
1112
},
1213
"https": {
1314
"commandName": "Project",
1415
"dotnetRunMessages": true,
1516
"applicationUrl": "https://localhost:7133;http://localhost:3001",
1617
"environmentVariables": {
17-
"ASPNETCORE_ENVIRONMENT": "Development"
18+
"ASPNETCORE_ENVIRONMENT": "Development",
19+
"OTEL_SERVICE_NAME": "sse-server",
1820
}
1921
}
2022
}

samples/ChatWithTools/ChatWithTools.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<PackageReference Include="Microsoft.Extensions.AI" />
1616
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
1717
<PackageReference Include="Anthropic.SDK" />
18+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
19+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
1820
</ItemGroup>
1921

2022
<ItemGroup>

samples/ChatWithTools/Program.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,49 @@
33
using Microsoft.Extensions.AI;
44
using OpenAI;
55

6+
using OpenTelemetry;
7+
using OpenTelemetry.Trace;
8+
using Microsoft.Extensions.Logging;
9+
using OpenTelemetry.Logs;
10+
using OpenTelemetry.Metrics;
11+
12+
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
13+
.AddHttpClientInstrumentation()
14+
.AddSource("*")
15+
.AddOtlpExporter()
16+
.Build();
17+
using var metricsProvider = Sdk.CreateMeterProviderBuilder()
18+
.AddHttpClientInstrumentation()
19+
.AddMeter("*")
20+
.AddOtlpExporter()
21+
.Build();
22+
using var loggerFactory = LoggerFactory.Create(builder => builder.AddOpenTelemetry(opt => opt.AddOtlpExporter()));
23+
624
// Connect to an MCP server
725
Console.WriteLine("Connecting client to MCP 'everything' server");
26+
27+
// Create OpenAI client (or any other compatible with IChatClient)
28+
// Provide your own OPENAI_API_KEY via an environment variable.
29+
var openAIClient = new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY")).GetChatClient("gpt-4o-mini");
30+
31+
// Create a sampling client.
32+
using IChatClient samplingClient = openAIClient.AsIChatClient()
33+
.AsBuilder()
34+
.UseOpenTelemetry(loggerFactory: loggerFactory, configure: o => o.EnableSensitiveData = true)
35+
.Build();
36+
837
var mcpClient = await McpClientFactory.CreateAsync(
938
new StdioClientTransport(new()
1039
{
1140
Command = "npx",
1241
Arguments = ["-y", "--verbose", "@modelcontextprotocol/server-everything"],
1342
Name = "Everything",
14-
}));
43+
}),
44+
clientOptions: new()
45+
{
46+
Capabilities = new() { Sampling = new() { SamplingHandler = samplingClient.CreateSamplingHandler() } },
47+
},
48+
loggerFactory: loggerFactory);
1549

1650
// Get all available tools
1751
Console.WriteLine("Tools available:");
@@ -20,13 +54,15 @@
2054
{
2155
Console.WriteLine($" {tool}");
2256
}
57+
2358
Console.WriteLine();
2459

25-
// Create an IChatClient. (This shows using OpenAIClient, but it could be any other IChatClient implementation.)
26-
// Provide your own OPENAI_API_KEY via an environment variable.
27-
using IChatClient chatClient =
28-
new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY")).GetChatClient("gpt-4o-mini").AsIChatClient()
29-
.AsBuilder().UseFunctionInvocation().Build();
60+
// Create an IChatClient that can use the tools.
61+
using IChatClient chatClient = openAIClient.AsIChatClient()
62+
.AsBuilder()
63+
.UseFunctionInvocation()
64+
.UseOpenTelemetry(loggerFactory: loggerFactory, configure: o => o.EnableSensitiveData = true)
65+
.Build();
3066

3167
// Have a conversation, making all tools available to the LLM.
3268
List<ChatMessage> messages = [];

samples/EverythingServer/EverythingServer.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Extensions.Hosting" />
12+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
13+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
14+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
1215
</ItemGroup>
1316

1417
<ItemGroup>

0 commit comments

Comments
 (0)