diff --git a/src/main/java/org/apache/solr/mcp/server/collection/CollectionService.java b/src/main/java/org/apache/solr/mcp/server/collection/CollectionService.java index 48ec56d3..9d41c007 100644 --- a/src/main/java/org/apache/solr/mcp/server/collection/CollectionService.java +++ b/src/main/java/org/apache/solr/mcp/server/collection/CollectionService.java @@ -411,6 +411,10 @@ public List listCollections() { public SolrMetrics getCollectionStats( @McpToolParam(description = "Solr collection to get stats/metrics for") String collection) throws SolrServerException, IOException { + if (collection == null || collection.isBlank()) { + throw new IllegalArgumentException(BLANK_COLLECTION_NAME_ERROR); + } + // Extract actual collection name from shard name if needed String actualCollection = extractCollectionName(collection); @@ -957,6 +961,10 @@ private boolean validateCollectionExists(String collection) { */ @McpTool(name = "check-health", description = "Check health of a Solr collection") public SolrHealthStatus checkHealth(@McpToolParam(description = "Solr collection") String collection) { + if (collection == null || collection.isBlank()) { + throw new IllegalArgumentException(BLANK_COLLECTION_NAME_ERROR); + } + String actualCollection = extractCollectionName(collection); try { // Ping Solr diff --git a/src/main/java/org/apache/solr/mcp/server/indexing/IndexingService.java b/src/main/java/org/apache/solr/mcp/server/indexing/IndexingService.java index e52ca7b1..9ee23c89 100644 --- a/src/main/java/org/apache/solr/mcp/server/indexing/IndexingService.java +++ b/src/main/java/org/apache/solr/mcp/server/indexing/IndexingService.java @@ -196,6 +196,10 @@ public IndexingService(SolrClient solrClient, IndexingDocumentCreator indexingDo public void indexJsonDocuments(@McpToolParam(description = "Solr collection to index into") String collection, @McpToolParam(description = "JSON string containing documents to index") String json) throws IOException, SolrServerException { + if (collection == null || collection.isBlank()) { + throw new IllegalArgumentException("Collection name must not be blank"); + } + List schemalessDoc = indexingDocumentCreator.createSchemalessDocumentsFromJson(json); indexDocuments(collection, schemalessDoc); } @@ -262,6 +266,10 @@ public void indexJsonDocuments(@McpToolParam(description = "Solr collection to i public void indexCsvDocuments(@McpToolParam(description = "Solr collection to index into") String collection, @McpToolParam(description = "CSV string containing documents to index") String csv) throws IOException, SolrServerException { + if (collection == null || collection.isBlank()) { + throw new IllegalArgumentException("Collection name must not be blank"); + } + List schemalessDoc = indexingDocumentCreator.createSchemalessDocumentsFromCsv(csv); indexDocuments(collection, schemalessDoc); } @@ -352,6 +360,10 @@ public void indexCsvDocuments(@McpToolParam(description = "Solr collection to in public void indexXmlDocuments(@McpToolParam(description = "Solr collection to index into") String collection, @McpToolParam(description = "XML string containing documents to index") String xml) throws ParserConfigurationException, SAXException, IOException, SolrServerException { + if (collection == null || collection.isBlank()) { + throw new IllegalArgumentException("Collection name must not be blank"); + } + List schemalessDoc = indexingDocumentCreator.createSchemalessDocumentsFromXml(xml); indexDocuments(collection, schemalessDoc); } diff --git a/src/main/java/org/apache/solr/mcp/server/metadata/SchemaService.java b/src/main/java/org/apache/solr/mcp/server/metadata/SchemaService.java index f7955872..8b23ae2e 100644 --- a/src/main/java/org/apache/solr/mcp/server/metadata/SchemaService.java +++ b/src/main/java/org/apache/solr/mcp/server/metadata/SchemaService.java @@ -163,6 +163,10 @@ public SchemaService(SolrClient solrClient, ObjectMapper objectMapper) { */ @McpResource(uri = "solr://{collection}/schema", name = "solr-collection-schema", description = "Schema definition for a Solr collection including fields, field types, and copy fields", mimeType = "application/json") public String getSchemaResource(String collection) { + if (collection == null || collection.isBlank()) { + throw new IllegalArgumentException("Collection name must not be blank"); + } + try { return toJson(objectMapper, getSchema(collection)); } catch (Exception e) { @@ -251,6 +255,10 @@ public String getSchemaResource(String collection) { */ @McpTool(name = "get-schema", description = "Get schema for a Solr collection") public SchemaRepresentation getSchema(String collection) throws Exception { + if (collection == null || collection.isBlank()) { + throw new IllegalArgumentException("Collection name must not be blank"); + } + SchemaRequest schemaRequest = new SchemaRequest(); return schemaRequest.process(solrClient, collection).getSchemaRepresentation(); } diff --git a/src/main/java/org/apache/solr/mcp/server/search/SearchService.java b/src/main/java/org/apache/solr/mcp/server/search/SearchService.java index c29ccb6a..e536ba4f 100644 --- a/src/main/java/org/apache/solr/mcp/server/search/SearchService.java +++ b/src/main/java/org/apache/solr/mcp/server/search/SearchService.java @@ -250,6 +250,10 @@ public SearchResponse search(@McpToolParam(description = "Solr collection to que @McpToolParam(description = "Number of rows to return", required = false) Integer rows) throws SolrServerException, IOException { + if (collection == null || collection.isBlank()) { + throw new IllegalArgumentException("Collection name must not be blank"); + } + // query final SolrQuery solrQuery = new SolrQuery("*:*"); if (StringUtils.hasText(query)) {