Skip to content

Commit ac14bb2

Browse files
committed
Fix build issues
1 parent 0bd80e6 commit ac14bb2

File tree

9 files changed

+92
-77
lines changed

9 files changed

+92
-77
lines changed

samples/AspNetCoreSseServer/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using OpenTelemetry.Metrics;
33
using OpenTelemetry.Trace;
44
using OpenTelemetry;
5+
using ModelContextProtocol.AspNetCore;
56

67
var builder = WebApplication.CreateBuilder(args);
78
builder.Services.AddMcpServer()
Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// filepath: c:\Users\ddelimarsky\source\csharp-sdk-anm\src\ModelContextProtocol.AspNetCore\Auth\McpAuthorizationExtensions.cs
21
using Microsoft.AspNetCore.Authentication;
32
using Microsoft.AspNetCore.Builder;
43
using Microsoft.AspNetCore.Http;
@@ -31,68 +30,4 @@ public static AuthenticationBuilder AddMcpAuthorization(
3130
"MCP Authentication",
3231
configureOptions ?? (options => { }));
3332
}
34-
35-
/// <summary>
36-
/// Maps the resource metadata endpoint for MCP OAuth authorization.
37-
/// </summary>
38-
/// <param name="endpoints">The endpoint route builder.</param>
39-
/// <param name="pattern">The route pattern.</param>
40-
/// <param name="configure">An action to configure the resource metadata.</param>
41-
/// <returns>An endpoint convention builder for further configuration.</returns>
42-
public static IEndpointConventionBuilder MapMcpResourceMetadata(
43-
this IEndpointRouteBuilder endpoints,
44-
string pattern = "/.well-known/oauth-protected-resource",
45-
Action<ProtectedResourceMetadata>? configure = null)
46-
{
47-
var metadataService = endpoints.ServiceProvider.GetRequiredService<ResourceMetadataService>();
48-
49-
if (configure != null)
50-
{
51-
metadataService.ConfigureMetadata(configure);
52-
}
53-
54-
return endpoints.MapGet(pattern, async (HttpContext context) =>
55-
{
56-
var metadata = metadataService.GetMetadata();
57-
58-
// Set default resource if not set
59-
if (metadata.Resource == null)
60-
{
61-
var request = context.Request;
62-
var hostString = request.Host.Value;
63-
var scheme = request.Scheme;
64-
metadata.Resource = new Uri($"{scheme}://{hostString}");
65-
}
66-
67-
return Results.Json(metadata);
68-
})
69-
.AllowAnonymous()
70-
.WithDisplayName("MCP Resource Metadata");
71-
}
7233
}
73-
74-
/// <summary>
75-
/// Service for managing MCP resource metadata.
76-
/// </summary>
77-
public class ResourceMetadataService
78-
{
79-
private readonly ProtectedResourceMetadata _metadata = new();
80-
81-
/// <summary>
82-
/// Configures the resource metadata.
83-
/// </summary>
84-
/// <param name="configure">Configuration action.</param>
85-
public void ConfigureMetadata(Action<ProtectedResourceMetadata> configure)
86-
{
87-
configure(_metadata);
88-
}
89-
90-
/// <summary>
91-
/// Gets the resource metadata.
92-
/// </summary>
93-
/// <returns>The resource metadata.</returns>
94-
public ProtectedResourceMetadata GetMetadata()
95-
{
96-
return _metadata;
97-
}
98-
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.AspNetCore.Http;
2+
using ModelContextProtocol.AspNetCore.Auth;
3+
4+
namespace ModelContextProtocol.AspNetCore;
5+
6+
public static partial class McpEndpointRouteBuilderExtensions
7+
{
8+
// This class handles the resource metadata endpoint in an AOT-compatible way
9+
private sealed class ResourceMetadataEndpointHandler
10+
{
11+
private readonly ResourceMetadataService _resourceMetadataService;
12+
13+
public ResourceMetadataEndpointHandler(ResourceMetadataService resourceMetadataService)
14+
{
15+
_resourceMetadataService = resourceMetadataService;
16+
}
17+
18+
public Task HandleRequest(HttpContext httpContext)
19+
{
20+
var result = _resourceMetadataService.HandleResourceMetadataRequest(httpContext);
21+
return result.ExecuteAsync(httpContext);
22+
}
23+
}
24+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.AspNetCore.Http;
2+
using ModelContextProtocol.Auth;
3+
using ModelContextProtocol.Utils.Json;
4+
5+
namespace ModelContextProtocol.AspNetCore.Auth;
6+
7+
/// <summary>
8+
/// Service for managing MCP resource metadata.
9+
/// </summary>
10+
public class ResourceMetadataService
11+
{
12+
private readonly ProtectedResourceMetadata _metadata = new();
13+
14+
/// <summary>
15+
/// Configures the resource metadata.
16+
/// </summary>
17+
/// <param name="configure">Configuration action.</param>
18+
public void ConfigureMetadata(Action<ProtectedResourceMetadata> configure)
19+
{
20+
configure(_metadata);
21+
}
22+
23+
/// <summary>
24+
/// Gets the resource metadata.
25+
/// </summary>
26+
/// <returns>The resource metadata.</returns>
27+
public ProtectedResourceMetadata GetMetadata()
28+
{
29+
return _metadata;
30+
}
31+
32+
/// <summary>
33+
/// Handles the resource metadata request.
34+
/// </summary>
35+
/// <param name="context">The HTTP context.</param>
36+
/// <returns>An IResult containing the resource metadata.</returns>
37+
public IResult HandleResourceMetadataRequest(HttpContext context)
38+
{
39+
var metadata = GetMetadata();
40+
41+
// Set default resource if not set
42+
if (metadata.Resource == null)
43+
{
44+
var request = context.Request;
45+
var hostString = request.Host.Value;
46+
var scheme = request.Scheme;
47+
metadata.Resource = new Uri($"{scheme}://{hostString}");
48+
}
49+
50+
return Results.Json(metadata, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ProtectedResourceMetadata)));
51+
}
52+
}

