The official Swift SDK for Model Context Protocol servers and clients. Maintained in collaboration with Apple
# Add to your Claude Code skills
git clone https://github.com/modelcontextprotocol/swift-sdkGuides for using mcp servers skills like swift-sdk.
swift-sdk is an open-source mcp servers skill for AI coding assistants such as Claude Code, Codex CLI, and ChatGPT, built by modelcontextprotocol. The official Swift SDK for Model Context Protocol servers and clients. Maintained in collaboration with Apple. It has 1,591 GitHub stars.
swift-sdk's catalog security scan is still queued. You can run an instant dependency and prompt-injection check now with the "Scan for vulnerabilities" button above.
Clone the repository with "git clone https://github.com/modelcontextprotocol/swift-sdk" and add it to your Claude Code skills directory (see the Installation section above).
swift-sdk is primarily written in Swift. It is open-source under modelcontextprotocol on GitHub, so you can review or fork the full source.
Yes. AgentsLLM lists many other MCP Servers skills you can browse and compare side by side. Open the MCP Servers category from the badge at the top of this page, or use the Related Skills and comparison links further down to weigh swift-sdk against similar tools.
No comments yet. Be the first to share your thoughts!
Top skills in this category by stars
Unlocks once the catalog security scan passes (runs nightly).
The deep catalog scan for this skill is still queued. Run an instant dependency check now instead.
Official Swift SDK for the [Model Context Protocol][mcp] (MCP).
The Model Context Protocol (MCP) defines a standardized way for applications to communicate with AI and ML models. This Swift SDK implements both client and server components according to the [2025-11-25][mcp-spec-2025-11-25] (latest) version of the MCP specification.
A Swift 6.0 package manifest ([email protected]) is also provided for the MCP library,
but CI only tests Swift 6.1 and later.
See the Platform Availability section below for platform-specific requirements.
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/modelcontextprotocol/swift-sdk.git", from: "0.12.0")
]
Then add the dependency to your target:
.target(
name: "YourTarget",
dependencies: [
.product(name: "MCP", package: "swift-sdk")
]
)
The client component allows your application to connect to MCP servers.
import MCP
// Initialize the client
let client = Client(name: "MyApp", version: "1.0.0")
// Create a transport and connect
let transport = StdioTransport()
let result = try await client.connect(transport: transport)
// Check server capabilities
if result.capabilities.tools != nil {
// Server supports tools (implicitly including tool calling if the 'tools' capability object is present)
}
[!NOTE] The
Client.connect(transport:)method returns the initialization result. This return value is discardable, so you can ignore it if you don't need to check server capabilities.
For communicating with a server running as a local subprocess.
StdioTransport() with no arguments uses the current process's own stdin and stdout,
which is what a server launched by a host application needs.
To talk to a server subprocess from a client, launch it and pass its pipes to the transport:
import Foundation
import MCP
#if canImport(System)
import System
#else
import SystemPackage
#endif
// Launch the server process
let serverInput = Pipe() // the client writes, the server reads
let serverOutput = Pipe() // the server writes, the client reads
let process = Process()
process.executableURL = URL(fileURLWithPath: "/path/to/mcp-server")
process.standardInput = serverInput
process.standardOutput = serverOutput
try process.run()
// Connect over the server's stdin and stdout
let transport = StdioTransport(
input: FileDescriptor(rawValue: serverOutput.fileHandleForReading.fileDescriptor),
output: FileDescriptor(rawValue: serverInput.fileHandleForWriting.fileDescriptor)
)
try await client.connect(transport: transport)
For remote server communication:
// Create a streaming HTTP transport
let transport = HTTPClientTransport(
endpoint: URL(string: "http://localhost:8080")!,
streaming: true // Enable Server-Sent Events for real-time updates
)
try await client.connect(transport: transport)
Tools represent functions that can be called by the client:
// List available tools
let (tools, cursor) = try await client.listTools()
print("Available tools: \(tools.map { $0.name }.joined(separator: ", "))")
// Call a tool with arguments and get the result
let (content, isError) = try await client.callTool(
name: "image-generator",
arguments: [
"prompt": "A serene mountain landscape at sunset",
"style": "photorealistic",
"width": 1024,
"height": 768
]
)
// Handle tool content
for item in content {
switch item {
case .text(let text, _, _):
print("Generated text: \(text)")
case .image(let data, let mimeType, _, _):
print("Received \(data.count) bytes of base64 image data of type \(mimeType)")
// Decode with Data(base64Encoded: data) to save or display the image
case .audio(_, let mimeType, _, _):
print("Received audio data of type \(mimeType)")
case .resource(let resource, _, _):
print("Received embedded resource: \(resource)")
case .resourceLink(let uri, let name, _, _, let mimeType, _):
print("Resource link: \(name) at \(uri), type: \(mimeType ?? "unknown")")
}
}
Resources represent data that can be accessed and potentially subscribed to:
// List available resources
let (resources, nextCursor) = try await client.listResources()
print("Available resources: \(resources.map { $0.uri }.joined(separator: ", "))")
// Read a resource
let contents = try await client.readResource(uri: "resource://example")
print("Resource content: \(contents)")
// Subscribe to resource updates if supported
// (`result` is the initialization result returned by `client.connect(transport:)`)
if result.capabilities.resources?.subscribe == true {
try await client.subscribeToResource(uri: "resource://example")
// Register notification handler
await client.onNotification(ResourceUpdatedNotification.self) { message in
let uri = message.params.uri
print("Resource \(uri) updated with new content")
// Fetch the updated resource content
let updatedContents = try await client.readResource(uri: uri)
print("Updated resource content received")
}
}
Prompts represent templated conversation starters:
// List available prompts
let (prompts, nextCursor) = try await client.listPrompts()
print("Available prompts: \(prompts.map { $0.name }.joined(separator: ", "))")
// Get a prompt with arguments
let (description, messages) = try await client.getPrompt(
name: "customer-service",
arguments: [
"customerName": "Alice",
"orderNumber": "ORD-12345",
"issue": "delivery delay"
]
)
// Use the prompt messages in your application
print("Prompt description: \(description ?? "none")")
for message in messages {
if case .text(text: let text) = message.content {
print("\(message.role): \(text)")
}
}
Completions allow servers to provide autocompletion suggestions for prompt and resource template arguments as users type:
// Request completions for a prompt argument
let completion = try await client.complete(
promptName: "code_review",
argumentName: "language",
argumentValue: "py"
)
// Display suggestions to the user
for value in completion.values {
print("Suggestion: \(value)")
}
if completion.hasMore == true {
print("More suggestions available (total: \(completion.total ?? 0))")
}
You can also provide context with already-resolved arguments:
// First, user selects a language
let languageCompletion = try await client.complete(
promptName: "code_review",
argumentName: "language",
argumentValue: "py"
)
// User selects "python"
// Then get framework suggestions based on the selected language
let frameworkCompletion = try await client.complete(
promptName: "code_review",
argumentName: "framework",
argumentValue: "fla",
context: ["language": "python"]
)
// Returns: ["flask"]
Completions work for resource templates as well:
// Get path completions for a resource URI template
let pathCompletion = try await client.complete(
resourceURI: "file:///{path}",
argumentName: "path",
argumentValue: "/usr/"
)
// Returns: ["/usr/bin", "/usr/lib", "/usr/local"]
Sampling allows servers to request LLM completions through the client, enabling agentic behaviors while maintaining human-in-the-loop control. Clients register a handler to process incoming sampling requests from servers.
[!TIP] Sampling requests flow from server to client, not client to server. This enables servers to reques