mcp-swift-sdk
by: loopwork-ai
Swift SDK for Model Context Protocol servers and clients
📌Overview
Purpose: The MCP Swift SDK aims to provide a robust Swift implementation of the Model Context Protocol (MCP) for building client-server applications.
Overview: This SDK facilitates seamless communication between clients and servers using the MCP, streamlining the development of interactive applications on macOS and iOS platforms. It leverages modern Swift features to create a user-friendly interface for managing resources, prompts, and tools efficiently.
Key Features:
-
Client and Server Initialization: Easy setup for both client and server with necessary configurations, enabling rapid development and testing of applications.
-
Tool Management: Allows listing, calling, and handling various tools, providing dynamic functionality within applications.
-
Resource Handling: Supports subscription to resource updates, reading, and managing resources effectively to keep application data synchronized.
-
Prompt Management: Facilitates fetching and utilizing predefined prompts, enhancing user interaction and functionality with minimal effort.
MCP Swift SDK
Swift implementation of the Model Context Protocol (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:
dependencies: [
.package(url: "https://github.com/loopwork-ai/mcp-swift-sdk.git", from: "0.5.1")
]
Usage
Basic Client Setup
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
import MCP
// Initialize the server with capabilities
let server = Server(
name: "MyServer",
version: "1.0.0",
capabilities: .init(
prompts: .init(),
resources: .init(subscribe: true),
tools: .init()
)
)
// Create transport and start server
let transport = StdioTransport()
try await server.start(transport: transport)
// Register method handlers
server.withMethodHandler(ReadResource.self) { params in
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
let tools = try await client.listTools()
let (content, isError) = try await client.callTool(
name: "example-tool",
arguments: ["key": "value"]
)
for item in content {
switch item {
case .text(let text):
print(text)
}
}
Working with Resources
let (resources, nextCursor) = try await client.listResources()
let contents = try await client.readResource(uri: "resource://example")
try await client.subscribeToResource(uri: "resource://example")
await client.onNotification(ResourceUpdatedNotification.self) { message in
let uri = message.params.uri
let content = message.params.content
}
Working with Prompts
let (prompts, nextCursor) = try await client.listPrompts()
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.