SwiftMCP

by Cocoanetics

159 stars
316 downloads
Not rated
GitHub

About

SwiftMCP is a Swift implementation of the Model Context Protocol (MCP) for JSON-RPC communication over multiple transports. It is intended for Swift developers who want to build MCP servers or clients that expose tools, resources, and prompts to AI models and other MCP-aware…

Details

Author
Cocoanetics
GitHub stars
159
Downloads
316
Categories
Other

- Multiple transport options: stdio, HTTP+SSE, TCP+Bonjour
- JSON-RPC 2.0 compliance with OpenAPI generation
- Built-in authorization and optional OAuth validation
- Macros for tools (@MCPTool), resources (@MCPResource), and prompts (@MCPPrompt)
- Typed client proxy generation for any MCP server
- Cross-platform support (macOS, Linux, Windows via trait selection)

Add SwiftMCP as a dependency in your Package.swift, then declare a server with the @MCPServer macro, define tools with @MCPTool, and configure a transport such as stdio, HTTP+SSE, or TCP+Bonjour. The included demo provides commands like SwiftMCPDemo stdio and SwiftMCPDemo httpsse --port 8080.

SwiftMCP

A Swift implementation of the MCP (Model Context Protocol) for JSON-RPC over various transports.

Features

- Multiple transport options
- Standard I/O (stdio) for command-line usage
- HTTP+SSE (Server-Sent Events) for web applications
- TCP+Bonjour for local discovery over raw TCP
- JSON-RPC 2.0 compliant with OpenAPI generation
- Built-in authorization and optional OAuth validation
- Transparent OAuth proxy mode for MCP and OpenAPI
- Cross-platform compatibility

Server Features

- Tools – expose functions with @MCPTool
- Resources – publish data with @MCPResource
- Prompts – build prompts using @MCPPrompt
- Utilities
- Progress notifications via RequestContext.current
- Structured logging streamed through Session.current
- Completion suggestions for parameters (defaults for Bool and CaseIterable)

Client Features

- Roots – dynamic filesystem roots announced by the client
- Sampling – request small previews of client files

Installation

Add SwiftMCP as a dependency in your Package.swift:

dependencies: [
    .package(url: "https://github.com/Cocoanetics/SwiftMCP.git", branch: "main")
]

Package traits

SwiftMCP exposes its optional feature areas as SwiftPM package traits, all enabled by default — existing consumers and a plain swift build are unaffected. The single import SwiftMCP module is unchanged.

| Trait | Enables | Extra dependencies |
| --- | --- | --- |
| Server | HTTP/SSE + TCP server transports | swift-nio, swift-crypto, swift-certificates |
| Client | The MCP client (MCPServerProxy) | — |
| OpenAPI | OpenAPI / AI-plugin manifest models and routes | — |

The tool/schema foundation — JSONValue, JSONSchema, MCPToolProviding, the @MCPTool / @MCPServer macros, errors — is always-on core and needs no trait.

Disabling Server drops swift-nio (and swift-crypto/swift-certificates) entirely, which makes the package build on Windows and gives client/tools-only consumers a lighter graph. A consumer that only talks to remote MCP servers selects just what it needs (your manifest must be swift-tools-version: 6.1 or newer to choose traits):

.package(
    url: "https://github.com/Cocoanetics/SwiftMCP.git",
    branch: "main",
    traits: ["Client"]   // tool/schema core + client, no swift-nio
)

> AppIntents bridging (@MCPAppIntentTool, AppShortcuts discovery) is not a trait: it is already gated by #if canImport(AppIntents), so it is automatically excluded on platforms without AppIntents (Linux, Windows, Android).

Usage

Command Line Demo

The included demo application shows how to use SwiftMCP with different transport options:

```bash

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.