Skip to content

Commit cc603d3

Browse files
committed
Initial commit
0 parents  commit cc603d3

File tree

8 files changed

+970
-0
lines changed

8 files changed

+970
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Package.swift

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// swift-tools-version:5.7
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "mcp-swift-sdk",
8+
platforms: [
9+
.macOS(.v12),
10+
.iOS(.v15),
11+
.tvOS(.v15),
12+
.watchOS(.v8)
13+
],
14+
products: [
15+
// Products define the executables and libraries a package produces, making them visible to other packages.
16+
.library(
17+
name: "MCP",
18+
targets: ["MCP"]),
19+
],
20+
dependencies: [],
21+
targets: [
22+
// Targets are the basic building blocks of a package, defining a module or a test suite.
23+
// Targets can depend on other targets in this package and products from dependencies.
24+
.target(
25+
name: "MCP",
26+
dependencies: []),
27+
.testTarget(
28+
name: "MCPTests",
29+
dependencies: ["MCP"]),
30+
]
31+
)

README.md

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)