src/ModelContextProtocol.AspNetCore/McpEndpointRouteBuilderExtensions.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Http;
23
using Microsoft.AspNetCore.Http.Metadata;
34
using Microsoft.AspNetCore.Routing;
45
using Microsoft.Extensions.DependencyInjection;
5-
using ModelContextProtocol.AspNetCore;
66
using ModelContextProtocol.AspNetCore.Auth;
77
using ModelContextProtocol.Auth;
88
using ModelContextProtocol.Protocol.Messages;
99
using System.Diagnostics.CodeAnalysis;
1010

11-
namespace Microsoft.AspNetCore.Builder;
11+
namespace ModelContextProtocol.AspNetCore;
1212

1313
/// <summary>
1414
/// Provides extension methods for <see cref="IEndpointRouteBuilder"/> to add MCP endpoints.
1515
/// </summary>
16-
public static class McpEndpointRouteBuilderExtensions
16+
public static partial class McpEndpointRouteBuilderExtensions
1717
{
1818
/// <summary>
1919
/// Sets up endpoints for handling MCP Streamable HTTP transport.
@@ -59,8 +59,13 @@ public static IEndpointConventionBuilder MapMcp(this IEndpointRouteBuilder endpo
5959
// Authorization is configured, so automatically map the OAuth protected resource endpoint
6060
var resourceMetadataService = endpoints.ServiceProvider.GetRequiredService<ResourceMetadataService>();
6161

62-
// Map the OAuth protected resource endpoint
63-
endpoints.MapMcpResourceMetadata("/.well-known/oauth-protected-resource");
62+
// Use an AOT-compatible approach with a statically compiled RequestDelegate
63+
var handler = new ResourceMetadataEndpointHandler(resourceMetadataService);
64+
65+
sseGroup.MapGet("/.well-known/oauth-protected-resource", handler.HandleRequest)
66+
.WithMetadata(new ProducesResponseTypeMetadata(StatusCodes.Status200OK, contentTypes: ["application/json"]))
67+
.AllowAnonymous()
68+
.WithDisplayName("MCP Resource Metadata");
6469

6570
// Apply authorization to MCP endpoints
6671
mcpGroup.RequireAuthorization("McpAuth");

tests/ModelContextProtocol.AspNetCore.Tests/MapMcpSseTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.AspNetCore.Builder;
2-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.DependencyInjection;
32
using ModelContextProtocol.Client;
43

54
namespace ModelContextProtocol.AspNetCore.Tests;

tests/ModelContextProtocol.AspNetCore.Tests/MapMcpStreamableHttpTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.AspNetCore.Builder;
2-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.DependencyInjection;
32

43
namespace ModelContextProtocol.AspNetCore.Tests;
54

tests/ModelContextProtocol.AspNetCore.Tests/MapMcpTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.AspNetCore.Builder;
2-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Http;
32
using Microsoft.Extensions.DependencyInjection;
43
using ModelContextProtocol.AspNetCore.Tests.Utils;
54
using ModelContextProtocol.Client;

tests/ModelContextProtocol.TestSseServer/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Connections;
2+
using ModelContextProtocol.AspNetCore;
23
using ModelContextProtocol.Protocol.Types;
34
using ModelContextProtocol.Server;
45
using ModelContextProtocol.Utils.Json;

0 commit comments

Comments
 (0)