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
32 changes: 16 additions & 16 deletions example/ExampleUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
public class ExampleUsage {
public static void main(String[] args) {
// CONFIGURATION
String targetUrl = "YOUR_TOOLBOX_SERVICE_ENDPOINT";
String targetUrl = "YOUR_TOOLBOX_SERVICE_ENDPOINT";

// Match the Service URL if using Cloud Run OIDC
String tokenAudience = targetUrl;
String tokenAudience = targetUrl;

// --------------------------------------------------------------------------------
// AUTHENTICATION SETUP
Expand All @@ -44,21 +44,21 @@ public static void main(String[] args) {
// FOR PRODUCTION (Cloud Run): Comment out the 'keyPath' logic and use ADC directly.
// --------------------------------------------------------------------------------

String keyPath = "/YOUR_CREDENTIALS_JSON_FILE_PATH.json";
String keyPath = "YOUR_CREDENTIALS_JSON_FILE_PATH.json";

System.out.println("--- Starting MCP Toolbox Integration Test ---");
System.out.println("Target Server: " + targetUrl);

try {
System.out.println(" [Init] Fetching ID Token...");

GoogleCredentials credentials;

// --- OPTION A: LOCAL DEV (Explicit Key File) ---
if (keyPath != null && !keyPath.isEmpty()) {
System.out.println(" [Auth] Using Service Account Key File: " + keyPath);
credentials = GoogleCredentials.fromStream(new FileInputStream(keyPath));
}
}
// --- OPTION B: PRODUCTION (ADC) ---
else {
System.out.println(" [Auth] Using Application Default Credentials (ADC)");
Expand All @@ -76,14 +76,14 @@ public static void main(String[] args) {
// Initialize Client with Global Auth (Applies to ALL calls - Gate 1)
McpToolboxClient client = McpToolboxClient.builder()
.baseUrl(targetUrl)
.apiKey(idToken)
.apiKey(idToken)
.build();

// STEP 1: TEST DISCOVERY METHODS
client.listTools()
.thenCompose(tools -> {
System.out.println("\n[1] listTools(): Success. Found " + tools.size() + " tools.");
return client.loadToolset();
return client.loadToolset();
})
.thenCompose(tools -> {
System.out.println("[2] loadToolset() (Alias): Success.");
Expand All @@ -95,17 +95,17 @@ public static void main(String[] args) {
});
})
.thenCompose(ignore -> {

// STEP 2: INVOKE TOOL WITHOUT EXTRA AUTH
System.out.println("\n[4] Testing Simple Tool: 'get-retail-facet-filters'...");
return client.invokeTool("get-retail-facet-filters", Map.of());
})
.thenCompose(result -> {
System.out.println(" -> Result: " + (result.content() != null ? "Received Data" : "Empty"));

// STEP 3: INVOKE TOOL WITH AUTHENTICATED PARAMETERS
System.out.println("\n[5] Testing Authenticated Tool: 'get-toy-price'...");

// Define the getter for the 'google_auth' service
AuthTokenGetter toolAuthGetter = () -> CompletableFuture.completedFuture(idToken);

Expand All @@ -114,20 +114,20 @@ public static void main(String[] args) {
})
.thenCompose(tool -> {
System.out.println(" -> Loaded Tool: " + tool.definition().description());

// STEP 4: TEST BINDING PARAMETERS SEQUENTIALLY
System.out.println("\n[A] Executing UNBOUND (Runtime arg: 'barbie')...");

return tool.execute(Map.of("description", "barbie"))
.thenCompose(result1 -> {
if (result1.content() != null && !result1.content().isEmpty()) {
System.out.println(" -> Result (Unbound): " + result1.content().get(0).text());
}

// NOW bind the parameter
System.out.println("\n[B] Binding 'description' to 'soft toy'...");
tool.bindParam("description", "soft toy");

System.out.println(" -> Executing BOUND (Runtime arg: 'barbie' - should be IGNORED)...");
// We pass 'barbie', but expecting 'soft toy' price because of binding override
return tool.execute(Map.of("description", "barbie"));
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/google/cloud/mcp/AuthTokenGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

import java.util.concurrent.CompletableFuture;

/**
* Functional interface for retrieving authentication tokens dynamically.
*/
/** Functional interface for retrieving authentication tokens dynamically. */
@FunctionalInterface
public interface AuthTokenGetter {
CompletableFuture<String> getToken();
CompletableFuture<String> getToken();
}
Loading
Loading