|
16 | 16 |
|
17 | 17 | package cloudcode.simple; |
18 | 18 |
|
19 | | -import com.google.auth.oauth2.GoogleCredentials; |
20 | | -import com.google.auth.oauth2.IdTokenProvider; |
21 | | -import com.google.cloud.mcp.AuthTokenGetter; |
22 | 19 | import com.google.cloud.mcp.McpToolboxClient; |
23 | | -import java.io.FileInputStream; |
24 | | -import java.util.Collections; |
25 | 20 | import java.util.Map; |
26 | | -import java.util.concurrent.CompletableFuture; |
27 | 21 |
|
28 | 22 | /** |
29 | | - * Sample Application to demonstrate the usage of the MCP Toolbox Java SDK. Covers: Global Auth, |
30 | | - * Parameterized Auth, Discovery, Simple Tool, Authenticated Tool, Parameter Binding. |
| 23 | + * A minimal example demonstrating client initialization, tool discovery, and invocation. |
31 | 24 | */ |
32 | 25 | public class ExampleUsage { |
33 | 26 | public static void main(String[] args) { |
34 | | - // CONFIGURATION |
35 | 27 | String targetUrl = System.getProperty("toolbox.url", "YOUR_TOOLBOX_SERVICE_ENDPOINT"); |
36 | | - |
37 | | - // Match the Service URL if using Cloud Run OIDC |
38 | | - String tokenAudience = System.getProperty("toolbox.tokenAudience", targetUrl); |
39 | | - |
40 | | - // -------------------------------------------------------------------------------- |
41 | | - // AUTHENTICATION SETUP |
42 | | - // -------------------------------------------------------------------------------- |
43 | | - // FOR LOCAL DEVELOPMENT: Use a Service Account Key JSON file. |
44 | | - // FOR PRODUCTION (Cloud Run): Comment out the 'keyPath' logic and use ADC directly. |
45 | | - // -------------------------------------------------------------------------------- |
46 | | - |
47 | | - String keyPath = System.getProperty("toolbox.keyPath", "YOUR_CREDENTIALS_JSON_FILE_PATH.json"); |
48 | | - |
49 | | - System.out.println("--- Starting MCP Toolbox Integration Test ---"); |
50 | | - System.out.println("Target Server: " + targetUrl); |
51 | | - |
52 | | - try { |
53 | | - System.out.println(" [Init] Fetching ID Token..."); |
54 | | - |
55 | | - GoogleCredentials credentials; |
56 | | - |
57 | | - // --- OPTION A: LOCAL DEV (Explicit Key File) --- |
58 | | - if (keyPath != null && !keyPath.isEmpty()) { |
59 | | - System.out.println(" [Auth] Using Service Account Key File: " + keyPath); |
60 | | - credentials = GoogleCredentials.fromStream(new FileInputStream(keyPath)); |
61 | | - } |
62 | | - // --- OPTION B: PRODUCTION (ADC) --- |
63 | | - else { |
64 | | - System.out.println(" [Auth] Using Application Default Credentials (ADC)"); |
65 | | - credentials = GoogleCredentials.getApplicationDefault(); |
66 | | - } |
67 | | - |
68 | | - if (!(credentials instanceof IdTokenProvider)) { |
69 | | - throw new RuntimeException("Loaded credentials do not support ID Tokens."); |
70 | | - } |
71 | | - |
72 | | - // Generate Token for the specified Audience |
73 | | - String idToken = |
74 | | - ((IdTokenProvider) credentials) |
75 | | - .idTokenWithAudience(tokenAudience, Collections.emptyList()) |
76 | | - .getTokenValue(); |
77 | | - System.out.println(" [Debug] Token Generated."); |
78 | | - |
79 | | - // Initialize Client with Global Auth (Applies to ALL calls - Gate 1) |
80 | | - McpToolboxClient client = |
81 | | - McpToolboxClient.builder().baseUrl(targetUrl).apiKey(idToken).build(); |
82 | | - |
83 | | - // STEP 1: TEST DISCOVERY METHODS |
84 | | - client |
85 | | - .listTools() |
86 | | - .thenCompose( |
87 | | - tools -> { |
88 | | - System.out.println("\n[1] listTools(): Success. Found " + tools.size() + " tools."); |
89 | | - return client.loadToolset(); |
90 | | - }) |
91 | | - .thenCompose( |
92 | | - tools -> { |
93 | | - System.out.println("[2] loadToolset() (Alias): Success."); |
94 | | - return client |
95 | | - .loadToolset("retail") |
96 | | - .handle( |
97 | | - (res, ex) -> { |
98 | | - if (ex == null) |
99 | | - System.out.println( |
100 | | - "[3] loadToolset('retail'): Found " + res.size() + " tools."); |
101 | | - else |
102 | | - System.out.println( |
103 | | - "[3] loadToolset('retail'): Skipped (Not configured on server)."); |
104 | | - return null; |
105 | | - }); |
106 | | - }) |
107 | | - .thenCompose( |
108 | | - ignore -> { |
109 | | - |
110 | | - // STEP 2: INVOKE TOOL WITHOUT EXTRA AUTH |
111 | | - System.out.println("\n[4] Testing Simple Tool: 'get-retail-facet-filters'..."); |
112 | | - return client.invokeTool("get-retail-facet-filters", Map.of()); |
113 | | - }) |
114 | | - .thenCompose( |
115 | | - result -> { |
116 | | - System.out.println( |
117 | | - " -> Result: " + (result.content() != null ? "Received Data" : "Empty")); |
118 | | - |
119 | | - // STEP 3: INVOKE TOOL WITH AUTHENTICATED PARAMETERS |
120 | | - System.out.println("\n[5] Testing Authenticated Tool: 'get-toy-price'..."); |
121 | | - |
122 | | - // Define the getter for the 'google_auth' service |
123 | | - AuthTokenGetter toolAuthGetter = () -> CompletableFuture.completedFuture(idToken); |
124 | | - |
125 | | - // Load using the sophisticated overload |
126 | | - return client.loadTool("get-toy-price", Map.of("google_auth", toolAuthGetter)); |
127 | | - }) |
128 | | - .thenCompose( |
129 | | - tool -> { |
130 | | - System.out.println(" -> Loaded Tool: " + tool.definition().description()); |
131 | | - |
132 | | - // STEP 4: TEST BINDING PARAMETERS SEQUENTIALLY |
133 | | - System.out.println("\n[A] Executing UNBOUND (Runtime arg: 'barbie')..."); |
134 | | - |
135 | | - return tool.execute(Map.of("description", "barbie")) |
136 | | - .thenCompose( |
137 | | - result1 -> { |
138 | | - if (result1.content() != null && !result1.content().isEmpty()) { |
139 | | - System.out.println( |
140 | | - " -> Result (Unbound): " + result1.content().get(0).text()); |
141 | | - } |
142 | | - |
143 | | - // NOW bind the parameter |
144 | | - System.out.println("\n[B] Binding 'description' to 'soft toy'..."); |
145 | | - tool.bindParam("description", "soft toy"); |
146 | | - |
147 | | - System.out.println( |
148 | | - " -> Executing BOUND (Runtime arg: 'barbie' - should be" |
149 | | - + " IGNORED)..."); |
150 | | - // We pass 'barbie', but expecting 'soft toy' price because of binding |
151 | | - // override |
152 | | - return tool.execute(Map.of("description", "barbie")); |
153 | | - }); |
154 | | - }) |
155 | | - .thenAccept( |
156 | | - result -> { |
157 | | - System.out.println("\n[6] Final Result (Bound):"); |
158 | | - if (result.isError()) { |
159 | | - System.err.println("Tool execution failed: " + result.content().get(0).text()); |
160 | | - } else if (result.content() != null && !result.content().isEmpty()) { |
161 | | - String output = result.content().get(0).text(); |
162 | | - System.out.println( |
163 | | - " " + output.substring(0, Math.min(output.length(), 200)) + "..."); |
164 | | - } else { |
165 | | - System.out.println(" Empty Response"); |
166 | | - } |
167 | | - }) |
168 | | - .exceptionally( |
169 | | - ex -> { |
170 | | - System.err.println("\n!!! TEST FAILED !!!"); |
171 | | - ex.printStackTrace(); |
172 | | - return null; |
173 | | - }) |
174 | | - .join(); |
175 | | - |
176 | | - } catch (Exception e) { |
177 | | - e.printStackTrace(); |
178 | | - } |
179 | | - System.out.println("\n--- Test Suite Complete ---"); |
| 28 | + String apiKey = System.getProperty("toolbox.apiKey", "YOUR_API_KEY"); |
| 29 | + |
| 30 | + System.out.println("--- Starting MCP Toolbox Simple Example ---"); |
| 31 | + System.out.println("Connecting to: " + targetUrl); |
| 32 | + |
| 33 | + // Initialize the client |
| 34 | + McpToolboxClient client = |
| 35 | + McpToolboxClient.builder().baseUrl(targetUrl).apiKey(apiKey).build(); |
| 36 | + |
| 37 | + // 1. List available tools |
| 38 | + client |
| 39 | + .listTools() |
| 40 | + .thenAccept( |
| 41 | + tools -> { |
| 42 | + System.out.println("Available tools: " + tools.size()); |
| 43 | + for (String toolName : tools.keySet()) { |
| 44 | + System.out.println(" - " + toolName); |
| 45 | + } |
| 46 | + |
| 47 | + // 2. Invoke a simple tool |
| 48 | + if (tools.containsKey("get-retail-facet-filters")) { |
| 49 | + System.out.println("\nInvoking 'get-retail-facet-filters'..."); |
| 50 | + client |
| 51 | + .invokeTool("get-retail-facet-filters", Map.of()) |
| 52 | + .thenAccept( |
| 53 | + result -> { |
| 54 | + System.out.println("Result: " + result.content().get(0).text()); |
| 55 | + }) |
| 56 | + .join(); |
| 57 | + } |
| 58 | + }) |
| 59 | + .join(); |
| 60 | + |
| 61 | + System.out.println("\n--- Simple Example Complete ---"); |
180 | 62 | } |
181 | 63 | } |
0 commit comments