$git clone https://github.com/modelcontextprotocol/swift-sdkOfficial Swift SDK for the [Model Context Protocol][mcp] (MCP).
| 1 | # MCP Swift SDK |
| 2 | |
| 3 | Official Swift SDK for the [Model Context Protocol][mcp] (MCP). |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | The Model Context Protocol (MCP) defines a standardized way |
| 8 | for applications to communicate with AI and ML models. |
| 9 | This Swift SDK implements both client and server components |
| 10 | according to the [2025-11-25][mcp-spec-2025-11-25] (latest) version |
| 11 | of the MCP specification. |
| 12 | |
| 13 | ## Table of contents |
| 14 | |
| 15 | - [Requirements](#requirements) |
| 16 | - [Installation](#installation) |
| 17 | - [Client Usage](#client-usage) |
| 18 | - [Basic Client Setup](#basic-client-setup) |
| 19 | - [Transport Options for Clients](#transport-options-for-clients) |
| 20 | - [Tools](#tools) |
| 21 | - [Resources](#resources) |
| 22 | - [Prompts](#prompts) |
| 23 | - [Completions](#completions) |
| 24 | - [Sampling](#sampling) |
| 25 | - [Elicitation](#elicitation) |
| 26 | - [Roots](#roots) |
| 27 | - [Logging](#logging) |
| 28 | - [Error Handling](#error-handling) |
| 29 | - [Cancellation](#cancellation) |
| 30 | - [Progress Tracking](#progress-tracking) |
| 31 | - [Advanced Client Features](#advanced-client-features) |
| 32 | - [Server Usage](#server-usage) |
| 33 | - [Basic Server Setup](#basic-server-setup) |
| 34 | - [Tools](#tools-1) |
| 35 | - [Resources](#resources-1) |
| 36 | - [Prompts](#prompts-1) |
| 37 | - [Completions](#completions-1) |
| 38 | - [Sampling](#sampling-1) |
| 39 | - [Elicitations](#elicitations) |
| 40 | - [Roots](#roots-1) |
| 41 | - [Logging](#logging-1) |
| 42 | - [Progress Tracking](#progress-tracking-1) |
| 43 | - [Initialize Hook](#initialize-hook) |
| 44 | - [HTTP Request Context in Handlers](#http-request-context-in-handlers) |
| 45 | - [Graceful Shutdown](#graceful-shutdown) |
| 46 | - [Transports](#transports) |
| 47 | - [Authentication](#authentication) |
| 48 | - [Client: Client Credentials Flow](#client-client-credentials-flow) |
| 49 | - [Client: Authorization Code Flow](#client-authorization-code-flow) |
| 50 | - [Client: Custom Token Provider](#client-custom-token-provider) |
| 51 | - [Client: Custom Token Storage](#client-custom-token-storage) |
| 52 | - [Client: private\_key\_jwt Authentication](#client-private_key_jwt-authentication) |
| 53 | - [Client: Endpoint Overrides](#client-endpoint-overrides) |
| 54 | - [Server: Serving Protected Resource Metadata](#server-serving-protected-resource-metadata) |
| 55 | - [Server: Validating Bearer Tokens](#server-validating-bearer-tokens) |
| 56 | - [Platform Availability](#platform-availability) |
| 57 | - [Debugging and Logging](#debugging-and-logging) |
| 58 | - [Additional Resources](#additional-resources) |
| 59 | - [Changelog](#changelog) |
| 60 | - [License](#license) |
| 61 | |
| 62 | ## Requirements |
| 63 | |
| 64 | - Swift 6.0+ (Xcode 16+) |
| 65 | |
| 66 | See the [Platform Availability](#platform-availability) section below |
| 67 | for platform-specific requirements. |
| 68 | |
| 69 | ## Installation |
| 70 | |
| 71 | ### Swift Package Manager |
| 72 | |
| 73 | Add the following to your `Package.swift` file: |
| 74 | |
| 75 | ```swift |
| 76 | dependencies: [ |
| 77 | .package(url: "https://github.com/modelcontextprotocol/swift-sdk.git", from: "0.11.0") |
| 78 | ] |
| 79 | ``` |
| 80 | |
| 81 | Then add the dependency to your target: |
| 82 | |
| 83 | ```swift |
| 84 | .target( |
| 85 | name: "YourTarget", |
| 86 | dependencies: [ |
| 87 | .product(name: "MCP", package: "swift-sdk") |
| 88 | ] |
| 89 | ) |
| 90 | ``` |
| 91 | |
| 92 | ## Client Usage |
| 93 | |
| 94 | The client component allows your application to connect to MCP servers. |
| 95 | |
| 96 | ### Basic Client Setup |
| 97 | |
| 98 | ```swift |
| 99 | import MCP |
| 100 | |
| 101 | // Initialize the client |
| 102 | let client = Client(name: "MyApp", version: "1.0.0") |
| 103 | |
| 104 | // Create a transport and connect |
| 105 | let transport = StdioTransport() |
| 106 | let result = try await client.connect(transport: transport) |
| 107 | |
| 108 | // Check server capabilities |
| 109 | if result.capabilities.tools != nil { |
| 110 | // Server supports tools (implicitly including tool calling if the 'tools' capability object is present) |
| 111 | } |
| 112 | ``` |
| 113 | |
| 114 | > [!NOTE] |
| 115 | > The `Client.connect(transport:)` method returns the initialization result. |
| 116 | > This return value is discardable, |
| 117 | > so you can ignore it if you don't need to check server capabilities. |
| 118 | |
| 119 | ### Transport Options for Clients |
| 120 | |
| 121 | #### Stdio Transport |
| 122 | |
| 123 | For local subprocess communication: |
| 124 | |
| 125 | ```swift |
| 126 | // Create a stdio transport (simplest option) |
| 127 | let transport = StdioTransport() |
| 128 | try await client.connect(transport: transport) |
| 129 | ``` |
| 130 | |
| 131 | #### HTTP Transport |
| 132 | |
| 133 | For remote server communication: |
| 134 | |
| 135 | ```swift |
| 136 | // Create a streaming HTTP transport |
| 137 | let transport = HTTPClientTransport( |
| 138 | endpoint: URL(string: "http://localho |