Add backwards compatibility constructors and tests (#395)

- Updated integration tests to include assertions for `meta` being
`null` in the `listTools`, `listResources`, `readResource`, and
`callTool` results.
- Renamed test cases in `SseServerIntegrationTest` for clarity.
- Added (deprecated) synthetic constructors for various request
parameter classes.
- Enhanced naming conventions and structure in integration test cases.
- Replaced `throw Error(...)` with `error(...)` for cleaner error
handling.

## Motivation and Context
To verify backwards-compatibility after schema change

## How Has This Been Tested?
CI, integration tests, regression tests (samples)

## Breaking Changes
<!-- Will users need to update their code or configurations? -->

## Types of changes
<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [x] Test update

## Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [x] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [x] My code follows the repository's style guidelines
- [ ] New and existing tests pass locally
- [ ] I have added appropriate error handling
- [ ] I have added or updated documentation as needed

## Additional context
<!-- Add any other context, implementation notes, or design decisions
-->
This commit is contained in:
Konstantin Pavlov 2025-11-12 20:18:57 +02:00 • committed by GitHub
commit 8bdbd36c32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,40 +1,83 @@
import io.modelcontextprotocol.kotlin.sdk.EmptyJsonObject
import io.modelcontextprotocol.kotlin.sdk.ReadResourceRequest
import io.modelcontextprotocol.kotlin.sdk.TextContent
import io.modelcontextprotocol.kotlin.sdk.TextResourceContents
import io.modelcontextprotocol.kotlin.sdk.client.Client
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
class SseServerIntegrationTest {
private val client: Client = TestEnvironment.client
@Test
fun `should get tools`(): Unit = runBlocking {
val tools = client.listTools().tools
fun `should list tools`(): Unit = runBlocking {
// when
val listToolsResult = client.listTools()
// then
assertEquals(expected = EmptyJsonObject, actual = listToolsResult._meta)
val tools = listToolsResult.tools
assertEquals(actual = tools.size, expected = 1)
assertEquals(expected = listOf("kotlin-sdk-tool"), actual = tools.map { it.name })
}
@Test
fun `should get prompts`(): Unit = runBlocking {
val prompts = client.listPrompts().prompts
fun `should list prompts`(): Unit = runBlocking {
// when
val listPromptsResult = client.listPrompts()
// then
assertEquals(expected = EmptyJsonObject, actual = listPromptsResult._meta)
val prompts = listPromptsResult.prompts
assertEquals(expected = listOf("Kotlin Developer"), actual = prompts.map { it.name })
}
@Test
fun `should get resources`(): Unit = runBlocking {
val resources = client.listResources().resources
fun `should list resources`(): Unit = runBlocking {
val listResourcesResult = client.listResources()
// then
assertEquals(expected = EmptyJsonObject, actual = listResourcesResult._meta)
val resources = listResourcesResult.resources
assertEquals(expected = listOf("Web Search"), actual = resources.map { it.name })
}
@Test
fun `should get resource`(): Unit = runBlocking {
val testResourceUri = "https://search.com/"
val getResourcesResult = client.readResource(
ReadResourceRequest(uri = testResourceUri),
)
// then
assertEquals(expected = EmptyJsonObject, actual = getResourcesResult._meta)
val contents = getResourcesResult.contents
assertEquals(expected = 1, actual = contents.size)
assertTrue {
contents.contains(
TextResourceContents("Placeholder content for $testResourceUri", testResourceUri, "text/html"),
)
}
}
@Test
fun `should call tool`(): Unit = runBlocking {
// when
val toolResult = client.callTool("kotlin-sdk-tool", EmptyJsonObject)
val content = toolResult?.content?.single()
// then
assertNotNull(toolResult)
assertEquals(expected = EmptyJsonObject, actual = toolResult._meta)
val content = toolResult.content.single()
assertIs<TextContent>(content, "Tool result should be a text content")
assertEquals(expected = "Hello, world!", actual = content.text)