Skip to content

Commit c7c730b

Browse files
authored
Merge branch 'main' into fixes/forecast-sample
2 parents 6a511c0 + 3b26bf8 commit c7c730b

File tree

144 files changed

+5810
-2960
lines changed

Some content is hidden

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

144 files changed

+5810
-2960
lines changed

Directory.Packages.props

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,63 @@
11
<Project>
22
<PropertyGroup>
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4-
<SystemVersion>9.0.3</SystemVersion>
54
<System10Version>10.0.0-preview.2.25163.2</System10Version>
6-
<MicrosoftExtensionsVersion>9.0.3</MicrosoftExtensionsVersion>
75
<MicrosoftExtensionsAIVersion>9.3.0-preview.1.25161.3</MicrosoftExtensionsAIVersion>
86
</PropertyGroup>
7+
8+
<!-- Product dependencies netstandard -->
9+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
10+
<PackageVersion Include="Microsoft.Bcl.Memory" Version="9.0.0" />
11+
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
12+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
13+
<PackageVersion Include="System.IO.Pipelines" Version="8.0.0" />
14+
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
15+
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
16+
</ItemGroup>
17+
18+
<!-- Product dependencies LTS -->
19+
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
20+
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
21+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
22+
<PackageVersion Include="System.IO.Pipelines" Version="8.0.0" />
23+
</ItemGroup>
24+
25+
<!-- Product dependencies .NET 9 -->
26+
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
27+
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
28+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
29+
<PackageVersion Include="System.IO.Pipelines" Version="9.0.0" />
30+
</ItemGroup>
31+
32+
<!-- Product dependencies shared -->
933
<ItemGroup>
10-
<!-- Product dependencies -->
11-
<PackageVersion Include="coverlet.collector" Version="6.0.4">
12-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13-
<PrivateAssets>all</PrivateAssets>
14-
</PackageVersion>
15-
<PackageVersion Include="Microsoft.Bcl.Memory" Version="$(SystemVersion)" />
16-
<PackageVersion Include="Microsoft.Extensions.AI" Version="$(MicrosoftExtensionsAIVersion)" />
1734
<PackageVersion Include="Microsoft.Extensions.AI.Abstractions" Version="$(MicrosoftExtensionsAIVersion)" />
18-
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(MicrosoftExtensionsVersion)" />
19-
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsVersion)" />
35+
<PackageVersion Include="Microsoft.Extensions.AI" Version="$(MicrosoftExtensionsAIVersion)" />
2036
<PackageVersion Include="System.Net.ServerSentEvents" Version="$(System10Version)" />
21-
<PackageVersion Include="System.Text.Json" Version="$(SystemVersion)" />
22-
<PackageVersion Include="System.Threading.Channels" Version="$(SystemVersion)" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
2340

2441
<!-- Build Infra & Packaging -->
2542
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
2643

2744
<!-- Testing dependencies -->
2845
<PackageVersion Include="Anthropic.SDK" Version="5.0.0" />
46+
<PackageVersion Include="coverlet.collector" Version="6.0.4">
47+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
48+
<PrivateAssets>all</PrivateAssets>
49+
</PackageVersion>
2950
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
3051
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="$(MicrosoftExtensionsAIVersion)" />
31-
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsVersion)" />
32-
<PackageVersion Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsVersion)" />
33-
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsVersion)" />
52+
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.3" />
53+
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.3" />
54+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.3" />
55+
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.3" />
56+
<PackageVersion Include="Microsoft.Extensions.Options" Version="9.0.3" />
3457
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
3558
<PackageVersion Include="Moq" Version="4.20.72" />
59+
<PackageVersion Include="OpenTelemetry" Version="1.11.2" />
60+
<PackageVersion Include="OpenTelemetry.Exporter.InMemory" Version="1.11.2" />
3661
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
3762
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.0" />
3863
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />

