-
Notifications
You must be signed in to change notification settings - Fork 165
Add StreamableHttpClientTest
with test infrastructure setup and dependencies
#316
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5519959
Add `StreamableHttpClientTest` with test infrastructure setup and dep…
kpavlov 3bb4c72
Update Mokksy, set test timeout, set workflow timeout
kpavlov de4c903
Add `StreamableHttpClientTest` with test infrastructure setup and dep…
kpavlov 4226096
Update Mokksy, set test timeout, set workflow timeout
kpavlov 73573f0
Add `--no-daemon` flag to Gradle tasks in build workflow for better C…
kpavlov c96b2d9
Merge remote-tracking branch 'origin/kpavlov/streamable-client-transp…
kpavlov d82fc22
Update build configuration
kpavlov ecced12
Update Accept header for SSE requests to use `application/json`, `tex…
kpavlov 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
@file:OptIn(ExperimentalWasmDsl::class) | ||
|
||
import org.gradle.api.tasks.testing.logging.TestExceptionFormat | ||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl | ||
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode | ||
import org.jetbrains.kotlin.gradle.dsl.JvmDefaultMode | ||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
|
||
plugins { | ||
|
@@ -11,11 +13,50 @@ plugins { | |
} | ||
|
||
kotlin { | ||
|
||
compilerOptions { | ||
freeCompilerArgs = | ||
listOf( | ||
"-Wextra", | ||
"-Xmulti-dollar-interpolation", | ||
) | ||
} | ||
|
||
// coreLibrariesVersion = "2.0.10" | ||
|
||
jvm { | ||
compilerOptions.jvmTarget = JvmTarget.JVM_1_8 | ||
compilerOptions { | ||
jvmTarget = JvmTarget.JVM_1_8 | ||
javaParameters = true | ||
jvmDefault = JvmDefaultMode.ENABLE | ||
freeCompilerArgs.addAll( | ||
"-Xdebug", | ||
) | ||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
|
||
useJUnitPlatform() | ||
|
||
maxParallelForks = Runtime.getRuntime().availableProcessors() | ||
forkEvery = 100 | ||
testLogging { | ||
exceptionFormat = TestExceptionFormat.SHORT | ||
showStandardStreams = true | ||
events("failed") | ||
} | ||
systemProperty("kotest.output.ansi", "true") | ||
reports { | ||
junitXml.required.set(true) | ||
junitXml.includeSystemOutLog.set(true) | ||
junitXml.includeSystemErrLog.set(true) | ||
Comment on lines
+51
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for visibility |
||
} | ||
} | ||
} | ||
macosX64(); macosArm64() | ||
linuxX64(); linuxArm64() | ||
macosX64() | ||
macosArm64() | ||
linuxX64() | ||
linuxArm64() | ||
mingwX64() | ||
js { nodejs() } | ||
wasmJs { nodejs() } | ||
|
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,61 @@ | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension | ||
|
||
/** | ||
* Netty convention plugin that adds platform-specific Netty native transport libraries | ||
* to the jvmTest source set. This is similar to how Maven handles OS-specific dependencies | ||
* with profile activation. | ||
* | ||
* This plugin should be applied to any module that uses Netty for testing. | ||
*/ | ||
val nettyVersion = "4.2.7.Final" | ||
plugins { | ||
id("org.gradle.base") | ||
} | ||
|
||
// This plugin is applied to projects that already have the kotlin multiplatform plugin applied | ||
// It adds the Netty native transport libraries to the jvmTest source set | ||
|
||
afterEvaluate { | ||
|
||
extensions.findByType<KotlinMultiplatformExtension>()?.apply { | ||
sourceSets.findByName("jvmTest")?.apply { | ||
dependencies { | ||
// Netty native transport libraries for different platforms | ||
val osName = System.getProperty("os.name").lowercase() | ||
val osArch = System.getProperty("os.arch").lowercase() | ||
|
||
// Add the base Netty platform | ||
implementation(project.dependencies.platform("io.netty:netty-bom:$nettyVersion")) | ||
|
||
when { | ||
osName.contains("linux") -> { | ||
val archClassifier = | ||
if (osArch.contains("aarch64")) { | ||
"linux-aarch_64" | ||
} else { | ||
"linux-x86_64" | ||
} | ||
runtimeOnly( | ||
"io.netty:netty-transport-native-epoll:$nettyVersion:$archClassifier", | ||
) | ||
} | ||
|
||
osName.contains("mac") -> { | ||
val archClassifier = | ||
if (osArch.contains("aarch64")) { | ||
"osx-aarch_64" | ||
} else { | ||
"osx-x86_64" | ||
} | ||
runtimeOnly( | ||
"io.netty:netty-transport-native-kqueue:$nettyVersion:$archClassifier", | ||
) | ||
runtimeOnly( | ||
"io.netty:netty-resolver-dns-native-macos:$nettyVersion:$archClassifier", | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
89 changes: 89 additions & 0 deletions
89
kotlin-sdk-client/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/MockMcp.kt
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,89 @@ | ||
package io.modelcontextprotocol.kotlin.sdk.client | ||
|
||
import dev.mokksy.mokksy.Mokksy | ||
import dev.mokksy.mokksy.StubConfiguration | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpMethod | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.sse.ServerSentEvent | ||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* High-level helper for simulating an MCP server over Streaming HTTP transport with Server-Sent Events (SSE), | ||
* built on top of an HTTP server using the [Mokksy](https://mokksy.dev) library. | ||
* | ||
* Provides test utilities to mock server behavior based on specific request conditions. | ||
* | ||
* @param verbose Whether to print detailed logs. Defaults to `false`. | ||
* @author Konstantin Pavlov | ||
*/ | ||
internal class MockMcp(verbose: Boolean = false) { | ||
|
||
private val mokksy: Mokksy = Mokksy(verbose = verbose) | ||
|
||
fun checkForUnmatchedRequests() { | ||
mokksy.checkForUnmatchedRequests() | ||
} | ||
|
||
val url = mokksy.baseUrl() + "/mcp" | ||
|
||
@Suppress("LongParameterList") | ||
fun onJSONRPCRequest( | ||
httpMethod: HttpMethod = HttpMethod.Post, | ||
jsonRpcMethod: String, | ||
expectedSessionId: String? = null, | ||
sessionId: String, | ||
contentType: ContentType = ContentType.Application.Json, | ||
statusCode: HttpStatusCode = HttpStatusCode.OK, | ||
bodyBuilder: () -> String, | ||
) { | ||
mokksy.method( | ||
configuration = StubConfiguration(removeAfterMatch = true), | ||
httpMethod = httpMethod, | ||
requestType = JSONRPCRequest::class, | ||
) { | ||
path("/mcp") | ||
expectedSessionId?.let { | ||
containsHeader("Mcp-Session-Id", it) | ||
} | ||
bodyMatchesPredicates( | ||
{ | ||
it!!.method == jsonRpcMethod | ||
}, | ||
{ | ||
it!!.jsonrpc == "2.0" | ||
}, | ||
) | ||
} respondsWith { | ||
body = bodyBuilder.invoke() | ||
this.contentType = contentType | ||
headers += "Mcp-Session-Id" to sessionId | ||
httpStatus = statusCode | ||
} | ||
} | ||
|
||
fun onSubscribeWithGet(sessionId: String, block: () -> Flow<ServerSentEvent>) { | ||
mokksy.get(name = "MCP GETs", requestType = Any::class) { | ||
path("/mcp") | ||
containsHeader("Mcp-Session-Id", sessionId) | ||
containsHeader("Accept", "application/json,text/event-stream") | ||
containsHeader("Cache-Control", "no-store") | ||
} respondsWithSseStream { | ||
headers += "Mcp-Session-Id" to sessionId | ||
this.flow = block.invoke() | ||
} | ||
} | ||
|
||
fun mockUnsubscribeRequest(sessionId: String) { | ||
mokksy.delete( | ||
configuration = StubConfiguration(removeAfterMatch = true), | ||
requestType = JSONRPCRequest::class, | ||
) { | ||
path("/mcp") | ||
containsHeader("Mcp-Session-Id", sessionId) | ||
} respondsWith { | ||
body = 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.
for visibility