Update Client.connect(transport:) to automatically send initialize request (#100)

* Update Client to automatically call initialize in connect

* Make Client.connect(transport:) return value discardable

* Fix example usage in README

* Update example code in transports to remove initialize call

* Deprecate initialize method

* Update tests

* Update README

* Fix sampling tests

* Fix race condition tests
This commit is contained in:
Mattt 2025-05-26 06:04:38 -07:00 • committed by GitHub
commit a59b50bf5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -54,10 +54,7 @@ let client = Client(name: "MyApp", version: "1.0.0")
// Create a transport and connect
let transport = StdioTransport()
try await client.connect(transport: transport)
// Initialize the connection
let result = try await client.initialize()
let result = try await client.connect(transport: transport)
// Check server capabilities
if result.capabilities.tools != nil {
@ -65,6 +62,11 @@ if result.capabilities.tools != nil {
}
```
> [!NOTE]
> The `Client.connect(transport:)` method returns the initialization result.
> This return value is discardable,
> so you can ignore it if you don't need to check server capabilities.
### Transport Options for Clients
#### Stdio Transport
@ -278,7 +280,7 @@ Handle common client errors:
```swift
do {
let result = try await client.initialize()
try await client.connect(transport: transport)
// Success
} catch let error as MCPError {
print("MCP Error: \(error.localizedDescription)")
@ -402,7 +404,7 @@ The server component allows your application to host model capabilities and resp
```swift
import MCP
// Initialize the server with capabilities
// Create a server with given capabilities
let server = Server(
name: "MyModelServer",
version: "1.0.0",