Skip to content

Commit cc4cace

Browse files
committed
feat: add McpToolExtension(draft)
1 parent a496e59 commit cc4cace

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

Directory.Packages.props

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<PropertyGroup>
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
<CentralPackageFloatingVersionsEnabled>true</CentralPackageFloatingVersionsEnabled>
5-
<!-- https://learn.microsoft.com/en-us/nuget/concepts/auditing-packages -->
6-
<NuGetAudit>true</NuGetAudit>
5+
<!-- https://learn.microsoft.com/en-us/nuget/concepts/auditing-packages -->
6+
<NuGetAudit>true</NuGetAudit>
77
<NuGetAuditMode>all</NuGetAuditMode>
88
<!-- https://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu1901-nu1904 -->
99
<WarningsAsErrors>NU1901;NU1902;NU1903;NU1904</WarningsAsErrors>
@@ -16,7 +16,8 @@
1616
<PackageVersion Condition="'$(TargetFramework)'=='net9.0'" Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="$(AspNet9PackageVersion)" />
1717
<PackageVersion Condition="'$(TargetFramework)'=='net10.0'" Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="$(AspNet10PackageVersion)" />
1818
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="$(AspNet10PackageVersion)" />
19+
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.1.0-preview.10" />
1920
<PackageVersion Include="Scalar.AspNetCore" Version="2.1.17" />
2021
<PackageVersion Include="WeihanLi.Common" Version="1.0.77" />
2122
</ItemGroup>
22-
</Project>
23+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Weihan Li. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
namespace WeihanLi.Web.Extensions.Samples;
5+
6+
public interface IMcpToolEndpointMetadata
7+
{
8+
string Name { get; set; }
9+
string Description { get; set; }
10+
}
11+
12+
public class McpToolEndpointMetadata : IMcpToolEndpointMetadata
13+
{
14+
public string Name { get; set; } = string.Empty;
15+
public string Description { get; set; } = string.Empty;
16+
}
17+
18+
public static class McpToolExtension
19+
{
20+
public static IEndpointConventionBuilder AsMcpTool<TBuilder>(this TBuilder builder, Action<McpToolEndpointMetadata>? toolConfigure = null)
21+
where TBuilder : IEndpointConventionBuilder
22+
{
23+
ArgumentNullException.ThrowIfNull(builder);
24+
var metadata = new McpToolEndpointMetadata();
25+
toolConfigure?.Invoke(metadata);
26+
builder.Add(c =>
27+
{
28+
if (string.IsNullOrEmpty(metadata.Name))
29+
{
30+
metadata.Name = c.DisplayName!;
31+
}
32+
if (string.IsNullOrEmpty(metadata.Description))
33+
{
34+
metadata.Description = c.DisplayName!;
35+
}
36+
c.Metadata.Add(metadata);
37+
});
38+
return builder;
39+
}
40+
}

samples/WeihanLi.Web.Extensions.Samples/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
options.UserIdFactory = static context => $"{context.GetUserIP()}";
8888
});
8989

90-
// builder.Services.AddEndpointsApiExplorer();
90+
builder.Services.AddEndpointsApiExplorer();
9191
builder.Services.AddOpenApi();
9292

9393
builder.Host.UseFluentAspectsServiceProviderFactory(options =>

samples/WeihanLi.Web.Extensions.Samples/WeihanLi.Web.Extensions.Samples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
14+
<PackageReference Include="ModelContextProtocol.AspNetCore" />
1415
<PackageReference Include="Scalar.AspNetCore" />
1516
</ItemGroup>
1617
</Project>

0 commit comments

Comments
 (0)