https://github.com/loopwork-ai/mcp-swift-sdk
Swift SDK for Model Context Protocol servers and clients
https://github.com/loopwork-ai/mcp-swift-sdk
mcp swift
Last synced: 6 months ago
JSON representation
Swift SDK for Model Context Protocol servers and clients
- Host: GitHub
- URL: https://github.com/loopwork-ai/mcp-swift-sdk
- Owner: loopwork-ai
- License: apache-2.0
- Created: 2025-02-05T20:50:16.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-11T23:27:41.000Z (about 1 year ago)
- Last Synced: 2025-02-12T00:25:33.472Z (about 1 year ago)
- Topics: mcp, swift
- Language: Swift
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MCP Swift SDK
Swift implementation of the [Model Context Protocol][mcp] (MCP).
## Requirements
- Swift 6.0+ / Xcode 16+
- macOS 14.0+ (Sonoma)
- iOS 17.0+
## Installation
### Swift Package Manager
Add the following to your `Package.swift` file:
```swift
dependencies: [
.package(url: "https://github.com/loopwork-ai/mcp-swift-sdk.git", from: "0.1.0")
]
```
## Usage
### Basic Client Setup
```swift
import MCP
// Initialize the client
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()
```
### Basic Server Setup
```swift
import MCP
// Initialize the server with capabilities
let server = Server(
name: "MyServer",
version: "1.0.0",
capabilities: .init(
resources: .init(
list: true,
read: true,
subscribe: true
)
)
)
// Create transport and start server
let transport = StdioTransport()
try await server.start(transport: transport)
// Register method handlers
server.withMethodHandler(ReadResource.self) { params in
// Handle resource read request
let uri = params.uri
let content = [Resource.Content.text("Example content")]
return .init(contents: content)
}
// Register notification handlers
server.onNotification(ResourceUpdatedNotification.self) { message in
// Handle resource update notification
}
// Stop the server when done
await server.stop()
```
### Working with Tools
```swift
// List available tools
let tools = try await client.listTools()
// Call a tool
let (content, isError) = try await client.callTool(
name: "example-tool",
arguments: ["key": "value"]
)
// Handle tool content
for item in content {
switch item {
case .text(let text):
print(text)
case .image(let data, let mimeType, let metadata):
// Handle image data
}
}
```
### Working with Resources
```swift
// List available resources
let (resources, nextCursor) = try await client.listResources()
// Read a resource
let contents = try await client.readResource(uri: "resource://example")
// Subscribe to resource updates
try await client.subscribeToResource(uri: "resource://example")
// Handle resource updates
await client.onNotification(ResourceUpdatedNotification.self) { message in
let uri = message.params.uri
let content = message.params.content
// Handle the update
}
```
### Working with Prompts
```swift
// List available prompts
let (prompts, nextCursor) = try await client.listPrompts()
// Get a prompt with arguments
let (description, messages) = try await client.getPrompt(
name: "example-prompt",
arguments: ["key": "value"]
)
```
## License
This project is licensed under the Apache License, Version 2.0.
[mcp]: https://modelcontextprotocol.io