From 92322834645f7f213797bd6dc5c710c747006c11 Mon Sep 17 00:00:00 2001 From: Per Bering Date: Thu, 15 Jan 2026 08:14:57 +0100 Subject: [PATCH] added readonlymode setting to disable mutating tools --- README.md | 1 + src/SitecoreBasicMcp/AssemblyMarker.cs | 2 +- src/SitecoreBasicMcp/SitecoreSettings.cs | 2 ++ src/SitecoreBasicMcp/StartupExtensions.cs | 19 ++++++++++++++++--- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a23cf8b..9862ff0 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,4 @@ At least _one_ authentication provider must be configured, executed in order: | Sitecore:CliUserFileAuthentication:EndpointName | `xmCloud` | The endpoint name to use | | Sitecore:CloudAuthentication:ClientId | | Id with access to authoring endpoint | | Sitecore:CloudAuthentication:ClientSecret | | Secret with access to authoring endpoint | +| Sitecore:ReadonlyMode | `false` | Disables all tools that mutates data | diff --git a/src/SitecoreBasicMcp/AssemblyMarker.cs b/src/SitecoreBasicMcp/AssemblyMarker.cs index 7683e3b..105a513 100644 --- a/src/SitecoreBasicMcp/AssemblyMarker.cs +++ b/src/SitecoreBasicMcp/AssemblyMarker.cs @@ -1,5 +1,5 @@ namespace SitecoreBasicMcp; -public class AssemblyMarker +internal class AssemblyMarker { } \ No newline at end of file diff --git a/src/SitecoreBasicMcp/SitecoreSettings.cs b/src/SitecoreBasicMcp/SitecoreSettings.cs index f0fd4fd..50a1b03 100644 --- a/src/SitecoreBasicMcp/SitecoreSettings.cs +++ b/src/SitecoreBasicMcp/SitecoreSettings.cs @@ -21,4 +21,6 @@ public class CloudAuthenticationSettings public string? ClientId { get; set; } public string? ClientSecret { get; set; } } + + public bool ReadonlyMode { get; set; } } \ No newline at end of file diff --git a/src/SitecoreBasicMcp/StartupExtensions.cs b/src/SitecoreBasicMcp/StartupExtensions.cs index ab723ec..dd316ff 100644 --- a/src/SitecoreBasicMcp/StartupExtensions.cs +++ b/src/SitecoreBasicMcp/StartupExtensions.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Hosting; using ModelContextProtocol.Protocol; using SitecoreBasicMcp.Authentication; +using SitecoreBasicMcp.Tools; namespace SitecoreBasicMcp; @@ -10,7 +11,12 @@ public static class StartupExtensions { public static IMcpServerBuilder AddSitecoreMcpServer(this IHostApplicationBuilder builder) { - builder.Services.Configure(builder.Configuration.GetSection(SitecoreSettings.Key)); + var sitecoreSection = builder.Configuration.GetSection(SitecoreSettings.Key); + var sitecoreSettings = sitecoreSection.Get(); + + ArgumentNullException.ThrowIfNull(sitecoreSettings, nameof(sitecoreSettings)); + + builder.Services.Configure(sitecoreSection); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); @@ -18,8 +24,6 @@ public static IMcpServerBuilder AddSitecoreMcpServer(this IHostApplicationBuilde var serverBuilder = builder.Services .AddHttpClient() .AddMcpServer() - .WithToolsFromAssembly(typeof(AssemblyMarker).Assembly) - .WithPromptsFromAssembly(typeof(AssemblyMarker).Assembly) .AddCallToolFilter(next => async (context, cancellationToken) => { try @@ -36,6 +40,15 @@ public static IMcpServerBuilder AddSitecoreMcpServer(this IHostApplicationBuilde } }); + if (sitecoreSettings.ReadonlyMode) + { + serverBuilder.WithTools(); + } + else + { + serverBuilder.WithToolsFromAssembly(typeof(AssemblyMarker).Assembly); + } + return serverBuilder; } }