-
Notifications
You must be signed in to change notification settings - Fork 860
feat!: #697 added tool input arguments validation #873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ashakirin
wants to merge
6
commits into
modelcontextprotocol:main
Choose a base branch
from
ashakirin:feature/697-tools-input-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
18c626b
feat!: #697 added tool input arguments validation causes tool executi…
ashakirin 219bc3c
Merge branch 'main' into feature/697-tools-input-validation
Kehrlann 27f3a38
Update mcp-core/src/main/java/io/modelcontextprotocol/util/ToolInputV…
ashakirin 7610e99
Update mcp-test/src/test/java/io/modelcontextprotocol/server/ToolInpu…
ashakirin 1cabd8f
chore: #697 fixed author name
ashakirin ec41a40
feat: #697 removed system property option to change default validatio…
ashakirin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
mcp-core/src/main/java/io/modelcontextprotocol/util/ToolInputValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright 2026-2026 the original author or authors. | ||
| */ | ||
|
|
||
| package io.modelcontextprotocol.util; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import io.modelcontextprotocol.json.McpJsonMapper; | ||
| import io.modelcontextprotocol.json.TypeRef; | ||
| import io.modelcontextprotocol.json.schema.JsonSchemaValidator; | ||
| import io.modelcontextprotocol.spec.McpSchema; | ||
| import io.modelcontextprotocol.spec.McpSchema.CallToolResult; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Validates tool input arguments against JSON schema. | ||
| * | ||
| * @author Andrei Shakirin | ||
| */ | ||
| public final class ToolInputValidator { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(ToolInputValidator.class); | ||
|
|
||
| private ToolInputValidator() { | ||
| } | ||
|
|
||
| /** | ||
| * Validates tool arguments against the tool's input schema. | ||
| * @param tool the tool definition containing the input schema | ||
| * @param arguments the arguments to validate | ||
| * @param validateToolInputs whether validation is enabled | ||
| * @param jsonMapper the JSON mapper for schema conversion | ||
| * @param validator the JSON schema validator (may be null) | ||
| * @return CallToolResult with isError=true if validation fails, null if valid or | ||
| * validation skipped | ||
| */ | ||
| public static CallToolResult validate(McpSchema.Tool tool, Map<String, Object> arguments, | ||
| boolean validateToolInputs, McpJsonMapper jsonMapper, JsonSchemaValidator validator) { | ||
| if (!validateToolInputs || tool.inputSchema() == null || validator == null) { | ||
| return null; | ||
| } | ||
| Map<String, Object> inputSchema = jsonMapper.convertValue(tool.inputSchema(), | ||
| new TypeRef<Map<String, Object>>() { | ||
| }); | ||
| Map<String, Object> args = arguments != null ? arguments : Map.of(); | ||
| var validation = validator.validate(inputSchema, args); | ||
| if (!validation.valid()) { | ||
| logger.warn("Tool '{}' input validation failed: {}", tool.name(), validation.errorMessage()); | ||
| return CallToolResult.builder() | ||
| .content(List.of(new McpSchema.TextContent(validation.errorMessage()))) | ||
| .isError(true) | ||
| .build(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should leave this marshalling on the hot path. Look at what we've done in
DefaultJsonSchemaValidator, there's a schema cache to avoid specifically doing to much of these operations.Maybe we should have something similar here? Or maybe we should update the
JsonSchemaValidatorto also validateJsonSchemaobjects?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#886 points out similar issues ; there are open PRs about it #749
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for pointing it, I will wait until #749 is merged