Skip to content

Add Razor component hosting for MCP Apps - #70303

Draft
javiercn wants to merge 1 commit into
mainfrom
javiercn-components-mcp-implementation
Draft

javiercn wants to merge 1 commit into
mainfrom
javiercn-components-mcp-implementation

Conversation

@javiercn

@javiercn javiercn commented Nov 4, 2026

Copy link
Copy Markdown
Member

Add Razor component hosting for MCP Apps

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Add stateless Razor component forms and action handling over standard MCP Apps.

Description

Adds the standalone Microsoft.AspNetCore.Components.Mcp package so applications can expose ordinary Razor component forms through MCP Apps, using the C# MCP SDK over Streamable HTTP.

  • Opening calls, resource shell renders, and form actions own fresh async execution scopes. Singleton SDK registrations remain catalog adapters; the invoker, host, components, navigation, form mapper, and logical request context are scoped. Completion and CLOSE wait for execution cleanup.
  • App.razor owns the document and explicitly declares <script src="blazor.mcp.js"></script>. The host cascades McpComponentRequestContext; IsShellRendering lets the root defer required-data content during standard resources/read without weakening full-render validation. The packaged static web asset is served at the application asset root, including an HTTP PathBase, without an old _content alias or hidden script injection.
  • Supports standard named forms, EditForm, SupplyParameterFromForm, form-mapping scopes, binding, server validation, native constraint validation, submitter overrides, and URL-encoded/multipart input semantics. Internal navigation resolves server-side to final HTML without replaying POST mutations.
  • Existing SDK tools can be associated by name. Their primitive, schema, filters, content, metadata, and errors are preserved. Successful filtered opening results produce serialized initial-parameter snapshots; dispatch reconstructs fresh values without rerunning the business tool.
  • Adds focused managed coverage and a dedicated MSTest/Microsoft.Testing.Platform suite using Components.Testing and C# Playwright with a real standard Apps consumer and nested iframe.

The bounded protocol view registry retains opaque capabilities, principal associations, serialized initial parameters, and cancellation/completion bookkeeping separately from the scoped execution pipeline. Application business persistence belongs in application-owned singleton services or storage.

Scope is forms/actions, not general HTTP SSR parity. Streaming, session/TempData, broad navigation/history/focus/initializer parity, arbitrary remote @onclick, producer circuits, interactive render modes, and JavaScript interop are excluded. ConfigureBrowser is a no-op; MCP-owned browser options apply. CacheView is non-caching. Components.Endpoints and lower-level component assemblies are unchanged. Other SDK transports are not claimed as validated.

N/A — no linked originating issue. Separate API proposal filing is deferred at the author's request; this PR does not claim API approval. Approval covering the final API shape is still required before RTM inclusion.

Validation

Commands were run from the repository root after . .\activate.ps1; test runs used ASPNETCORE_TEST_LOG_MAXPATH=32767.

Command Actual result
dotnet test src\Components\Mcp\test\Microsoft.AspNetCore.Components.Mcp.Tests.csproj --no-restore --disable-build-servers -nr:false -p:UseIisNativeAssets=false --logger "trx;LogFileName=mcp-managed-shell.trx" -v:q Passed: 48, failed: 0, skipped: 0; independently replayed.
dotnet build src\Components\Mcp\testassets\Components.McpTestApp.E2E.Tests\Components.McpTestApp.E2E.Tests.csproj --no-restore --disable-build-servers -nr:false -p:UseIisNativeAssets=false -v:q Passed; 0 warnings/errors.
dotnet run --project src\Components\Mcp\testassets\Components.McpTestApp.E2E.Tests\Components.McpTestApp.E2E.Tests.csproj --no-build -- --minimum-expected-tests 40 --report-trx --report-trx-filename mcp-shell-all.trx Passed: 40, failed: 0, skipped: 0; independently replayed.
npm run build --workspace @microsoft/aspnetcore-components-mcp Passed; Debug and Release bundles.
npm run build --workspace @microsoft/aspnetcore-components-mcp-test-consumer Passed; Debug and Release bundles.
node node_modules\typescript\bin\tsc --noEmit -p src\Components\Mcp\src\JS\tsconfig.json Passed.
node node_modules\typescript\bin\tsc --noEmit -p src\Components\Mcp\testassets\Components.McpTestApp\JS\tsconfig.json Passed.
dotnet build src\Components\Mcp\src\Microsoft.AspNetCore.Components.Mcp.csproj -c Release --no-restore --disable-build-servers -nr:false -p:UseIisNativeAssets=false -v:q Passed; 0 warnings/errors.
dotnet pack src\Components\Mcp\src\Microsoft.AspNetCore.Components.Mcp.csproj -c Release --no-build --no-restore --disable-build-servers -nr:false -p:UseIisNativeAssets=false -o <artifact-directory> -v:q Passed after matching Release build.
.\eng\scripts\CodeCheck.ps1 Passed; 0 errors.
git diff --cached --check, git diff --exit-code, git ls-files --others --exclude-standard Passed/empty for the reviewed staged snapshot.

A separate PackageReference-only consumer restored the actual produced package into a fresh isolated cache, then built and published successfully. A real C# SDK client probe passed against its built Development app, published Production app, and published app under /app: blazor.mcp.js returned HTTP 200 (436,259 bytes), the old prefix returned HTTP 404, the resource contained the authored shell/script, and discovery/read/open/binding/POST/render/CLOSE succeeded. Published JavaScript matched the current Release bundle. This package-consumption probe is additional validation; its disposable harness is not repository source.

Permanent tests include authored script fetch/initialization exactly once across document updates in both DOM modes, script omission, required typed data deferred only during shell rendering, real native form inputs and effects, cancellation/disposal gates, per-view parameter isolation, SDK response-filter ordering, and redirects. The confirmed relative-action, filter-ordering, and authored-resource regressions were reproduced with failing assertions before their fixes and pass afterward.

A final independent, fresh-context source review covered all 72 changed paths and found no significant correctness, lifetime, data-integrity, compatibility, or test-validity blocker. This is a bounded readiness review, not a claim of exhaustive verification. The PR remains draft for maintainer and API review.

@github-actions github-actions Bot added area-blazor Includes: Blazor, Razor Components area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework labels Nov 4, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey @dotnet/aspnet-build, looks like this PR is something you want to take a look at.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant