Mirror of https://github.com/modelcontextprotocol/swift-sdk — The official Swift SDK for Model Context Protocol servers and clients.
  • Swift 99.8%
  • Shell 0.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-02-05 11:13:57 -08:00
Sources/MCP Initial commit 2025-02-05 11:13:57 -08:00
Tests/MCPTests Initial commit 2025-02-05 11:13:57 -08:00
.gitignore Initial commit 2025-02-05 11:13:57 -08:00
Package.swift Initial commit 2025-02-05 11:13:57 -08:00
README.md Initial commit 2025-02-05 11:13:57 -08:00

MCP Swift SDK

A Swift implementation of the Model Context Protocol (MCP) client. This SDK provides a modern, Swift-native way to interact with MCP servers.

Features

  • Full async/await support
  • Type-safe API
  • Resource management
  • Tool integration
  • Configurable transport layer
  • Swift concurrency with actor-based design

Requirements

  • Swift 5.7+
  • macOS 12.0+
  • iOS 15.0+
  • tvOS 15.0+
  • watchOS 8.0+

Installation

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/yourusername/mcp-swift-sdk.git", from: "1.0.0")
]

Usage

Basic Setup

import MCP

// Create a transport (e.g., StdioTransport)
let transport = StdioTransport(command: "/path/to/server")

// Create client info
let clientInfo = Implementation(name: "MyApp", version: "1.0.0")

// Initialize the client
let client = MCPClient(transport: transport, clientInfo: clientInfo)

// Connect and initialize
try await client.connect()
let result = try await client.initialize()

Working with Resources

// List available resources
let resources = try await client.listResources()

// Read a resource
let resource = try await client.readResource("resource://example")

// Subscribe to resource updates
try await client.subscribeResource("resource://example")

// Unsubscribe from resource updates
try await client.unsubscribeResource("resource://example")

Working with Tools

// List available tools
let tools = try await client.listTools()

// Call a tool
let result = try await client.callTool(name: "example-tool", arguments: ["key": "value"])

Custom Transport

You can create your own transport by implementing the MCPTransport protocol:

public protocol MCPTransport {
    func connect() async throws
    func disconnect() async
    func sendRequest(_ data: Data) async throws -> Data
    func sendNotification(_ data: Data) async throws
}

Error Handling

The SDK uses the MCPError type for error handling:

public enum MCPError: LocalizedError {
    case connectionClosed
    case invalidResponse
    case invalidParams(String)
    case serverError(String)
    case protocolError(String)
    case transportError(Error)
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.