Skip to content

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

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
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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 @@ -52,19 +52,21 @@ import MCP
// Initialize the client
let client = Client(name: "MyApp", version: "1.0.0")

// Create a transport and connect
// Create a transport and connect - initialization happens automatically
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 @@ -195,7 +197,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
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 @@ -32,9 +32,6 @@ import struct Foundation.Data
/// // 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 {
private let connection: NWConnection
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 @@ -218,6 +219,9 @@ public actor Client {
}
} while true
}

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

/// Disconnect the client and cancel all pending requests
Expand Down Expand Up @@ -450,7 +454,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