|
| 1 | +# MCP Swift SDK |
| 2 | + |
| 3 | +A Swift implementation of the Model Context Protocol (MCP) client. This SDK provides a modern, Swift-native way to interact with MCP servers. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Full async/await support |
| 8 | +- Type-safe API |
| 9 | +- Resource management |
| 10 | +- Tool integration |
| 11 | +- Configurable transport layer |
| 12 | +- Swift concurrency with actor-based design |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +- Swift 5.7+ |
| 17 | +- macOS 12.0+ |
| 18 | +- iOS 15.0+ |
| 19 | +- tvOS 15.0+ |
| 20 | +- watchOS 8.0+ |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +### Swift Package Manager |
| 25 | + |
| 26 | +Add the following to your `Package.swift` file: |
| 27 | + |
| 28 | +```swift |
| 29 | +dependencies: [ |
| 30 | + .package(url: "https://github.com/yourusername/mcp-swift-sdk.git", from: "1.0.0") |
| 31 | +] |
| 32 | +``` |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +### Basic Setup |
| 37 | + |
| 38 | +```swift |
| 39 | +import MCP |
| 40 | + |
| 41 | +// Create a transport (e.g., StdioTransport) |
| 42 | +let transport = StdioTransport(command: "/path/to/server") |
| 43 | + |
| 44 | +// Create client info |
| 45 | +let clientInfo = Implementation(name: "MyApp", version: "1.0.0") |
| 46 | + |
| 47 | +// Initialize the client |
| 48 | +let client = MCPClient(transport: transport, clientInfo: clientInfo) |
| 49 | + |
| 50 | +// Connect and initialize |
| 51 | +try await client.connect() |
| 52 | +let result = try await client.initialize() |
| 53 | +``` |
| 54 | + |
| 55 | +### Working with Resources |
| 56 | + |
| 57 | +```swift |
| 58 | +// List available resources |
| 59 | +let resources = try await client.listResources() |
| 60 | + |
| 61 | +// Read a resource |
| 62 | +let resource = try await client.readResource("resource://example") |
| 63 | + |
| 64 | +// Subscribe to resource updates |
| 65 | +try await client.subscribeResource("resource://example") |
| 66 | + |
| 67 | +// Unsubscribe from resource updates |
| 68 | +try await client.unsubscribeResource("resource://example") |
| 69 | +``` |
| 70 | + |
| 71 | +### Working with Tools |
| 72 | + |
| 73 | +```swift |
| 74 | +// List available tools |
| 75 | +let tools = try await client.listTools() |
| 76 | + |
| 77 | +// Call a tool |
| 78 | +let result = try await client.callTool(name: "example-tool", arguments: ["key": "value"]) |
| 79 | +``` |
| 80 | + |
| 81 | +### Custom Transport |
| 82 | + |
| 83 | +You can create your own transport by implementing the `MCPTransport` protocol: |
| 84 | + |
| 85 | +```swift |
| 86 | +public protocol MCPTransport { |
| 87 | + func connect() async throws |
| 88 | + func disconnect() async |
| 89 | + func sendRequest(_ data: Data) async throws -> Data |
| 90 | + func sendNotification(_ data: Data) async throws |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Error Handling |
| 95 | + |
| 96 | +The SDK uses the `MCPError` type for error handling: |
| 97 | + |
| 98 | +```swift |
| 99 | +public enum MCPError: LocalizedError { |
| 100 | + case connectionClosed |
| 101 | + case invalidResponse |
| 102 | + case invalidParams(String) |
| 103 | + case serverError(String) |
| 104 | + case protocolError(String) |
| 105 | + case transportError(Error) |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Contributing |
| 110 | + |
| 111 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 112 | + |
| 113 | +## License |
| 114 | + |
| 115 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments