Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,19 @@ 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 {
// Server supports tools (implicitly including tool calling if the 'tools' capability object is present)
}
```

> [!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
Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 0 additions & 3 deletions Sources/MCP/Base/Transports/HTTPClientTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ import Logging
/// let client = Client(name: "MyApp", version: "1.0.0")
/// try await client.connect(transport: transport)
///
/// // Initialize the connection
/// let result = try await client.initialize()
///
/// // The transport will automatically handle SSE events
/// // and deliver them through the client's notification handlers
/// ```
Expand Down
3 changes: 0 additions & 3 deletions Sources/MCP/Base/Transports/NetworkTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ import Logging
/// // Use the transport with an MCP client
/// let client = Client(name: "MyApp", version: "1.0.0")
/// try await client.connect(transport: transport)
///
/// // Initialize the connection
/// let result = try await client.initialize()
/// ```
public actor NetworkTransport: Transport {
/// Represents a heartbeat message for connection health monitoring.
Expand Down
3 changes: 0 additions & 3 deletions Sources/MCP/Base/Transports/StdioTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ import struct Foundation.Data
/// // Create a transport and connect
/// let transport = StdioTransport()
/// try await client.connect(transport: transport)
///
/// // Initialize the connection
/// let result = try await client.initialize()
/// ```
public actor StdioTransport: Transport {
private let input: FileDescriptor
Expand Down
22 changes: 21 additions & 1 deletion Sources/MCP/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ public actor Client {
}

/// Connect to the server using the given transport
public func connect(transport: any Transport) async throws {
@discardableResult
public func connect(transport: any Transport) async throws -> Initialize.Result {
self.connection = transport
try await self.connection?.connect()

Expand Down Expand Up @@ -217,6 +218,9 @@ public actor Client {
} while true
await self.logger?.info("Client message handling loop task is terminating.")
}

// Automatically initialize after connecting
return try await _initialize()
}

/// Disconnect the client and cancel all pending requests
Expand Down Expand Up @@ -480,7 +484,23 @@ public actor Client {

// MARK: - Lifecycle

/// Initialize the connection with the server.
///
/// - Important: This method is deprecated. Initialization now happens automatically
/// when calling `connect(transport:)`. You should use that method instead.
///
/// - Returns: The server's initialization response containing capabilities and server info
@available(
*, deprecated,
message:
"Initialization now happens automatically during connect. Use connect(transport:) instead."
)
public func initialize() async throws -> Initialize.Result {
return try await _initialize()
}

/// Internal initialization implementation
private func _initialize() async throws -> Initialize.Result {
let request = Initialize.request(
.init(
protocolVersion: Version.latest,
Expand Down
Loading