-
Notifications
You must be signed in to change notification settings - Fork 209
feat!: SEP-1577 sampling with tools #718
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
Open
devcrocod
wants to merge
1
commit into
main
Choose a base branch
from
devcrocod/sep-1577-sampling-with-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
251 changes: 251 additions & 0 deletions
251
...gration-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/SamplingTest.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,251 @@ | ||
| package io.modelcontextprotocol.kotlin.sdk.server | ||
|
|
||
| import io.modelcontextprotocol.kotlin.sdk.client.Client | ||
| import io.modelcontextprotocol.kotlin.sdk.client.ClientOptions | ||
| import io.modelcontextprotocol.kotlin.sdk.shared.InMemoryTransport | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ClientCapabilities | ||
| import io.modelcontextprotocol.kotlin.sdk.types.CreateMessageRequest | ||
| import io.modelcontextprotocol.kotlin.sdk.types.CreateMessageRequestParams | ||
| import io.modelcontextprotocol.kotlin.sdk.types.CreateMessageResult | ||
| import io.modelcontextprotocol.kotlin.sdk.types.EmptyJsonObject | ||
| import io.modelcontextprotocol.kotlin.sdk.types.Implementation | ||
| import io.modelcontextprotocol.kotlin.sdk.types.IncludeContext | ||
| import io.modelcontextprotocol.kotlin.sdk.types.Method | ||
| import io.modelcontextprotocol.kotlin.sdk.types.Role | ||
| import io.modelcontextprotocol.kotlin.sdk.types.SamplingMessage | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ServerCapabilities | ||
| import io.modelcontextprotocol.kotlin.sdk.types.StopReason | ||
| import io.modelcontextprotocol.kotlin.sdk.types.TextContent | ||
| import io.modelcontextprotocol.kotlin.sdk.types.Tool | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ToolChoice | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ToolResultContent | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ToolSchema | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ToolUseContent | ||
| import kotlinx.coroutines.CompletableDeferred | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.runBlocking | ||
| import kotlinx.serialization.json.JsonPrimitive | ||
| import kotlinx.serialization.json.buildJsonObject | ||
| import org.junit.jupiter.api.assertDoesNotThrow | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFailsWith | ||
|
|
||
| class SamplingTest { | ||
|
|
||
| private val dummyTool = Tool( | ||
| name = "t", | ||
| inputSchema = ToolSchema(properties = buildJsonObject { }, required = emptyList()), | ||
| ) | ||
|
|
||
| private val weatherTool = Tool( | ||
| name = "get_weather", | ||
| description = "Return the current temperature in Celsius.", | ||
| inputSchema = ToolSchema( | ||
| properties = buildJsonObject { | ||
| put("location", buildJsonObject { put("type", JsonPrimitive("string")) }) | ||
| }, | ||
| required = listOf("location"), | ||
| ), | ||
| ) | ||
|
|
||
| private val minimalMessages = listOf(SamplingMessage(Role.User, TextContent("hi"))) | ||
|
|
||
| /** | ||
| * Builds a connected [Server]+[Client] pair using [InMemoryTransport]. | ||
| * | ||
| * @param clientCapabilities the capabilities the client advertises during initialize | ||
| * @param samplingHandler the handler the client uses to respond to sampling requests | ||
| * @return Pair of (server, sessionId) ready for [Server.createMessage] calls. | ||
| */ | ||
| private fun buildPair( | ||
| clientCapabilities: ClientCapabilities = ClientCapabilities( | ||
| sampling = ClientCapabilities.Sampling(), | ||
| ), | ||
| samplingHandler: (CreateMessageRequest) -> CreateMessageResult = { _ -> | ||
| CreateMessageResult(role = Role.Assistant, content = TextContent("ok"), model = "m") | ||
| }, | ||
| ): Pair<Server, String> { | ||
| val server = Server( | ||
| serverInfo = Implementation(name = "srv", version = "1.0"), | ||
| options = ServerOptions(capabilities = ServerCapabilities()), | ||
| ) | ||
|
|
||
| val client = Client( | ||
| clientInfo = Implementation(name = "cli", version = "1.0"), | ||
| options = ClientOptions(capabilities = clientCapabilities), | ||
| ) | ||
|
|
||
| client.setRequestHandler<CreateMessageRequest>(Method.Defined.SamplingCreateMessage) { req, _ -> | ||
| samplingHandler(req) | ||
| } | ||
|
|
||
| val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair() | ||
|
|
||
| var sessionId: String? = null | ||
| runBlocking { | ||
| val sessionDeferred = CompletableDeferred<String>() | ||
| launch { client.connect(clientTransport) } | ||
| launch { | ||
| val session = server.createSession(serverTransport) | ||
| sessionDeferred.complete(session.sessionId) | ||
| } | ||
| sessionId = sessionDeferred.await() | ||
| } | ||
|
|
||
| return Pair(server, checkNotNull(sessionId)) | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Server.createMessage — capability enforcement (SEP-1577) | ||
| // ============================================================================ | ||
|
|
||
| @Test | ||
| fun `tools field rejected when client has no sampling tools capability`() { | ||
| val (server, sessionId) = buildPair() | ||
| assertFailsWith<IllegalArgumentException> { | ||
| runBlocking { | ||
| server.createMessage( | ||
| sessionId = sessionId, | ||
| params = CreateMessageRequest( | ||
| params = CreateMessageRequestParams( | ||
| maxTokens = 100, | ||
| messages = minimalMessages, | ||
| tools = listOf(dummyTool), | ||
| ), | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `toolChoice field rejected when client has no sampling tools capability`() { | ||
| val (server, sessionId) = buildPair() | ||
| assertFailsWith<IllegalArgumentException> { | ||
| runBlocking { | ||
| server.createMessage( | ||
| sessionId = sessionId, | ||
| params = CreateMessageRequest( | ||
| params = CreateMessageRequestParams( | ||
| maxTokens = 100, | ||
| messages = minimalMessages, | ||
| toolChoice = ToolChoice(), | ||
| ), | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `includeContext with no sampling context capability succeeds with a warning`() { | ||
| val (server, sessionId) = buildPair() | ||
| assertDoesNotThrow { | ||
| runBlocking { | ||
| server.createMessage( | ||
| sessionId = sessionId, | ||
| params = CreateMessageRequest( | ||
| params = CreateMessageRequestParams( | ||
| maxTokens = 100, | ||
| messages = minimalMessages, | ||
| includeContext = IncludeContext.ThisServer, | ||
| ), | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // End-to-end tool-loop integration | ||
| // ============================================================================ | ||
|
|
||
| @Test | ||
| fun `server sends tools, client returns tool_use then final text`() { | ||
| var turn = 0 | ||
| val (server, sessionId) = buildPair( | ||
| clientCapabilities = ClientCapabilities( | ||
| sampling = ClientCapabilities.Sampling(tools = EmptyJsonObject), | ||
| ), | ||
| samplingHandler = { _ -> | ||
| turn++ | ||
| if (turn == 1) { | ||
| CreateMessageResult( | ||
| role = Role.Assistant, | ||
| content = listOf( | ||
| TextContent("Let me check."), | ||
| ToolUseContent( | ||
| id = "call_1", | ||
| name = "get_weather", | ||
| input = buildJsonObject { put("location", JsonPrimitive("London")) }, | ||
| ), | ||
| ), | ||
| model = "test", | ||
| stopReason = StopReason.ToolUse, | ||
| ) | ||
| } else { | ||
| CreateMessageResult( | ||
| role = Role.Assistant, | ||
| content = TextContent("The temperature in London is 20°C."), | ||
| model = "test", | ||
| stopReason = StopReason.EndTurn, | ||
| ) | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| runBlocking { | ||
| val messages = mutableListOf( | ||
| SamplingMessage(Role.User, TextContent("What is the weather in London?")), | ||
| ) | ||
|
|
||
| // — Turn 1: server sends tools, expects tool_use stop reason | ||
| val first = server.createMessage( | ||
| sessionId = sessionId, | ||
| params = CreateMessageRequest( | ||
| params = CreateMessageRequestParams( | ||
| maxTokens = 256, | ||
| messages = messages.toList(), | ||
| tools = listOf(weatherTool), | ||
| toolChoice = ToolChoice(mode = ToolChoice.Mode.Auto), | ||
| ), | ||
| ), | ||
| ) | ||
| assertEquals(StopReason.ToolUse, first.stopReason) | ||
| assertEquals(2, first.content.size) | ||
|
|
||
| // — Append assistant turn and inject tool result | ||
| messages.add(SamplingMessage(Role.Assistant, first.content)) | ||
| val toolUse = first.content.filterIsInstance<ToolUseContent>().single() | ||
| messages.add( | ||
| SamplingMessage( | ||
| Role.User, | ||
| ToolResultContent( | ||
| toolUseId = toolUse.id, | ||
| content = listOf(TextContent("""{"tempC":20}""")), | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
| // — Turn 2: server sends updated history, expects final text | ||
| val second = server.createMessage( | ||
| sessionId = sessionId, | ||
| params = CreateMessageRequest( | ||
| params = CreateMessageRequestParams( | ||
| maxTokens = 256, | ||
| messages = messages.toList(), | ||
| tools = listOf(weatherTool), | ||
| toolChoice = ToolChoice(mode = ToolChoice.Mode.Auto), | ||
| ), | ||
| ), | ||
| ) | ||
| assertEquals(StopReason.EndTurn, second.stopReason) | ||
| assertEquals(1, second.content.size) | ||
| val text = second.content.single() as TextContent | ||
| assertEquals("The temperature in London is 20°C.", text.text) | ||
|
|
||
| server.close() | ||
| } | ||
| } | ||
| } |
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
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.
result.content.joinToString("\n") { it.toString() }will produce Kotlin data-classtoString()representations (e.g.TextContent(text=..., type=...)) rather than the model’s text output. If this conformance tool is intended to return the sampled text, consider extractingTextContent.text(and deciding how to handle non-text blocks/tool_use blocks) instead of relying ontoString().