McpServiceCollectionExtensions Class
Assembly: CodeEffects.Rule.Mcp.dll
Namespace: CodeEffects.Rule.Mcp
Summary
Adds the Code Effects MCP server to an ASP.NET Core application. It holds the extension method that starts the registration, AddCodeEffectsMcp(..).
Syntax
public static class McpServiceCollectionExtensions
Call AddCodeEffectsMcp(..) on builder.Services once, then chain WithRepository(..) and WithAudit(..) on the CodeEffectsMcpBuilder it returns, and map the endpoint with MapCodeEffectsMcp(..) behind your own authentication.
The server uses stateless HTTP: every request carries its own caller, and nothing about a client is kept between requests. The list of tools is built for each caller from the permissions your MapCaller delegate gives them.
Methods
AddCodeEffectsMcp(..)
Returns: CodeEffects.Rule.Mcp.CodeEffectsMcpBuilder - The builder to which you add the libraries the server offers.
public static CodeEffectsMcpBuilder AddCodeEffectsMcp(this IServiceCollection services, Action<CodeEffectsMcpOptions> configure)
An extension method of Microsoft.Extensions.DependencyInjection.IServiceCollection. Adds the MCP server to the application's services. The configure delegate is called at once with a new CodeEffectsMcpOptions; the options are read when the method runs, so a change made to them later has no effect.
The method also reads Code Effects' own skills and, when SkillsFolder is set, the skills in your folder, so a skill that breaks the rules stops the application at start-up and not at the first request. It adds IHttpContextAccessor to the services.
Parameters
- services (
Microsoft.Extensions.DependencyInjection.IServiceCollection) - The application's services, usually builder.Services.
- configure (
System.Action<CodeEffectsMcpOptions>) - Sets the options. MapCaller is required.
Exceptions
System.ArgumentNullException - services or configure is null.
System.ArgumentException - MapCaller is not set.
System.InvalidOperationException - SkillsFolder is set and is not a full path, does not exist, or holds a skill that breaks the rules described on SkillsFolder.
Example
using CodeEffects.Rule.Common.Models;
using CodeEffects.Rule.Mcp;
using Microsoft.AspNetCore.Builder;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddCodeEffectsMcp(o => o.MapCaller = (user, http) =>
{
var id = user.FindFirst("sub")?.Value;
if (id == null) return null;
return new McpCaller
{
UserId = id,
AccessMode = ActionPermissionMode.AllowAllExcept
};
})
.WithRepository(r => r.DbProvider = dbProvider);