-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the resources server feature
- Resource Templates are not supported yet
- Loading branch information
Showing
41 changed files
with
1,204 additions
and
207 deletions.
There are no files selected for viewing
This file contains 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 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
215 changes: 167 additions & 48 deletions
215
deployment/src/main/java/io/quarkiverse/mcp/server/deployment/McpServerProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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
47 changes: 47 additions & 0 deletions
47
deployment/src/test/java/io/quarkiverse/mcp/server/test/resources/MyResources.java
This file contains 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,47 @@ | ||
package io.quarkiverse.mcp.server.test.resources; | ||
|
||
import static io.quarkiverse.mcp.server.test.Checks.checkDuplicatedContext; | ||
import static io.quarkiverse.mcp.server.test.Checks.checkExecutionModel; | ||
import static io.quarkiverse.mcp.server.test.Checks.checkRequestContext; | ||
|
||
import java.util.List; | ||
|
||
import io.quarkiverse.mcp.server.Resource; | ||
import io.quarkiverse.mcp.server.ResourceResponse; | ||
import io.quarkiverse.mcp.server.TextResourceContents; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class MyResources { | ||
|
||
@Resource(uri = "file:///project/alpha") | ||
ResourceResponse alpha(String uri) { | ||
checkExecutionModel(true); | ||
checkDuplicatedContext(); | ||
checkRequestContext(); | ||
return new ResourceResponse(List.of(new TextResourceContents(uri, "1", null))); | ||
} | ||
|
||
@Resource(uri = "file:///project/uni_alpha") | ||
Uni<ResourceResponse> uni_alpha(String uri) { | ||
checkExecutionModel(false); | ||
checkDuplicatedContext(); | ||
checkRequestContext(); | ||
return Uni.createFrom().item(new ResourceResponse(List.of(new TextResourceContents(uri, "2", null)))); | ||
} | ||
|
||
@Resource(uri = "file:///project/bravo") | ||
TextResourceContents bravo(String uri) { | ||
checkExecutionModel(true); | ||
checkDuplicatedContext(); | ||
checkRequestContext(); | ||
return new TextResourceContents(uri, "3", null); | ||
} | ||
|
||
@Resource(uri = "file:///project/uni_bravo") | ||
Uni<TextResourceContents> uni_bravo() { | ||
checkExecutionModel(false); | ||
checkDuplicatedContext(); | ||
checkRequestContext(); | ||
return Uni.createFrom().item(new TextResourceContents("file:///foo", "4", null)); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
deployment/src/test/java/io/quarkiverse/mcp/server/test/resources/ResourcesTest.java
This file contains 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,94 @@ | ||
package io.quarkiverse.mcp.server.test.resources; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkiverse.mcp.server.test.Checks; | ||
import io.quarkiverse.mcp.server.test.McpClient; | ||
import io.quarkiverse.mcp.server.test.McpServerTest; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.http.ContentType; | ||
import io.vertx.core.json.JsonArray; | ||
import io.vertx.core.json.JsonObject; | ||
|
||
public class ResourcesTest extends McpServerTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = defaultConfig() | ||
.withApplicationRoot( | ||
root -> root.addClasses(McpClient.class, MyResources.class, Checks.class)); | ||
|
||
@Test | ||
public void testResources() throws URISyntaxException { | ||
URI endpoint = initClient(); | ||
|
||
JsonObject resourcesListMessage = newMessage("resources/list"); | ||
|
||
JsonObject resourcesListResponse = new JsonObject(given() | ||
.contentType(ContentType.JSON) | ||
.when() | ||
.body(resourcesListMessage.encode()) | ||
.post(endpoint) | ||
.then() | ||
.statusCode(200) | ||
.extract().body().asString()); | ||
|
||
JsonObject resourcesListResult = assertResponseMessage(resourcesListMessage, resourcesListResponse); | ||
assertNotNull(resourcesListResult); | ||
JsonArray resources = resourcesListResult.getJsonArray("resources"); | ||
assertEquals(4, resources.size()); | ||
|
||
// alpha, bravo, uni_alpha, uni_bravo | ||
assertResource(resources.getJsonObject(0), "alpha", null, "file:///project/alpha", null); | ||
assertResource(resources.getJsonObject(1), "bravo", null, "file:///project/bravo", null); | ||
assertResource(resources.getJsonObject(2), "uni_alpha", null, "file:///project/uni_alpha", null); | ||
assertResource(resources.getJsonObject(3), "uni_bravo", null, "file:///project/uni_bravo", null); | ||
|
||
assertResourceRead("1", "file:///project/alpha", endpoint, "file:///project/alpha"); | ||
assertResourceRead("2", "file:///project/uni_alpha", endpoint, "file:///project/uni_alpha"); | ||
assertResourceRead("3", "file:///project/bravo", endpoint, "file:///project/bravo"); | ||
assertResourceRead("4", "file:///foo", endpoint, "file:///project/uni_bravo"); | ||
} | ||
|
||
private void assertResource(JsonObject resource, String name, String description, String uri, String mimeType) { | ||
assertEquals(name, resource.getString("name")); | ||
if (description != null) { | ||
assertEquals(description, resource.getString("description")); | ||
} | ||
assertEquals(uri, resource.getString("uri")); | ||
if (mimeType != null) { | ||
assertEquals(description, resource.getString("mimeType")); | ||
} | ||
} | ||
|
||
private void assertResourceRead(String expectedText, String expectedUri, URI endpoint, String uri) { | ||
JsonObject resourceReadMessage = newMessage("resources/read") | ||
.put("params", new JsonObject() | ||
.put("uri", uri)); | ||
|
||
JsonObject resourceReadResponse = new JsonObject(given() | ||
.contentType(ContentType.JSON) | ||
.when() | ||
.body(resourceReadMessage.encode()) | ||
.post(endpoint) | ||
.then() | ||
.statusCode(200) | ||
.extract().body().asString()); | ||
|
||
JsonObject resourceReadResult = assertResponseMessage(resourceReadMessage, resourceReadResponse); | ||
assertNotNull(resourceReadResult); | ||
JsonArray contents = resourceReadResult.getJsonArray("contents"); | ||
assertEquals(1, contents.size()); | ||
JsonObject textContent = contents.getJsonObject(0); | ||
assertEquals(expectedText, textContent.getString("text")); | ||
assertEquals(expectedUri, textContent.getString("uri")); | ||
} | ||
|
||
} |
This file contains 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 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 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
40 changes: 40 additions & 0 deletions
40
...ent/src/test/java/io/quarkiverse/mcp/server/test/validation/DuplicateResourceUriTest.java
This file contains 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,40 @@ | ||
package io.quarkiverse.mcp.server.test.validation; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkiverse.mcp.server.Resource; | ||
import io.quarkiverse.mcp.server.ResourceResponse; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class DuplicateResourceUriTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(InvalidResources.class); | ||
}) | ||
.setExpectedException(IllegalStateException.class, true); | ||
|
||
@Test | ||
public void test() { | ||
fail(); | ||
} | ||
|
||
public static class InvalidResources { | ||
|
||
@Resource(uri = "baz") | ||
ResourceResponse foo() { | ||
return null; | ||
} | ||
|
||
@Resource(uri = "baz") | ||
ResourceResponse foos() { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.