|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.i3830; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 11 | + |
| 12 | +import java.util.concurrent.atomic.AtomicReference; |
| 13 | + |
| 14 | +import io.jooby.jackson3.Jackson3Module; |
| 15 | +import io.jooby.junit.ServerTest; |
| 16 | +import io.jooby.junit.ServerTestRunner; |
| 17 | +import io.jooby.mcp.McpModule; |
| 18 | + |
| 19 | +public class McpExchangeInjectionTest { |
| 20 | + |
| 21 | + @ServerTest |
| 22 | + public void shouldInjectExchangeAndAccessSession(ServerTestRunner runner) throws Exception { |
| 23 | + runner |
| 24 | + .define( |
| 25 | + app -> { |
| 26 | + app.install(new Jackson3Module()); |
| 27 | + // Register the module using the STREAMABLE_HTTP transport |
| 28 | + app.install( |
| 29 | + new McpModule(new CalculatorToolsMcp_()) |
| 30 | + .transport(McpModule.Transport.STREAMABLE_HTTP)); |
| 31 | + }) |
| 32 | + .ready( |
| 33 | + client -> { |
| 34 | + AtomicReference<String> sessionId = new AtomicReference<>(); |
| 35 | + |
| 36 | + // 1. STREAMABLE_HTTP requires a formal MCP initialize handshake via POST |
| 37 | + // to generate the session ID. |
| 38 | + String initRequest = |
| 39 | + """ |
| 40 | + { |
| 41 | + "jsonrpc": "2.0", |
| 42 | + "id": "init-1", |
| 43 | + "method": "initialize", |
| 44 | + "params": { |
| 45 | + "protocolVersion": "2024-11-05", |
| 46 | + "capabilities": {}, |
| 47 | + "clientInfo": { "name": "test-client", "version": "1.0.0" } |
| 48 | + } |
| 49 | + } |
| 50 | + """; |
| 51 | + |
| 52 | + // The transport provider strictly requires these Accept headers |
| 53 | + client.header("Accept", "text/event-stream, application/json"); |
| 54 | + client.header("Content-Type", "application/json"); |
| 55 | + |
| 56 | + client.postJson( |
| 57 | + "/mcp", |
| 58 | + initRequest, |
| 59 | + response -> { |
| 60 | + assertEquals(200, response.code()); |
| 61 | + |
| 62 | + // 2. Extract the session ID from the headers |
| 63 | + String header = response.header("mcp-session-id"); |
| 64 | + assertNotNull( |
| 65 | + header, "mcp-session-id header must be present in initialization response"); |
| 66 | + sessionId.set(header); |
| 67 | + }); |
| 68 | + |
| 69 | + // 3. Construct the Tool request |
| 70 | + String toolRequest = |
| 71 | + """ |
| 72 | + { |
| 73 | + "jsonrpc": "2.0", |
| 74 | + "id": "tool-1", |
| 75 | + "method": "tools/call", |
| 76 | + "params": { |
| 77 | + "name": "get_session_info", |
| 78 | + "arguments": {} |
| 79 | + } |
| 80 | + } |
| 81 | + """; |
| 82 | + |
| 83 | + // 4. Send the tool request, appending the session ID we just obtained |
| 84 | + client.header("Accept", "text/event-stream, application/json"); |
| 85 | + client.header("Content-Type", "application/json"); |
| 86 | + client.header("mcp-session-id", sessionId.get()); |
| 87 | + |
| 88 | + client.postJson( |
| 89 | + "/mcp", |
| 90 | + toolRequest, |
| 91 | + response -> { |
| 92 | + assertEquals(200, response.code()); |
| 93 | + |
| 94 | + var body = response.body().string(); |
| 95 | + |
| 96 | + assertThat(body) |
| 97 | + .contains("\"id\":\"tool-1\"") |
| 98 | + .as("The response should contain the calculated result"); |
| 99 | + ; |
| 100 | + }); |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
0 commit comments