-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(mcp): support multi-level data extraction #2399
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
175 changes: 175 additions & 0 deletions
175
...t/java/com/alibaba/cloud/ai/mcp/gateway/core/jsontemplate/ResponseTemplateParserTest.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,175 @@ | ||
/* | ||
* Copyright 2024-2025 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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.alibaba.cloud.ai.mcp.gateway.core.jsontemplate; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class ResponseTemplateParserTest { | ||
|
||
@Test | ||
void shouldReturnRawResponseWhenTemplateIsEmpty() { | ||
String rawResponse = "{\"status\": \"success\"}"; | ||
String template = ""; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals(rawResponse, result); | ||
} | ||
|
||
@Test | ||
void shouldReturnRawResponseWhenTemplateIsRootAccess() { | ||
String rawResponse = "{\"status\": \"success\"}"; | ||
String template = "{{.}}"; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals(rawResponse, result); | ||
} | ||
|
||
@Test | ||
void shouldExtractSingleLevelValue() { | ||
String rawResponse = "{\"status\": \"success\", \"code\": 200}"; | ||
String template = "Status: {{.status}}"; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("Status: success", result); | ||
} | ||
|
||
@Test | ||
void shouldExtractNestedValue() { | ||
String rawResponse = "{\"location\": {\"province\": \"Zhejiang\", \"city\": \"Hangzhou\"}, \"code\": 200}"; | ||
String template = "The city is {{.location.city}} and the code is {{.code}}."; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("The city is Hangzhou and the code is 200.", result); | ||
} | ||
|
||
@Test | ||
void shouldExtractDeeplyNestedValue() { | ||
String rawResponse = "{\"data\": {\"user\": {\"profile\": {\"name\": \"Alice\", \"age\": 30}}, \"timestamp\": 1234567890}}"; | ||
String template = "User {{.data.user.profile.name}} is {{.data.user.profile.age}} years old."; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("User Alice is 30 years old.", result); | ||
} | ||
|
||
@Test | ||
void shouldHandleSpacesInTemplate() { | ||
String rawResponse = "{\"data\": {\"value\": \"test\"}}"; | ||
String template = "The value is {{ .data.value }}."; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("The value is test.", result); | ||
} | ||
|
||
@Test | ||
void shouldHandleMultipleNestedReplacements() { | ||
String rawResponse = "{\"weather\": {\"temperature\": 25, \"humidity\": 60}, \"location\": {\"city\": \"Beijing\"}}"; | ||
String template = "Temperature in {{.location.city}} is {{.weather.temperature}}°C with {{.weather.humidity}}% humidity."; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("Temperature in Beijing is 25°C with 60% humidity.", result); | ||
} | ||
|
||
@Test | ||
void shouldHandleMissingNestedKey() { | ||
String rawResponse = "{\"location\": {\"city\": \"Hangzhou\"}}"; | ||
String template = "City: {{.location.city}}, Country: {{.location.country}}"; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("City: Hangzhou, Country: ", result); | ||
} | ||
|
||
@Test | ||
void shouldHandleArrayAccess() { | ||
String rawResponse = "{\"users\": [{\"name\": \"Alice\"}, {\"name\": \"Bob\"}]}"; | ||
String template = "First user: {{.users.0.name}}"; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("First user: Alice", result); | ||
} | ||
|
||
@Test | ||
void shouldWorkWithJsonPathWhenStartsWithDollar() { | ||
String rawResponse = "{\"location\": {\"city\": \"Hangzhou\"}}"; | ||
String template = "$.location.city"; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("Hangzhou", result); | ||
} | ||
|
||
@Test | ||
void shouldFallbackToSimpleTemplateWhenNoMultiLevel() { | ||
String rawResponse = "{\"status\": \"success\", \"message\": \"OK\"}"; | ||
String template = "{{.status}}: {{.message}}"; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("success: OK", result); | ||
} | ||
|
||
@Test | ||
void shouldHandleNonJsonResponse() { | ||
String rawResponse = "Simple text response"; | ||
String template = "Response: {{.}}"; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("Response: Simple text response", result); | ||
} | ||
|
||
@Test | ||
void shouldHandleComplexNestedStructure() { | ||
String rawResponse = "{\"api\": {\"response\": {\"data\": {\"items\": [{\"id\": 1, \"name\": \"Item1\"}, {\"id\": 2, \"name\": \"Item2\"}], \"total\": 2}, \"status\": {\"code\": 200, \"message\": \"success\"}}}}"; | ||
String template = "Status: {{.api.response.status.message}}, Total items: {{.api.response.data.total}}, First item: {{.api.response.data.items.[0].name}}"; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("Status: success, Total items: 2, First item: Item1", result); | ||
} | ||
|
||
@Test | ||
void shouldHandleNumbersAndBooleans() { | ||
String rawResponse = "{\"config\": {\"enabled\": true, \"maxRetries\": 3, \"timeout\": 30.5}}"; | ||
String template = "Enabled: {{.config.enabled}}, Max retries: {{.config.maxRetries}}, Timeout: {{.config.timeout}}"; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
assertEquals("Enabled: true, Max retries: 3, Timeout: 30.5", result); | ||
} | ||
|
||
@Test | ||
void shouldHandleEmptyObjectsAndNulls() { | ||
String rawResponse = "{\"data\": null, \"empty\": {}, \"info\": {\"value\": \"test\"}}"; | ||
String template = "Data: {{.data}}, Info: {{.info.value}}"; | ||
|
||
String result = ResponseTemplateParser.parse(rawResponse, template); | ||
|
||
// Handlebars 将 null 值渲染为空字符串,这是符合模板引擎标准行为的 | ||
assertEquals("Data: , Info: test", result); | ||
} | ||
|
||
} |
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.