Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ public Uni<ToolResponse> processCollectionCommand(
.addThrowable(throwable)
.build();
return Uni.createFrom()
.item(new ToolResponse(true, null, errorResult.errors(), Map.of()));
.item(
new ToolResponse(
true, List.of(), Map.of("errors", errorResult.errors()), Map.of()));
} else {
VectorColumnDefinition vectorColDef = null;
if (schemaObject.type() == SchemaObject.SchemaObjectType.COLLECTION) {
Expand Down Expand Up @@ -218,7 +220,7 @@ public Uni<ToolResponse> processCommand(CommandContext<?> context, Command comma
Map<MetaKey, Object> meta =
(result.status() != null && !result.status().isEmpty())
? Map.of(MetaKey.of("status"), result.status())
: null;
: Map.of();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace null with empty list/map


// Map "errors" or "data" to structuredContent
// Also, structuredContent is expected to be a Record (a plain JSON object {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public CommandResult get() {
// when we move to use OperationAttempt for the collection commands we can refactor
if (deletedInformation == null) {
// when returnDocument is set this means we are runnning findOneAndDelete, so we have to
// return a
// data and documents section
// return a data and documents section
// aaron - this is a giant hack 21 oct 2024
if (returnDocument()) {
if (singleDocument()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package io.stargate.sgv2.jsonapi.api.v1.mcp;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import io.quarkiverse.mcp.server.MetaKey;
import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.stargate.sgv2.jsonapi.api.model.command.CommandName;
import io.stargate.sgv2.jsonapi.testresource.DseTestResource;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import java.util.Map;
import org.junit.jupiter.api.*;

/**
* MCP integration tests for {@link GeneralCommandTools}. Uses the Streamable HTTP transport via
* McpAssured to test all general-level MCP tools end-to-end.
*/
@QuarkusIntegrationTest
@WithTestResource(value = DseTestResource.class)
class GeneralCommandToolsMcpIntegrationTest extends McpIntegrationTestBase {

private static final String EXTRA_KEYSPACE = "new_ks";

@Nested
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class CreateFindAndDropKeyspaceToolCall {
@Test
@Order(1)
void testCreateKeyspaceToolCall() {
callToolAndAssert(
CommandName.Names.CREATE_KEYSPACE,
Map.of("name", EXTRA_KEYSPACE),
response -> {
assertFalse(response.isError());
assertNotNull(response._meta());
assertNull(response.structuredContent());
assertThat(response.content()).isEmpty();
});
}

@Test
@Order(2)
void testFindKeyspaceToolCallAfterCreateKeyspace() {
callToolAndAssert(
CommandName.Names.FIND_KEYSPACES,
Map.of(),
response -> {
// check mcp response
assertFalse(response.isError());
assertNotNull(response._meta());
assertNull(response.structuredContent());

// check the new keyspace is there
var status = (JsonObject) response._meta().get(MetaKey.of("status"));
assertNotNull(status, "Status should not be null");
JsonArray keyspaces = status.getJsonArray("keyspaces");
assertNotNull(keyspaces, "Keyspaces array should not be null");
assertTrue(
keyspaces.contains(EXTRA_KEYSPACE), "New created Keyspace should be in the list");
});
}

@Test
@Order(3)
void testDropKeyspaceToolCall() {
callToolAndAssert(
CommandName.Names.DROP_KEYSPACE,
Map.of("name", EXTRA_KEYSPACE),
response -> {
assertFalse(response.isError());
assertNotNull(response._meta());
assertNull(response.structuredContent());
});
}

@Test
@Order(4)
void testFindKeyspaceToolCallAfterDropKeyspace() {
callToolAndAssert(
CommandName.Names.FIND_KEYSPACES,
Map.of(),
response -> {
// check mcp response
assertFalse(response.isError());
assertNotNull(response._meta());
assertNull(response.structuredContent());

// check the new keyspace is dropped
var status = (JsonObject) response._meta().get(MetaKey.of("status"));
assertNotNull(status, "Status should not be null");
JsonArray keyspaces = status.getJsonArray("keyspaces");
assertNotNull(keyspaces, "Keyspaces array should not be null");
assertFalse(
keyspaces.contains(EXTRA_KEYSPACE),
"Keyspace should be dropped and not in the list");
});
}
}

@Test
void findEmbeddingProvidersToolCall() {
callToolAndAssert(
"findEmbeddingProviders",
Map.of(),
response -> {
assertFalse(response.isError());
assertNotNull(response._meta());
assertNull(response.structuredContent());
});
}

@Test
void findRerankingProvidersToolCall() {
callToolAndAssert(
"findRerankingProviders",
Map.of(),
response -> {
assertFalse(response.isError());
assertNotNull(response._meta());
assertNull(response.structuredContent());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package io.stargate.sgv2.jsonapi.api.v1.mcp;

import static io.stargate.sgv2.jsonapi.api.v1.util.IntegrationTestUtils.*;
import static org.junit.jupiter.api.Assertions.assertFalse;

import io.quarkiverse.mcp.server.ToolResponse;
import io.quarkiverse.mcp.server.test.McpAssured;
import io.quarkiverse.mcp.server.test.McpAssured.McpStreamableTestClient;
import io.stargate.sgv2.jsonapi.config.constants.HttpConstants;
import io.vertx.core.MultiMap;
import java.net.URI;
import java.util.Base64;
import java.util.Map;
import java.util.function.Consumer;
import org.apache.commons.lang3.RandomStringUtils;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.TestInstance;

/**
* Abstract base class for MCP integration tests. Provides a shared MCP client instance,
* authentication, and utility methods for invoking MCP tools via the Streamable HTTP transport.
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class McpIntegrationTestBase {

private static final String MCP_PATH = "/v1/mcp";

/** MCP Assured cannot automatically resolve the URI (like Rest Assured) */
private static final String MCP_HOSTNAME = "http://localhost:";

/** Test keyspace name, with a random suffix for isolation. */
protected final String keyspaceName =
"mcp_ks_" + RandomStringUtils.insecure().nextAlphanumeric(8).toLowerCase();

/** Test collection name, with a random suffix for isolation. */
protected final String collectionName =
"mcp_col_" + RandomStringUtils.insecure().nextAlphanumeric(8).toLowerCase();

/** Shared MCP client instance, connected once per test class. */
protected McpStreamableTestClient mcpClient;

/**
* Initializes the shared MCP client before all tests in the class. Connects to the local test
* server using Streamable HTTP transport with authentication headers.
*/
@BeforeAll
void setUpMcpClient() {
mcpClient =
McpAssured.newStreamableClient()
.setBaseUri(URI.create(MCP_HOSTNAME + getTestPort()))
.setMcpPath(MCP_PATH)
.setAdditionalHeaders(msg -> authHeaders())
.build()
.connect();
}

/** Disconnects and releases the shared MCP client after all tests in the class have run. */
@AfterAll
void tearDownMcpClient() {
if (mcpClient != null) {
mcpClient.disconnect();
mcpClient = null;
}
}

protected int getTestPort() {
try {
return ConfigProvider.getConfig().getValue("quarkus.http.test-port", Integer.class);
} catch (Exception e) {
return Integer.parseInt(System.getProperty("quarkus.http.test-port"));
}
}

/** Build authentication headers matching the Token header format used by the REST API. */
protected MultiMap authHeaders() {
String credential =
"Cassandra:"
+ Base64.getEncoder().encodeToString(getCassandraUsername().getBytes())
+ ":"
+ Base64.getEncoder().encodeToString(getCassandraPassword().getBytes());
return MultiMap.caseInsensitiveMultiMap()
.add(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, credential);
}

/** Create a keyspace via the MCP createKeyspace tool. */
protected void createKeyspace(String keyspace) {
callToolAndAssert(
"createKeyspace", Map.of("name", keyspace), response -> assertFalse(response.isError()));
}

/** Drop a keyspace via the MCP dropKeyspace tool. */
protected void dropKeyspace(String keyspace) {
callToolAndAssert(
"dropKeyspace", Map.of("name", keyspace), response -> assertFalse(response.isError()));
}

/** Create a collection via the MCP createCollection tool. */
protected void createCollection(String keyspace, String collection) {
callToolAndAssert(
"createCollection",
Map.of("keyspace", keyspace, "collection", collection),
response -> assertFalse(response.isError()));
}

/** Delete a collection via the MCP deleteCollection tool */
protected void deleteCollection(String keyspace, String collection) {
callToolAndAssert(
"deleteCollection",
Map.of("keyspace", keyspace, "collection", collection),
response -> assertFalse(response.isError()));
}
Comment on lines +88 to +113
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no usage for now, reserved for other tools IT


/**
* Execute an MCP tool call and assert the response using the shared client.
*
* @param toolName the MCP tool name to invoke
* @param args the tool arguments
* @param assertFn assertion function for the ToolResponse
*/
protected void callToolAndAssert(
String toolName, Map<String, Object> args, Consumer<ToolResponse> assertFn) {
mcpClient.when().toolsCall(toolName, args, assertFn).thenAssertResults();
}
}
Loading