Repository navigation
Add WithMcpServer extension method for fluent MCP server endpoint configuration - #14618
Merged
David Fowler (davidfowl) merged 4 commits intoFeb 23, 2026
Conversation
Copilot
AI
changed the title
[WIP] Add extension method for MCP server integration
Add Feb 23, 2026
WithMcpServer extension method for fluent MCP server endpoint configuration
Co-authored-by: davidfowl <[email protected]>
David Fowler (davidfowl)
force-pushed
the
copilot/add-extension-method-for-mcp-server
branch
from
February 23, 2026 05:50
5316735 to
727a7ce
Compare
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 14618Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 14618" |
- Move WithMcpServer from ResourceBuilderExtensions to new
McpServerResourceBuilderExtensions class
- Make endpointName optional, defaulting to https then http fallback
(consistent with WithHttpHealthCheck pattern)
- Add [Experimental("ASPIREMCP001")] and [EndpointName] attributes
- Update PostgresBuilderExtensions to use new extension method
- Add tests for default endpoint selection and fallback behavior
Co-authored-by: Copilot <[email protected]>
David Fowler (davidfowl)
requested review from
James Newton-King (JamesNK) and
Sébastien Ros (sebastienros)
February 23, 2026 06:34
David Fowler (davidfowl)
marked this pull request as ready for review
February 23, 2026 06:34
David Fowler (davidfowl)
requested a review
from Mitch Denny (mitchdenny)
as a code owner
February 23, 2026 06:34
Co-authored-by: David Fowler <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new experimental fluent API for annotating resources with an MCP (Model Context Protocol) server endpoint, and updates PostgreSQL’s MCP helper to use it.
Changes:
- Introduces
McpServerResourceBuilderExtensions.WithMcpServer<T>()to addMcpServerEndpointAnnotationbased on an optional endpoint name + optional path (default"/mcp"). - Updates PostgreSQL
WithPostgresMcpto callWithMcpServer("/sse", ...)instead of directly creating the annotation. - Adds unit tests covering default endpoint selection (https → http), default/custom/null path behavior, and fluent chaining.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/Aspire.Hosting.Tests/WithMcpServerTests.cs | Adds test coverage for the new WithMcpServer extension behavior. |
| src/Aspire.Hosting/ResourceBuilderExtensions.cs | No functional change (trailing whitespace/newline). |
| src/Aspire.Hosting/McpServerResourceBuilderExtensions.cs | Adds the new experimental WithMcpServer extension method and its endpoint/path resolution behavior. |
| src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs | Switches PostgreSQL MCP setup to use WithMcpServer, adding a warning suppression for the experimental API. |
Comments suppressed due to low confidence (3)
src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs:5
#pragma warning disable ASPIREMCP001is applied to the entire file, which can unintentionally suppress future experimental-API warnings in unrelated code. Consider scoping the suppression to the smallest region (e.g., around the single.WithMcpServer(...)call or withinWithPostgresMcp) and restoring the warning afterward.
#pragma warning disable ASPIREMCP001
src/Aspire.Hosting/McpServerResourceBuilderExtensions.cs:106
WithMcpServerre-implements the same URL/path normalization logic that already exists inMcpServerEndpointAnnotation.FromEndpoint. Duplicating this logic increases the chance the behaviors diverge over time; consider resolving the endpoint name first, then delegating toMcpServerEndpointAnnotation.FromEndpoint(resolvedEndpointName, path)for the resolver portion.
return builder.WithAnnotation(new McpServerEndpointAnnotation(async (resource, cancellationToken) =>
{
var endpoints = resource.GetEndpoints();
EndpointReference? endpoint = null;
if (endpointName is not null)
{
endpoint = endpoints.FirstOrDefault(e => string.Equals(e.EndpointName, endpointName, StringComparisons.EndpointAnnotationName));
if (endpoint is null)
{
throw new DistributedApplicationException(
$"Could not create MCP server for resource '{resource.Name}' as no endpoint was found with name '{endpointName}'.");
}
}
else
{
foreach (var scheme in s_httpSchemes)
{
endpoint = endpoints.FirstOrDefault(e => string.Equals(e.EndpointName, scheme, StringComparisons.EndpointAnnotationName));
if (endpoint is not null)
{
break;
}
}
if (endpoint is null)
{
throw new DistributedApplicationException(
$"Could not create MCP server for resource '{resource.Name}' as no endpoint was found matching one of the specified names: {string.Join(", ", s_httpSchemes)}");
}
}
if (!endpoint.Exists)
{
return null;
}
var baseUrl = await endpoint.GetValueAsync(cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(baseUrl))
{
return null;
}
if (string.IsNullOrEmpty(path))
{
return new Uri(baseUrl, UriKind.Absolute);
}
var normalizedPath = path;
if (!normalizedPath.StartsWith("/", StringComparison.Ordinal))
{
normalizedPath = "/" + normalizedPath;
}
var combined = baseUrl.TrimEnd('/') + normalizedPath;
return new Uri(combined, UriKind.Absolute);
}));
src/Aspire.Hosting/McpServerResourceBuilderExtensions.cs:79
- The error paths that throw
DistributedApplicationExceptionwhen (a) an explicitendpointNamecan’t be found or (b) no default http/https endpoint names exist aren’t covered by tests. Adding unit tests for these failures would lock down the intended behavior and prevent regressions in exception handling/messages.
if (endpoint is null)
{
throw new DistributedApplicationException(
$"Could not create MCP server for resource '{resource.Name}' as no endpoint was found with name '{endpointName}'.");
}
}
else
{
foreach (var scheme in s_httpSchemes)
{
endpoint = endpoints.FirstOrDefault(e => string.Equals(e.EndpointName, scheme, StringComparisons.EndpointAnnotationName));
if (endpoint is not null)
{
break;
}
}
if (endpoint is null)
{
throw new DistributedApplicationException(
$"Could not create MCP server for resource '{resource.Name}' as no endpoint was found matching one of the specified names: {string.Join(", ", s_httpSchemes)}");
}
James Newton-King (JamesNK)
approved these changes
Feb 23, 2026
Contributor
🎬 CLI E2E Test RecordingsThe following terminal recordings are available for commit
📹 Recordings uploaded automatically from CI run #22295915375 |
Sébastien Ros (sebastienros)
approved these changes
Feb 23, 2026
David Fowler (davidfowl)
deleted the
copilot/add-extension-method-for-mcp-server
branch
February 23, 2026 15:45
Copilot AI
added a commit
that referenced
this pull request
Mar 10, 2026
…onfiguration (#14618) * Add WithMcpServer extension method to ResourceBuilderExtensions Co-authored-by: davidfowl <[email protected]> * Move WithMcpServer to dedicated class with improved API - Move WithMcpServer from ResourceBuilderExtensions to new McpServerResourceBuilderExtensions class - Make endpointName optional, defaulting to https then http fallback (consistent with WithHttpHealthCheck pattern) - Add [Experimental("ASPIREMCP001")] and [EndpointName] attributes - Update PostgresBuilderExtensions to use new extension method - Add tests for default endpoint selection and fallback behavior Co-authored-by: Copilot <[email protected]> * Apply suggestions from code review Co-authored-by: David Fowler <[email protected]> * Update src/Aspire.Hosting/McpServerResourceBuilderExtensions.cs --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: davidfowl <[email protected]> Co-authored-by: David Fowler <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: James Newton-King <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds a
WithMcpServerextension method for configuring MCP (Model Context Protocol) server endpoints on resources, following established Aspire API patterns.Changes
McpServerResourceBuilderExtensionsclass withWithMcpServer<T>()extension method in its own file (consistent with other domain-specific extensions)endpointNameis optional — when omitted, defaults to the first HTTPS endpoint, falling back to HTTP (same pattern asWithHttpHealthCheck)pathdefaults to"/mcp"— can be customized or set tonullfor no path[Experimental("ASPIREMCP001")]since MCP support is still evolving[EndpointName]attribute on theendpointNameparameter for consistency withWithEndpoint,WithHttpEndpoint, etc.PostgresBuilderExtensionsto use the newWithMcpServerinstead of directly constructingMcpServerEndpointAnnotationUsage
Checklist
McpServerResourceBuilderExtensions.WithMcpServer<T>()(experimental)