-
Notifications
You must be signed in to change notification settings - Fork 793
Moves mcp-json API back into mcp-core for simplified dependencies and support of osgi runtimes #762
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
scottslewis
wants to merge
5
commits into
modelcontextprotocol:main
Choose a base branch
from
scottslewis:issue_612_jackson3
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.
+524
−99
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
003eb6e
Rebasing on main after #742 merged. Moves mcp-json API back into
scottslewis bedf075
Fix for missing <scope>test</scope> in mcp-core. Thanks to
scottslewis fbda61f
Update mcp-core/src/main/java/io/modelcontextprotocol/util/McpService…
scottslewis 76ce84d
Removed DefaultMcpJsonMapperSupplier
scottslewis e6041be
Merge branch 'issue_612_jackson3' of https://github.com/scottslewis/m…
scottslewis 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
Some comments aren't visible on the classic Files Changed page.
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
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
50 changes: 50 additions & 0 deletions
50
mcp-core/src/main/java/io/modelcontextprotocol/json/McpJsonDefaults.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,50 @@ | ||
| package io.modelcontextprotocol.json; | ||
|
|
||
| import io.modelcontextprotocol.json.schema.JsonSchemaValidator; | ||
| import io.modelcontextprotocol.json.schema.JsonSchemaValidatorSupplier; | ||
| import io.modelcontextprotocol.util.McpServiceLoader; | ||
|
|
||
| public class McpJsonDefaults { | ||
|
|
||
| protected static McpServiceLoader<McpJsonMapperSupplier, McpJsonMapper> mcpMapperServiceLoader; | ||
|
|
||
| protected static McpServiceLoader<JsonSchemaValidatorSupplier, JsonSchemaValidator> mcpValidatorServiceLoader; | ||
|
|
||
| public McpJsonDefaults() { | ||
| mcpMapperServiceLoader = new McpServiceLoader<McpJsonMapperSupplier, McpJsonMapper>( | ||
| McpJsonMapperSupplier.class); | ||
| mcpValidatorServiceLoader = new McpServiceLoader<JsonSchemaValidatorSupplier, JsonSchemaValidator>( | ||
| JsonSchemaValidatorSupplier.class); | ||
| } | ||
|
|
||
| void setMcpJsonMapperSupplier(McpJsonMapperSupplier supplier) { | ||
| mcpMapperServiceLoader.setSupplier(supplier); | ||
| } | ||
|
|
||
| void unsetMcpJsonMapperSupplier(McpJsonMapperSupplier supplier) { | ||
| mcpMapperServiceLoader.unsetSupplier(supplier); | ||
| } | ||
|
|
||
| public synchronized static McpJsonMapper getDefaultMcpJsonMapper() { | ||
| if (mcpMapperServiceLoader == null) { | ||
| new McpJsonDefaults(); | ||
| } | ||
| return mcpMapperServiceLoader.getDefault(); | ||
| } | ||
|
|
||
| void setJsonSchemaValidatorSupplier(JsonSchemaValidatorSupplier supplier) { | ||
| mcpValidatorServiceLoader.setSupplier(supplier); | ||
| } | ||
|
|
||
| void unsetJsonSchemaValidatorSupplier(JsonSchemaValidatorSupplier supplier) { | ||
| mcpValidatorServiceLoader.unsetSupplier(supplier); | ||
| } | ||
|
|
||
| public synchronized static JsonSchemaValidator getDefaultJsonSchemaValidator() { | ||
| if (mcpValidatorServiceLoader == null) { | ||
| new McpJsonDefaults(); | ||
| } | ||
| return mcpValidatorServiceLoader.getDefault(); | ||
| } | ||
|
|
||
| } | ||
90 changes: 90 additions & 0 deletions
90
mcp-core/src/main/java/io/modelcontextprotocol/json/McpJsonMapper.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,90 @@ | ||
| /* | ||
| * Copyright 2025 - 2025 the original author or authors. | ||
| */ | ||
|
|
||
| package io.modelcontextprotocol.json; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Abstraction for JSON serialization/deserialization to decouple the SDK from any | ||
| * specific JSON library. A default implementation backed by Jackson is provided in | ||
| * io.modelcontextprotocol.spec.json.jackson.JacksonJsonMapper. | ||
| */ | ||
| public interface McpJsonMapper { | ||
|
|
||
| /** | ||
| * Deserialize JSON string into a target type. | ||
| * @param content JSON as String | ||
| * @param type target class | ||
| * @return deserialized instance | ||
| * @param <T> generic type | ||
| * @throws IOException on parse errors | ||
| */ | ||
| <T> T readValue(String content, Class<T> type) throws IOException; | ||
|
|
||
| /** | ||
| * Deserialize JSON bytes into a target type. | ||
| * @param content JSON as bytes | ||
| * @param type target class | ||
| * @return deserialized instance | ||
| * @param <T> generic type | ||
| * @throws IOException on parse errors | ||
| */ | ||
| <T> T readValue(byte[] content, Class<T> type) throws IOException; | ||
|
|
||
| /** | ||
| * Deserialize JSON string into a parameterized target type. | ||
| * @param content JSON as String | ||
| * @param type parameterized type reference | ||
| * @return deserialized instance | ||
| * @param <T> generic type | ||
| * @throws IOException on parse errors | ||
| */ | ||
| <T> T readValue(String content, TypeRef<T> type) throws IOException; | ||
|
|
||
| /** | ||
| * Deserialize JSON bytes into a parameterized target type. | ||
| * @param content JSON as bytes | ||
| * @param type parameterized type reference | ||
| * @return deserialized instance | ||
| * @param <T> generic type | ||
| * @throws IOException on parse errors | ||
| */ | ||
| <T> T readValue(byte[] content, TypeRef<T> type) throws IOException; | ||
|
|
||
| /** | ||
| * Convert a value to a given type, useful for mapping nested JSON structures. | ||
| * @param fromValue source value | ||
| * @param type target class | ||
| * @return converted value | ||
| * @param <T> generic type | ||
| */ | ||
| <T> T convertValue(Object fromValue, Class<T> type); | ||
|
|
||
| /** | ||
| * Convert a value to a given parameterized type. | ||
| * @param fromValue source value | ||
| * @param type target type reference | ||
| * @return converted value | ||
| * @param <T> generic type | ||
| */ | ||
| <T> T convertValue(Object fromValue, TypeRef<T> type); | ||
|
|
||
| /** | ||
| * Serialize an object to JSON string. | ||
| * @param value object to serialize | ||
| * @return JSON as String | ||
| * @throws IOException on serialization errors | ||
| */ | ||
| String writeValueAsString(Object value) throws IOException; | ||
|
|
||
| /** | ||
| * Serialize an object to JSON bytes. | ||
| * @param value object to serialize | ||
| * @return JSON as bytes | ||
| * @throws IOException on serialization errors | ||
| */ | ||
| byte[] writeValueAsBytes(Object value) throws IOException; | ||
|
|
||
| } |
14 changes: 14 additions & 0 deletions
14
mcp-core/src/main/java/io/modelcontextprotocol/json/McpJsonMapperSupplier.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,14 @@ | ||
| /* | ||
| * Copyright 2025 - 2025 the original author or authors. | ||
| */ | ||
|
|
||
| package io.modelcontextprotocol.json; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Strategy interface for resolving a {@link McpJsonMapper}. | ||
| */ | ||
| public interface McpJsonMapperSupplier extends Supplier<McpJsonMapper> { | ||
|
|
||
| } |
44 changes: 44 additions & 0 deletions
44
mcp-core/src/main/java/io/modelcontextprotocol/json/TypeRef.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,44 @@ | ||
| /* | ||
| * Copyright 2025 - 2025 the original author or authors. | ||
| */ | ||
|
|
||
| package io.modelcontextprotocol.json; | ||
|
|
||
| import java.lang.reflect.ParameterizedType; | ||
| import java.lang.reflect.Type; | ||
|
|
||
| /** | ||
| * Captures generic type information at runtime for parameterized JSON (de)serialization. | ||
| * Usage: TypeRef<List<Foo>> ref = new TypeRef<>(){}; | ||
| */ | ||
| public abstract class TypeRef<T> { | ||
|
|
||
| private final Type type; | ||
|
|
||
| /** | ||
| * Constructs a new TypeRef instance, capturing the generic type information of the | ||
| * subclass. This constructor should be called from an anonymous subclass to capture | ||
| * the actual type arguments. For example: <pre> | ||
| * TypeRef<List<Foo>> ref = new TypeRef<>(){}; | ||
| * </pre> | ||
| * @throws IllegalStateException if TypeRef is not subclassed with actual type | ||
| * information | ||
| */ | ||
| protected TypeRef() { | ||
| Type superClass = getClass().getGenericSuperclass(); | ||
| if (superClass instanceof Class) { | ||
| throw new IllegalStateException("TypeRef constructed without actual type information"); | ||
| } | ||
| this.type = ((ParameterizedType) superClass).getActualTypeArguments()[0]; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the captured type information. | ||
| * @return the Type representing the actual type argument captured by this TypeRef | ||
| * instance | ||
| */ | ||
| public Type getType() { | ||
| return type; | ||
| } | ||
|
|
||
| } |
44 changes: 44 additions & 0 deletions
44
mcp-core/src/main/java/io/modelcontextprotocol/json/schema/JsonSchemaValidator.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,44 @@ | ||
| /* | ||
| * Copyright 2024-2024 the original author or authors. | ||
| */ | ||
| package io.modelcontextprotocol.json.schema; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Interface for validating structured content against a JSON schema. This interface | ||
| * defines a method to validate structured content based on the provided output schema. | ||
| * | ||
| * @author Christian Tzolov | ||
| */ | ||
| public interface JsonSchemaValidator { | ||
|
|
||
| /** | ||
| * Represents the result of a validation operation. | ||
| * | ||
| * @param valid Indicates whether the validation was successful. | ||
| * @param errorMessage An error message if the validation failed, otherwise null. | ||
| * @param jsonStructuredOutput The text structured content in JSON format if the | ||
| * validation was successful, otherwise null. | ||
| */ | ||
| record ValidationResponse(boolean valid, String errorMessage, String jsonStructuredOutput) { | ||
|
|
||
| public static ValidationResponse asValid(String jsonStructuredOutput) { | ||
| return new ValidationResponse(true, null, jsonStructuredOutput); | ||
| } | ||
|
|
||
| public static ValidationResponse asInvalid(String message) { | ||
| return new ValidationResponse(false, message, null); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validates the structured content against the provided JSON schema. | ||
| * @param schema The JSON schema to validate against. | ||
| * @param structuredContent The structured content to validate. | ||
| * @return A ValidationResponse indicating whether the validation was successful or | ||
| * not. | ||
| */ | ||
| ValidationResponse validate(Map<String, Object> schema, Object structuredContent); | ||
|
|
||
| } |
19 changes: 19 additions & 0 deletions
19
mcp-core/src/main/java/io/modelcontextprotocol/json/schema/JsonSchemaValidatorSupplier.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,19 @@ | ||
| /* | ||
| * Copyright 2025 - 2025 the original author or authors. | ||
| */ | ||
|
|
||
| package io.modelcontextprotocol.json.schema; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * A supplier interface that provides a {@link JsonSchemaValidator} instance. | ||
| * Implementations of this interface are expected to return a new or cached instance of | ||
| * {@link JsonSchemaValidator} when {@link #get()} is invoked. | ||
| * | ||
| * @see JsonSchemaValidator | ||
| * @see Supplier | ||
| */ | ||
| public interface JsonSchemaValidatorSupplier extends Supplier<JsonSchemaValidator> { | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.