ModelContextProtocol.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickstartWeatherServer", "
5050
EndProject
5151
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickstartClient", "samples\QuickstartClient\QuickstartClient.csproj", "{0D1552DC-E6ED-4AAC-5562-12F8352F46AA}"
5252
EndProject
53+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModelContextProtocol.AspNetCore", "src\ModelContextProtocol.AspNetCore\ModelContextProtocol.AspNetCore.csproj", "{37B6A5E0-9995-497D-8B43-3BC6870CC716}"
54+
EndProject
5355
Global
5456
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5557
Debug|Any CPU = Debug|Any CPU
@@ -92,6 +94,10 @@ Global
9294
{0D1552DC-E6ED-4AAC-5562-12F8352F46AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
9395
{0D1552DC-E6ED-4AAC-5562-12F8352F46AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
9496
{0D1552DC-E6ED-4AAC-5562-12F8352F46AA}.Release|Any CPU.Build.0 = Release|Any CPU
97+
{37B6A5E0-9995-497D-8B43-3BC6870CC716}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
98+
{37B6A5E0-9995-497D-8B43-3BC6870CC716}.Debug|Any CPU.Build.0 = Debug|Any CPU
99+
{37B6A5E0-9995-497D-8B43-3BC6870CC716}.Release|Any CPU.ActiveCfg = Release|Any CPU
100+
{37B6A5E0-9995-497D-8B43-3BC6870CC716}.Release|Any CPU.Build.0 = Release|Any CPU
95101
EndGlobalSection
96102
GlobalSection(SolutionProperties) = preSolution
97103
HideSolutionNode = FALSE
@@ -107,6 +113,7 @@ Global
107113
{0C6D0512-D26D-63D3-5019-C5F7A657B28C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
108114
{4653EB0C-8FC0-98F4-E9C8-220EDA7A69DF} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
109115
{0D1552DC-E6ED-4AAC-5562-12F8352F46AA} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
116+
{37B6A5E0-9995-497D-8B43-3BC6870CC716} = {A2F1F52A-9107-4BF8-8C3F-2F6670E7D0AD}
110117
EndGlobalSection
111118
GlobalSection(ExtensibilityGlobals) = postSolution
112119
SolutionGuid = {384A3888-751F-4D75-9AE5-587330582D89}

README.md

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,24 @@ It includes a simple echo tool as an example (this is included in the same file
8383
the 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;
8993
using Microsoft.Extensions.Hosting;
94+
using Microsoft.Extensions.Logging;
95+
using ModelContextProtocol.Server;
9096
using 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+
});
93104
builder.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
110121
an `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")]
113124
public 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+
137159
More control is also available, with fine-grained control over configuring the server and how it should handle client requests. For example:
138160

139161
```csharp
140162
using ModelContextProtocol.Protocol.Transport;
141163
using ModelContextProtocol.Protocol.Types;
142164
using ModelContextProtocol.Server;
143-
using Microsoft.Extensions.Logging.Abstractions;
165+
using System.Text.Json;
144166

145167
McpServerOptions 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}'");

samples/AspNetCoreSseServer/AspNetCoreSseServer.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<PublishAot>true</PublishAot>
78
</PropertyGroup>
89

910
<ItemGroup>
1011
<ProjectReference Include="..\..\src\ModelContextProtocol\ModelContextProtocol.csproj" />
12+
<ProjectReference Include="..\..\src\ModelContextProtocol.AspNetCore\ModelContextProtocol.AspNetCore.csproj" />
1113
</ItemGroup>
1214

1315
</Project>

samples/AspNetCoreSseServer/McpEndpointRouteBuilderExtensions.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using ModelContextProtocol;
2-
using AspNetCoreSseServer;
1+
using TestServerWithHosting.Tools;
32

43
var builder = WebApplication.CreateBuilder(args);
5-
builder.Services.AddMcpServer().WithToolsFromAssembly();
4+
builder.Services.AddMcpServer()
5+
.WithTools<EchoTool>()
6+
.WithTools<SampleLlmTool>();
7+
68
var app = builder.Build();
79

8-
app.MapGet("/", () => "Hello World!");
9-
app.MapMcpSse();
10+
app.MapMcp();
1011

1112
app.Run();

samples/AspNetCoreSseServer/Tools/EchoTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace TestServerWithHosting.Tools;
55

66
[McpServerToolType]
7-
public static class EchoTool
7+
public sealed class EchoTool
88
{
99
[McpServerTool, Description("Echoes the input back to the client.")]
1010
public static string Echo(string message)

samples/AspNetCoreSseServer/Tools/SampleLlmTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace TestServerWithHosting.Tools;
88
/// This tool uses dependency injection and async method
99
/// </summary>
1010
[McpServerToolType]
11-
public static class SampleLlmTool
11+
public sealed class SampleLlmTool
1212
{
1313
[McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
1414
public static async Task<string> SampleLLM(

samples/ChatWithTools/ChatWithTools.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<!--
9+
Anthropic SDK isn't AOT compatible yet
10+
<PublishAot>true</PublishAot>
11+
-->
812
</PropertyGroup>
913

1014
<ItemGroup>

samples/QuickstartClient/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using ModelContextProtocol.Client;
66
using ModelContextProtocol.Protocol.Transport;
77

8-
var builder = Host.CreateEmptyApplicationBuilder(settings: null);
8+
var builder = Host.CreateApplicationBuilder(args);
99

1010
builder.Configuration
1111
.AddEnvironmentVariables()

0 commit comments

Comments
 (0)