Skip to content
Open
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 @@ -346,13 +346,20 @@ private Map<String, ToolDefinition> handleListToolsResponse(HttpResponse<String>
}
}

Object defaultValue = null;
if (propNode.has("default")) {
JsonNode defNode = propNode.get("default");
defaultValue = objectMapper.treeToValue(defNode, Object.class);
}

params.add(
new ToolDefinition.Parameter(
paramName,
paramType,
requiredSet.contains(paramName),
paramDesc,
authSources));
authSources,
defaultValue));
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/google/cloud/mcp/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ private void validateAndSanitizeArgs(Map<String, Object> args) {
for (ToolDefinition.Parameter param : definition.parameters()) {
Object value = args.get(param.name());

if (value == null && param.defaultValue() != null) {
value = param.defaultValue();
args.put(param.name(), value);
}

// A. Check Required Parameters
if (param.required() && value == null) {
throw new IllegalArgumentException(
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/google/cloud/mcp/ToolDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.mcp;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

/**
Expand All @@ -36,13 +37,14 @@ public record ToolDefinition(
* @param required Whether the parameter is required.
* @param description A description of the parameter.
* @param authSources A list of authentication sources for this parameter.
* @param defaultValue The default value for the parameter.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public record Parameter(
String name,
String type,
boolean required,
String description,
List<String> authSources // Maps services to parameters
) {}
List<String> authSources, // Maps services to parameters
@JsonProperty("default") Object defaultValue) {}
}
113 changes: 113 additions & 0 deletions src/test/java/com/google/cloud/mcp/ToolTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.mcp;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

class ToolTest {

@Test
void testDefaultValueInjection() throws Exception {
McpToolboxClient mockClient = mock(McpToolboxClient.class);

ToolDefinition.Parameter paramWithDefault =
new ToolDefinition.Parameter(
"param1", "string", false, "A parameter", null, "default_value");
ToolDefinition.Parameter paramNoDefault =
new ToolDefinition.Parameter("param2", "string", false, "Another parameter", null, null);

ToolDefinition def =
new ToolDefinition("A test tool", List.of(paramWithDefault, paramNoDefault), null);

Tool tool = new Tool("testTool", def, mockClient);

when(mockClient.invokeTool(eq("testTool"), any(), any()))
.thenReturn(
CompletableFuture.completedFuture(new ToolResult(Collections.emptyList(), false)));

Map<String, Object> args = new HashMap<>();
args.put("param2", "provided_value");

CompletableFuture<ToolResult> future = tool.execute(args);
future.join(); // Wait for execution

@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> argsCaptor = ArgumentCaptor.forClass(Map.class);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, String>> headersCaptor = ArgumentCaptor.forClass(Map.class);

verify(mockClient).invokeTool(eq("testTool"), argsCaptor.capture(), headersCaptor.capture());

Map<String, Object> capturedArgs = argsCaptor.getValue();

assertEquals(
"default_value",
capturedArgs.get("param1"),
"Default value should be injected when not provided");
assertEquals("provided_value", capturedArgs.get("param2"), "Provided value should be kept");
}

@Test
void testDefaultValueNotOverwritten() throws Exception {
McpToolboxClient mockClient = mock(McpToolboxClient.class);

ToolDefinition.Parameter paramWithDefault =
new ToolDefinition.Parameter(
"param1", "string", false, "A parameter", null, "default_value");

ToolDefinition def = new ToolDefinition("A test tool", List.of(paramWithDefault), null);

Tool tool = new Tool("testTool", def, mockClient);

when(mockClient.invokeTool(eq("testTool"), any(), any()))
.thenReturn(
CompletableFuture.completedFuture(new ToolResult(Collections.emptyList(), false)));

Map<String, Object> args = new HashMap<>();
args.put("param1", "custom_value");

CompletableFuture<ToolResult> future = tool.execute(args);
future.join(); // Wait for execution

@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> argsCaptor = ArgumentCaptor.forClass(Map.class);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, String>> headersCaptor = ArgumentCaptor.forClass(Map.class);

verify(mockClient).invokeTool(eq("testTool"), argsCaptor.capture(), headersCaptor.capture());

Map<String, Object> capturedArgs = argsCaptor.getValue();

assertEquals(
"custom_value",
capturedArgs.get("param1"),
"Provided value should not be overwritten by default value");
}
}