KristofferStrube.Blazor.WebMCP
0.1.0-alpha.1
Prefix Reserved
dotnet add package KristofferStrube.Blazor.WebMCP --version 0.1.0-alpha.1
NuGet\Install-Package KristofferStrube.Blazor.WebMCP -Version 0.1.0-alpha.1
<PackageReference Include="KristofferStrube.Blazor.WebMCP" Version="0.1.0-alpha.1" />
<PackageVersion Include="KristofferStrube.Blazor.WebMCP" Version="0.1.0-alpha.1" />
<PackageReference Include="KristofferStrube.Blazor.WebMCP" />
paket add KristofferStrube.Blazor.WebMCP --version 0.1.0-alpha.1
#r "nuget: KristofferStrube.Blazor.WebMCP, 0.1.0-alpha.1"
#:package KristofferStrube.Blazor.WebMCP@0.1.0-alpha.1
#addin nuget:?package=KristofferStrube.Blazor.WebMCP&version=0.1.0-alpha.1&prerelease
#tool nuget:?package=KristofferStrube.Blazor.WebMCP&version=0.1.0-alpha.1&prerelease
Blazor.WebMCP
A Blazor wrapper for the WebMCP browser API. The Web API enables the browser to provide tools that can be accessed by AI agents and assistive technologies to create collaborative, human-in-the-loop workflows. This project implements a wrapper around the API for Blazor so that we can easily and safely expose our own tools in C# through the WebMCP API.
The browser API is not stable yet, so this project might break in the future if the API changes
Demo
The sample project can be demoed at https://kristofferstrube.github.io/Blazor.WebMCP/
On each page, you can find the corresponding code for the example in the top right corner.
On the API Coverage Status page, you can see how much of the WebIDL specs this wrapper has covered.
Getting started
I don't have a NuGet package out for the library yet, but it is planned. Once it is out you can follow this small guide to add your own tools to your Blazor App.
Using the library you can add your own WebMCP tools to your web page. First you need to register a service in your service collection using the following extension:
builder.Services.AddModelContextService();
Then in some page you can inject the IModelContextService to register your own tool.
@using KristofferStrube.Blazor.DOM
@using KristofferStrube.Blazor.WebMCP
@implements IAsyncDisposable
@inject IModelContextService ModelContextService
@inject IJSRuntime JSRuntime
<PageTitle>Blazor WebMCP</PageTitle>
<h1>@contentFromAI</h1>
@code {
string contentFromAI = "";
SupportStatus browserSupport;
AbortController? toolUnregisterController;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender) return;
ModelContextTool<ToolArguments, string> tool = new()
{
Name = "WriteContent",
Description = "This tool can write some content to the website.",
Execute = async (ToolArguments input, ModelContextClient? client) =>
{
contentFromAI = input.Message;
await InvokeAsync(StateHasChanged);
return "Content was written to the website.";
}
};
toolUnregisterController = await AbortController.CreateAsync(JSRuntime);
await using AbortSignal toolUnregisterSignal = await toolUnregisterController.GetSignalAsync();
await ModelContextService.RegisterToolAsync(tool, new ModelContextRegisterToolOptions()
{
Signal = toolUnregisterSignal
});
}
public record ToolArguments(string Message);
public async ValueTask DisposeAsync()
{
if (toolUnregisterController is not null)
{
await toolUnregisterController.AbortAsync();
await toolUnregisterController.DisposeAsync();
}
}
}
In the above example we also unregister our tool once the user navigated away from the specific page by implementing AsyncDisposable. This is especially important in Blazor WASM where navigation does not reload the page.
Related repositories
The library uses the following other packages to support its features:
- https://github.com/KristofferStrube/Blazor.WebIDL (To make error handling JSInterop)
Related articles
This repository was built with help from the following series of articles:
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- KristofferStrube.Blazor.DOM (>= 0.3.0)
- Microsoft.AspNetCore.Components.Web (>= 10.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0-alpha.1 | 23 | 3/30/2026 |