Skip to content

Commit 57eb02f

Browse files
authored
Merge pull request #47 from WeihanLi/dev
2.2.0 preview 2
2 parents d9a6ea9 + 9c38de9 commit 57eb02f

File tree

8 files changed

+72
-8
lines changed

8 files changed

+72
-8
lines changed

Directory.Packages.props

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
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>
10-
<AspNet8PackageVersion>8.0.14</AspNet8PackageVersion>
11-
<AspNet9PackageVersion>9.0.3</AspNet9PackageVersion>
12-
<AspNet10PackageVersion>10.0.0-preview.3.25172.1</AspNet10PackageVersion>
10+
<AspNet8PackageVersion>8.0.16</AspNet8PackageVersion>
11+
<AspNet9PackageVersion>9.0.5</AspNet9PackageVersion>
12+
<AspNet10PackageVersion>10.0.0-preview.4.25258.110</AspNet10PackageVersion>
1313
</PropertyGroup>
1414
<ItemGroup>
1515
<PackageVersion Condition="'$(TargetFramework)'=='net8.0'" Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="$(AspNet8PackageVersion)" />
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="Scalar.AspNetCore" Version="2.1.17" />
20-
<PackageVersion Include="WeihanLi.Common" Version="1.0.77" />
19+
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.2.0-preview.1" />
20+
<PackageVersion Include="Scalar.AspNetCore" Version="2.4.3" />
21+
<PackageVersion Include="WeihanLi.Common" Version="1.0.78" />
2122
</ItemGroup>
2223
</Project>

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ ASP.NET Core Web extensions
44

55
[![WeihanLi.Web.Extensions](https://img.shields.io/nuget/v/WeihanLi.Web.Extensions)](https://www.nuget.org/packages/WeihanLi.Web.Extensions/)
66

7+
[![WeihanLi.Web.Extensions](https://img.shields.io/nuget/vpre/WeihanLi.Web.Extensions)](https://www.nuget.org/packages/WeihanLi.Web.Extensions/absoluteLatest)
8+
79
[![Azure Pipelines Build Status](https://weihanli.visualstudio.com/Pipelines/_apis/build/status/WeihanLi.WeihanLi.Web.Extensions?branchName=dev)](https://weihanli.visualstudio.com/Pipelines/_build/latest?definitionId=19&branchName=dev)
810

911
[![Github Actions Build Status](https://github.com/WeihanLi/WeihanLi.Web.Extensions/workflows/dotnetcore/badge.svg)](https://github.com/WeihanLi/WeihanLi.Web.Extensions/actions?query=workflow%3Adotnetcore)
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: 18 additions & 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 =>
@@ -101,6 +101,11 @@
101101
)
102102
);
103103

104+
builder.Services.AddMcpServer()
105+
.WithToolsFromAssembly()
106+
.WithHttpTransport()
107+
;
108+
104109
var app = builder.Build();
105110

106111
app.MapRuntimeInfo().ShortCircuit();
@@ -185,13 +190,25 @@
185190
// });
186191

187192
app.MapConfigInspector()
193+
.AsMcpTool(tool =>
194+
{
195+
tool.Description = "Get configurations";
196+
})
188197
// .RequireAuthorization(x => x
189198
// .AddAuthenticationSchemes("ApiKey")
190199
// .RequireAuthenticatedUser()
191200
// )
192201
;
193202
app.MapControllers();
194203

204+
app.MapGet("/mcp-tools", (EndpointDataSource endpointDataSource) =>
205+
{
206+
var tools = endpointDataSource.Endpoints.Where(x => x.Metadata.Any(m => m is McpToolEndpointMetadata))
207+
.Select(x => x.Metadata.OfType<McpToolEndpointMetadata>().First())
208+
.ToArray();
209+
return tools;
210+
});
211+
195212
await app.RunAsync();
196213

197214

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>

src/WeihanLi.Web.Extensions/Authentication/BasicAuthentication/BasicAuthenticationDefaults.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
namespace WeihanLi.Web.Authentication.BasicAuthentication;
5+
56
public static class BasicAuthenticationDefaults
67
{
78
public const string AuthenticationScheme = "Basic";

src/WeihanLi.Web.Extensions/Authentication/BasicAuthentication/BasicAuthenticationHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using AuthenticateResult = Microsoft.AspNetCore.Authentication.AuthenticateResult;
99

1010
namespace WeihanLi.Web.Authentication.BasicAuthentication;
11+
1112
public sealed class BasicAuthenticationHandler : AuthenticationHandler<BasicAuthenticationOptions>
1213
{
1314
public BasicAuthenticationHandler(IOptionsMonitor<BasicAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder)

src/WeihanLi.Web.Extensions/Authentication/BasicAuthentication/BasicAuthenticationOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
namespace WeihanLi.Web.Authentication.BasicAuthentication;
5+
56
public sealed class BasicAuthenticationOptions : AuthenticationSchemeOptions
67
{
78
public string? UserName { get; set; }

0 commit comments

Comments
 (0)