|
| 1 | +package com.openelements.content; |
| 2 | + |
| 3 | +import static com.openelements.spring.base.mcp.McpTools.integer; |
| 4 | +import static com.openelements.spring.base.mcp.McpTools.paginationProps; |
| 5 | +import static com.openelements.spring.base.mcp.McpTools.prop; |
| 6 | +import static com.openelements.spring.base.mcp.McpTools.requiredString; |
| 7 | +import static com.openelements.spring.base.mcp.McpTools.string; |
| 8 | +import static com.openelements.spring.base.mcp.McpTools.tool; |
| 9 | + |
| 10 | +import com.openelements.spring.base.mcp.McpPaging; |
| 11 | +import com.openelements.spring.base.mcp.McpToolProvider; |
| 12 | +import com.openelements.spring.base.mcp.McpToolSupport; |
| 13 | +import com.openelements.spring.base.mcp.McpUnavailableException; |
| 14 | +import com.openelements.spring.base.services.search.SearchReadinessState; |
| 15 | +import io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification; |
| 16 | +import io.modelcontextprotocol.spec.McpSchema.Tool; |
| 17 | +import java.util.LinkedHashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.NoSuchElementException; |
| 21 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 22 | +import org.springframework.stereotype.Component; |
| 23 | + |
| 24 | +/** |
| 25 | + * Exposes the four content tools on the {@code /mcp} endpoint by implementing the library |
| 26 | + * {@link McpToolProvider} (which {@code McpServerConfig} auto-aggregates). The tools are thin |
| 27 | + * adapters over {@link ContentSearchService}; schemas, argument parsing, paging, access logging, and |
| 28 | + * JSON-RPC error mapping are handled by the house helpers. |
| 29 | + * |
| 30 | + * <p>Error mapping (via {@link McpToolSupport#spec}): {@link IllegalArgumentException} → |
| 31 | + * invalid-argument, {@link NoSuchElementException} → not-found, {@link McpUnavailableException} → |
| 32 | + * temporary-unavailable. Search and list report unavailable while the index is bootstrapping. |
| 33 | + * |
| 34 | + * <p>Only created when the MCP server is enabled. |
| 35 | + */ |
| 36 | +@Component |
| 37 | +@ConditionalOnProperty(prefix = "openelements.mcp", name = "enabled", havingValue = "true", matchIfMissing = true) |
| 38 | +public class ContentMcpToolProvider implements McpToolProvider { |
| 39 | + |
| 40 | + private final ContentSearchService searchService; |
| 41 | + private final McpToolSupport support; |
| 42 | + private final McpPaging paging; |
| 43 | + private final SearchReadinessState readinessState; |
| 44 | + |
| 45 | + public ContentMcpToolProvider(ContentSearchService searchService, McpToolSupport support, |
| 46 | + McpPaging paging, SearchReadinessState readinessState) { |
| 47 | + this.searchService = searchService; |
| 48 | + this.support = support; |
| 49 | + this.paging = paging; |
| 50 | + this.readinessState = readinessState; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public List<SyncToolSpecification> toolSpecifications() { |
| 55 | + return List.of(searchContent(), listPosts(), getPost(), listCategories()); |
| 56 | + } |
| 57 | + |
| 58 | + // ---- tool definitions ---- |
| 59 | + |
| 60 | + private SyncToolSpecification searchContent() { |
| 61 | + Map<String, Object> props = new LinkedHashMap<>(paginationProps()); |
| 62 | + props.put("query", prop("string", "Search query (required).")); |
| 63 | + props.put("locale", prop("string", "Filter by locale, e.g. en or de.")); |
| 64 | + props.put("source", prop("string", "Filter by source id.")); |
| 65 | + props.put("category", prop("string", "Filter by category.")); |
| 66 | + Tool tool = tool("search_content", |
| 67 | + "Full-text search across the indexed content; returns highlighted snippets.", |
| 68 | + props, List.of("query")); |
| 69 | + return support.spec(tool, this::searchContentLogic); |
| 70 | + } |
| 71 | + |
| 72 | + private SyncToolSpecification listPosts() { |
| 73 | + Map<String, Object> props = new LinkedHashMap<>(paginationProps()); |
| 74 | + props.put("locale", prop("string", "Filter by locale, e.g. en or de.")); |
| 75 | + props.put("source", prop("string", "Filter by source id.")); |
| 76 | + props.put("category", prop("string", "Filter by category.")); |
| 77 | + props.put("since", prop("string", "Only posts published on/after this ISO date (YYYY-MM-DD).")); |
| 78 | + Tool tool = tool("list_posts", |
| 79 | + "List indexed posts, newest first.", props, List.of()); |
| 80 | + return support.spec(tool, this::listPostsLogic); |
| 81 | + } |
| 82 | + |
| 83 | + private SyncToolSpecification getPost() { |
| 84 | + Map<String, Object> props = new LinkedHashMap<>(); |
| 85 | + props.put("url", prop("string", "The post URL (provide url or id).")); |
| 86 | + props.put("id", prop("string", "The post id (provide url or id).")); |
| 87 | + Tool tool = tool("get_post", |
| 88 | + "Get the full content and metadata of a single post by url or id.", props, List.of()); |
| 89 | + return support.spec(tool, this::getPostLogic); |
| 90 | + } |
| 91 | + |
| 92 | + private SyncToolSpecification listCategories() { |
| 93 | + Map<String, Object> props = new LinkedHashMap<>(); |
| 94 | + props.put("source", prop("string", "Filter by source id.")); |
| 95 | + props.put("locale", prop("string", "Filter by locale, e.g. en or de.")); |
| 96 | + Tool tool = tool("list_categories", |
| 97 | + "List content categories with their document counts.", props, List.of()); |
| 98 | + return support.spec(tool, this::listCategoriesLogic); |
| 99 | + } |
| 100 | + |
| 101 | + // ---- tool logic (package-private for direct unit testing) ---- |
| 102 | + |
| 103 | + Object searchContentLogic(Map<String, Object> args) { |
| 104 | + requireReady(); |
| 105 | + ContentFilters filters = new ContentFilters( |
| 106 | + string(args, "source"), string(args, "locale"), string(args, "category"), null); |
| 107 | + return searchService.search( |
| 108 | + requiredString(args, "query"), filters, |
| 109 | + paging.resolvePage(integer(args, "page")), paging.resolveSize(integer(args, "size"))); |
| 110 | + } |
| 111 | + |
| 112 | + Object listPostsLogic(Map<String, Object> args) { |
| 113 | + requireReady(); |
| 114 | + ContentFilters filters = new ContentFilters( |
| 115 | + string(args, "source"), string(args, "locale"), string(args, "category"), string(args, "since")); |
| 116 | + return searchService.listPosts( |
| 117 | + filters, paging.resolvePage(integer(args, "page")), paging.resolveSize(integer(args, "size"))); |
| 118 | + } |
| 119 | + |
| 120 | + Object getPostLogic(Map<String, Object> args) { |
| 121 | + String url = string(args, "url"); |
| 122 | + String id = string(args, "id"); |
| 123 | + if (isBlank(url) && isBlank(id)) { |
| 124 | + throw new IllegalArgumentException("Either 'url' or 'id' is required"); |
| 125 | + } |
| 126 | + return searchService.getByUrlOrId(url, id) |
| 127 | + .orElseThrow(() -> new NoSuchElementException("No post found for the given url/id")); |
| 128 | + } |
| 129 | + |
| 130 | + Object listCategoriesLogic(Map<String, Object> args) { |
| 131 | + return searchService.categoryFacets(string(args, "source"), string(args, "locale")); |
| 132 | + } |
| 133 | + |
| 134 | + private void requireReady() { |
| 135 | + if (readinessState.isBootstrapping()) { |
| 136 | + throw new McpUnavailableException("Content index is still bootstrapping; try again shortly"); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private static boolean isBlank(String value) { |
| 141 | + return value == null || value.isBlank(); |
| 142 | + } |
| 143 | +} |
0 commit comments