This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch issue/title-version-override in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-mcp-server.git
commit 71ab12a67c4c66def148ad34232df9d36a6c6f92 Author: Robert Munteanu <[email protected]> AuthorDate: Thu Mar 12 15:49:49 2026 +0100 feat: allow the reported server title and version to be overridden by an OSGi service --- .../apache/sling/mcp/server/impl/McpServlet.java | 25 +++++++++---------- .../sling/mcp/server/spi/McpServerDescription.java | 29 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/sling/mcp/server/impl/McpServlet.java b/src/main/java/org/apache/sling/mcp/server/impl/McpServlet.java index 668d583..44e0714 100644 --- a/src/main/java/org/apache/sling/mcp/server/impl/McpServlet.java +++ b/src/main/java/org/apache/sling/mcp/server/impl/McpServlet.java @@ -44,6 +44,7 @@ import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.apache.sling.mcp.server.spi.McpServerContribution; +import org.apache.sling.mcp.server.spi.McpServerDescription; import org.apache.sling.servlets.annotations.SlingServletPaths; import org.jetbrains.annotations.NotNull; import org.osgi.framework.BundleContext; @@ -51,6 +52,7 @@ import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; import org.osgi.service.component.annotations.ReferencePolicy; import org.osgi.service.metatype.annotations.AttributeDefinition; import org.osgi.service.metatype.annotations.Designate; @@ -69,13 +71,8 @@ public class McpServlet extends SlingAllMethodsServlet { @ObjectClassDefinition(name = "Apache Sling MCP Server Configuration") public @interface Config { - @AttributeDefinition(name = "Server Title", description = "The title of the MCP server") - String serverTitle() default "Apache Sling"; - - @AttributeDefinition( - name = "Server Version", - description = "The version of the MCP server. Defaults to the bundle version if not set") - String serverVersion(); + @AttributeDefinition(name = "Server Name", description = "The name of the MCP server. Can be overridden by a registered McpServerDescription service") + String serverName() default "Apache Sling"; @AttributeDefinition(name = "Instructions", description = "Initial instructions for the MCP server") String instructions() default @@ -98,6 +95,8 @@ public class McpServlet extends SlingAllMethodsServlet { Config config, @Reference McpJsonMapperSupplier jsonMapperSupplier, @Reference JsonSchemaValidatorSupplier jsonSchemaValidatorSupplier, + @Reference(cardinality = ReferenceCardinality.OPTIONAL, policyOption = GREEDY) + McpServerDescription serverDescription, @Reference(cardinality = MULTIPLE, policyOption = GREEDY) List<McpServerContribution> contributions) throws IllegalAccessException, NoSuchMethodException { @@ -129,18 +128,18 @@ public class McpServlet extends SlingAllMethodsServlet { jakarta.servlet.http.HttpServletRequest.class, jakarta.servlet.http.HttpServletResponse.class)); - String serverVersion = config.serverVersion(); - if (serverVersion == null || serverVersion.isEmpty()) { - serverVersion = ctx.getBundle().getVersion().toString(); - } - var completions = contributions.stream() .map(McpServerContribution::getSyncCompletionSpecification) .flatMap(List::stream) .toList(); + String serverName = serverDescription != null ? serverDescription.name() : config.serverName(); + String serverVersion = serverDescription != null + ? serverDescription.version() + : ctx.getBundle().getVersion().toString(); + syncServer = McpServer.sync(transportProvider) - .serverInfo(config.serverTitle(), serverVersion) + .serverInfo(serverName, serverVersion) .jsonMapper(jsonMapperSupplier.get()) .jsonSchemaValidator(jsonSchemaValidatorSupplier.get()) .instructions(config.instructions()) diff --git a/src/main/java/org/apache/sling/mcp/server/spi/McpServerDescription.java b/src/main/java/org/apache/sling/mcp/server/spi/McpServerDescription.java new file mode 100644 index 0000000..5c5ecbe --- /dev/null +++ b/src/main/java/org/apache/sling/mcp/server/spi/McpServerDescription.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.mcp.server.spi; + +/** + * Describes the MCP server title and version that will be sent to MCP clients upon initial connection in the <tt>serverInfo</tt> field. + */ +public interface McpServerDescription { + + String name(); + + String version(); +}
