MCP ExplorerExplorer

Mcp Swift Sdk

@gsabranon 20 days ago
181 MIT
FreeCommunity
AI Systems
Swift SDK for Model Context Protocol, enabling LLM integration.

Overview

What is Mcp Swift Sdk

The mcp-swift-sdk is a Swift implementation of the Model Context Protocol (MCP), designed to provide both client and server capabilities for integrating with LLM surfaces.

Use cases

Use cases include developing applications that need to interact with LLMs while managing context efficiently, such as chatbots, virtual assistants, and other AI-driven applications.

How to use

To use mcp-swift-sdk, add it as a dependency in your Xcode project or SPM-based project. For Xcode, go to ‘File → Add Package Dependencies…’ and input the package URL. For SPM, add the package URL in your dependencies and include the necessary products in your targets.

Key features

Key features include the ability to build MCP clients that connect to any MCP server, support for standard transports like stdio and SSE, and handling all MCP protocol messages and lifecycle events.

Where to use

mcp-swift-sdk can be used in applications that require standardized context provision for LLMs, particularly in environments where LLM interactions are necessary.

Content

MCP Swift SDK!

Build & Deploy
Coverage Statusodecov
Buy me a coffee

Swift implementation of the Model Context Protocol (MCP), providing both client and server capabilities for integrating with LLM surfaces.

Archive notice

Given that another package was chosen by Anthropic as their official Swift MCP SDK, this package will not be maintained anymore. Particularly it will not support future changes to the MCP specs.
While I would have liked to keep maintaining it, it doesn’t seem to make sense to dedicate significant time to doing so given the official recommendation.
Thanks to all those who contributed to this project, and provided feedback ❤️.

Overview

The Model Context Protocol allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction. This Swift SDK implements the full MCP specification for the client, making it easy to:

  • Build MCP clients that can connect to any MCP server
  • Use standard transports like stdio and SSE
  • Handle all MCP protocol messages and lifecycle events

Installation

  • Xcode: File → Add Package Dependencies… and input the package URL https://github.com/gsabran/mcp-swift-sdk

  • SPM-based projects:
    Add the dependency to your package:

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

And then add the product that you need to all targets that use the dependency:

.product(name: "MCPServer", package: "mcp-swift-sdk"),
// and/or
.product(name: "MCPClient", package: "mcp-swift-sdk"),

Quick Start

⚠️ When using stdio servers in a MacOS app, you need to disable sandboxing. This is because the app will need to run the processes for each server.

Creating a Server

import MCPServer

let server = try await MCPServer(
  info: Implementation(name: "test-server", version: "1.0.0"),
  capabilities: .init(...),
  transport: .stdio())

// The client's roots, if available.  
let roots = await server.roots.value

// Keep the process running until the client disconnects.
try await server.waitForDisconnection()

Tool calling

The tool input schema can be generated for you (thanks swift-json-schema!)

import JSONSchemaBuilder

@Schemable
struct ToolInput {
  let text: String
}

let capabilities = ServerCapabilityHandlers(tools: [
  Tool(name: "repeat") { (input: ToolInput) in
    [.text(.init(text: input.text))]
  },
])

Creating a Client

import MCPClient

let transport = try Transport.stdioProcess(
  serverInfo.executable,
  args: serverInfo.args,
  env: serverInfo.env)
  
let client = try await MCPClient(
  info: .init(name: "example-client", version: "1.0.0"),
  transport: transport,
  capabilities: .init(
    roots: .init(info: .init(listChanged: true), handler: listRoots)))

// List available resources
let resources = await client.resources.value

// Read a specific resource
let resourceContent = try await client.readResource(uri: "file:///example.txt")

Documentation

License

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

Tools

No tools

Comments