
gavinaboulhosn/swiftmcp
Developer toolsこのページは AI により自動翻訳されています。技術的な詳細は原文をご確認ください。 原文を見る
Swift Implementation of the Model Context Protocol (MCP) Spec
概要
SwiftMCP is a work-in-progress Swift implementation of the Model Context Protocol (MCP), aiming to provide a robust and type-safe way to integrate Language Model capabilities into Swift applications. ⚠️ Note: This SDK is under active development. APIs may change as we work towards feature parity with the TypeScript implementation. - 🏃 - Built with Swift's actor model and async/await - 🔒 - Full type safety for all MCP messages and operations - 🔌 - Support for stdio and Server-Sent Events (SSE) - ⚡️ - Efficient message handling with timeout and retry support - 🛠 - Support for resources, prompts, tools, and more - 📦 - Easy integration through Swift Package Manager Or add it to your Xcode project: 1. File > Add Package Dependencies 2. Enter: https://github.com/gavinaboulhosn/SwiftMCP.
README
以下は原文 README の抜粋です。原文を見る。
SwiftMCP
SwiftMCP is a work-in-progress Swift implementation of the Model Context Protocol (MCP), aiming to provide a robust and type-safe way to integrate Language Model capabilities into Swift applications.
⚠️ Note: This SDK is under active development. APIs may change as we work towards feature parity with the TypeScript implementation.
Features
- 🏃 Modern Swift Concurrency - Built with Swift’s actor model and async/await
- 🔒 Type-Safe - Full type safety for all MCP messages and operations
- 🔌 Multiple Transports - Support for stdio and Server-Sent Events (SSE)
- ⚡️ Performance - Efficient message handling with timeout and retry support
- 🛠 Rich Capabilities - Support for resources, prompts, tools, and more
- 📦 SwiftPM Ready - Easy integration through Swift Package Manager
Installation
Add SwiftMCP to your Swift package:
dependencies: [
.package(url: "https://github.com/gavinaboulhosn/SwiftMCP.git", branch: "main")
]
Or add it to your Xcode project:
- File > Add Package Dependencies
- Enter:
https://github.com/gavinaboulhosn/SwiftMCP.git
Basic Usage
Creating a Client
import SwiftMCP
// Initialize client
let client = MCPClient(
clientInfo: .init(name: "MyApp", version: "1.0.0"),
capabilities: .init(
roots: .init(listChanged: true) // Enable roots with change notifications
)
)
// Connect using stdio transport
let transport = StdioTransport(
options: .init(
command: "npx",
arguments: ["-y", "@modelcontextprotocol/server-memory"]
)
)
// Start the client
try await client.start(transport)
// Make requests
let resources = try await client.listResources()
print("Found \(resources.resources.count) resources")
// Listen for notifications
for await notification in await client.notifications {
switch notification {
case let resourceUpdate as ResourceUpdatedNotification:
print("Resource updated: \(resourceUpdate.params?.uri ?? "")")
default:
break
}
}
Hosts
Hosts are used to manage client connections and provide a higher-level API for handling requests and notifications:
Basic Setup
To get started, you need to create an MCPHost instance and connect to a server using a transport layer:
import SwiftMCP
// Basic host - assumes app name and version via Bundle properties
let host = MCPHost()
// or more advanced configuration
let host = MCPHost(config:
.init(
roots: .list([Root(uri: "file:///some/root")]),
sampling: .init(
handler: { request in
/** Use your llm to fulfill the server's sampling request */
インストール
This server does not publish a one-line install command.
Open the repository installation guide MCP 初心者?mcpskill.org のガイドから始めましょう →