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-04-04 12:11:49 -07:00
.github/workflows Set permissions for CI workflow (#29) 2025-03-27 07:22:13 -07:00
.vscode Initial implementation 2025-02-11 15:22:35 -08:00
Sources/MCP Remove top-level MCP enumeration (#53) 2025-04-04 12:11:27 -07:00
Tests/MCPTests Remove top-level MCP enumeration (#53) 2025-04-04 12:11:27 -07:00
.gitignore Initial commit 2025-02-05 11:13:57 -08:00
LICENSE Transfer project to @modelcontextprotocol (#28) 2025-03-27 07:51:20 -07:00
Package.resolved Initial implementation 2025-02-11 15:22:35 -08:00
Package.swift Update platform requirements (#30) 2025-03-31 10:22:39 -07:00
README.md Bump version to 0.7.1 2025-04-04 12:11:49 -07:00

MCP Swift SDK

Swift implementation of the Model Context Protocol (MCP).

Requirements

  • Swift 6.0+ / Xcode 16+
  • macOS 13.0+
  • iOS / Mac Catalyst 16.0+
  • watchOS 9.0+
  • tvOS 16.0+
  • visionOS 1.0+

Installation

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/modelcontextprotocol/swift-sdk.git", from: "0.7.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
    // 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

// 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

// 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

// 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"]
)

Changelog

This project follows Semantic Versioning. For pre-1.0 releases, minor version increments (0.X.0) may contain breaking changes.

For details about changes in each release, see the GitHub Releases page.

License

This project is licensed under the MIT License.