Add experimental mcpClient(...) factory method (#396)

- Introduced `@ExperimentalMcpApi` annotation to mark experimental APIs.
- Added `mcpClient` function for initializing and connecting MCP
clients.
- Modified tests to utilize the new `mcpClient` function.
- Refactored variable naming in transport tests for improved
readability.

## Motivation and Context
To improve the creation of the MCP Client experience.

## How Has This Been Tested?
Integration test updated

## Breaking Changes
No

## 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)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation 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
- [x] 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:53:25 +02:00 • committed by GitHub
commit 93d544e618
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
package io.modelcontextprotocol.kotlin.sdk.client
import io.github.oshai.kotlinlogging.KotlinLogging
import io.modelcontextprotocol.kotlin.sdk.ExperimentalMcpApi
import io.modelcontextprotocol.kotlin.sdk.shared.Protocol
import io.modelcontextprotocol.kotlin.sdk.shared.ProtocolOptions
import io.modelcontextprotocol.kotlin.sdk.shared.RequestOptions
@ -69,6 +70,30 @@ public class ClientOptions(
enforceStrictCapabilities: Boolean = true,
) : ProtocolOptions(enforceStrictCapabilities = enforceStrictCapabilities)
/**
* Initializes and connects an MCP client using the provided clientInfo [Implementation], client options,
* and transport mechanism.
*
* @param clientInfo The implementation details of the MCP client, including its name, version, and other metadata.
* @param clientOptions Optional client configuration settings, such as supported capabilities
* and strict enforcement options. Defaults to a new instance of [ClientOptions].
* @param transport The transport mechanism used for communication.
* @return An instance of [Client] that is connected and ready for use with the specified transport.
*/
@ExperimentalMcpApi
public suspend fun mcpClient(
clientInfo: Implementation,
clientOptions: ClientOptions = ClientOptions(),
transport: Transport,
): Client {
val client = Client(
clientInfo = clientInfo,
options = clientOptions,
)
client.connect(transport)
return client
}
/**
* An MCP client on top of a pluggable transport.
*