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 @@ -57,6 +57,7 @@ public class SchemaOnlyTool extends ToolBase {

private final Boolean strict;
private final Boolean deferLoading;
private final Map<String, Object> outputSchema;

/**
* Creates a new SchemaOnlyTool from a ToolSchema.
Expand All @@ -71,7 +72,8 @@ public SchemaOnlyTool(ToolSchema schema) {
schema.getDescription(),
schema.getParameters(),
schema.getStrict(),
schema.getDeferLoading());
schema.getDeferLoading(),
schema.getOutputSchema());
}

/**
Expand Down Expand Up @@ -109,6 +111,32 @@ public SchemaOnlyTool(
Map<String, Object> parameters,
Boolean strict,
Boolean deferLoading) {
this(name, description, parameters, strict, deferLoading, null);
}

/**
* Creates a new SchemaOnlyTool with the specified name, description, parameters, strict mode,
* deferred loading, and output schema.
*
* <p>The parameters and output schema maps are defensively copied to prevent external
* mutations from affecting the tool's internal state.
*
* @param name The tool name
* @param description The tool description
* @param parameters The tool parameters schema
* @param strict Whether the tool should use strict schema validation (null if unspecified)
* @param deferLoading Whether to defer loading this tool's schema until tool search (null if
* unspecified)
* @param outputSchema The structured output schema the tool declares, if any
* @throws NullPointerException if name or description is null
*/
public SchemaOnlyTool(
String name,
String description,
Map<String, Object> parameters,
Boolean strict,
Boolean deferLoading,
Map<String, Object> outputSchema) {
super(
ToolBase.builder()
.name(Objects.requireNonNull(name, "name cannot be null"))
Expand All @@ -123,6 +151,10 @@ public SchemaOnlyTool(
.concurrencySafe(true));
this.strict = strict;
this.deferLoading = deferLoading;
this.outputSchema =
outputSchema != null
? Collections.unmodifiableMap(new HashMap<>(outputSchema))
: null;
}

/**
Expand Down Expand Up @@ -152,6 +184,11 @@ public Boolean getDeferLoading() {
return deferLoading;
}

@Override
public Map<String, Object> getOutputSchema() {
return outputSchema;
}

/**
* Throws a ToolSuspendException to signal that this tool requires external execution.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,49 @@ void testDeferLoadingPreservedFromToolSchema() {
assertEquals(Boolean.TRUE, tool.getDeferLoading());
}

@Test
@DisplayName("Should preserve outputSchema when creating from ToolSchema")
void testOutputSchemaPreservedFromToolSchema() {
Map<String, Object> outputSchema =
Map.of("type", "object", "properties", Map.of("answer", Map.of("type", "string")));
ToolSchema schemaWithOutput =
ToolSchema.builder()
.name("external_lookup")
.description("Look something up externally")
.parameters(Map.of("type", "object", "properties", Map.of()))
.outputSchema(outputSchema)
.build();

SchemaOnlyTool tool = new SchemaOnlyTool(schemaWithOutput);

assertEquals(outputSchema, tool.getOutputSchema());
}

@Test
@DisplayName("Should keep the declared outputSchema through Toolkit.getToolSchemas")
void testOutputSchemaSurvivesToolkitRegistration() {
Map<String, Object> outputSchema =
Map.of("type", "object", "properties", Map.of("answer", Map.of("type", "string")));
ToolSchema schema =
ToolSchema.builder()
.name("external_lookup")
.description("Look something up externally")
.parameters(Map.of("type", "object", "properties", Map.of()))
.outputSchema(outputSchema)
.build();
Toolkit toolkit = new Toolkit();

toolkit.registerSchema(schema);

assertEquals(
outputSchema,
toolkit.getToolSchemas().stream()
.filter(exposed -> "external_lookup".equals(exposed.getName()))
.findFirst()
.orElseThrow()
.getOutputSchema());
}

@Test
@DisplayName("Should create SchemaOnlyTool with name, description, parameters")
void testCreateWithParameters() {
Expand Down
Loading