swiftapplications

MCP on macOS

Build an MCP server in Swift with the official Swift SDK

A minimal MCP server in Swift using the official swift-sdk: package setup, one tool, the stdio transport, logging to stderr, and adding it to Claude Code.

Updated 8 October 2026 3 min read

The short answer

Add the official modelcontextprotocol/swift-sdk package, create a Server, register handlers for ListTools and CallTool, and start it on a StdioTransport. Never print to stdout (that’s the protocol channel); log to stderr. Then register the built binary with your MCP client.

Package.swift

Package.swift
// swift-tools-version: 6.0
import PackageDescription

let package = Package(
    name: "uptime-mcp",
    platforms: [.macOS(.v13)],
    dependencies: [
        .package(url: "https://github.com/modelcontextprotocol/swift-sdk.git", from: "0.10.0"),
    ],
    targets: [
        .executableTarget(name: "uptime-mcp", dependencies: [.product(name: "MCP", package: "swift-sdk")]),
    ]
)

The server

Sources/uptime-mcp/main.swift
import Foundation
import MCP

let server = Server(
    name: "uptime-mcp",
    version: "1.0.0",
    capabilities: .init(tools: .init(listChanged: false))
)

await server.withMethodHandler(ListTools.self) { _ in
    .init(tools: [
        Tool(name: "uptime", description: "How long this Mac has been running", inputSchema: .object(["type": "object"]))
    ])
}

await server.withMethodHandler(CallTool.self) { params in
    guard params.name == "uptime" else {
        return .init(content: [.text("Unknown tool: \(params.name)")], isError: true)
    }
    let hours = Int(ProcessInfo.processInfo.systemUptime / 3600)
    return .init(content: [.text("Up for \(hours) hours")], isError: false)
}

// stdout carries the protocol: log to stderr only
FileHandle.standardError.write(Data("uptime-mcp ready\n".utf8))

try await server.start(transport: StdioTransport())
await server.waitUntilCompleted()

Build and register it

bash
swift build -c release
claude mcp add uptime -- "$PWD/.build/release/uptime-mcp"

Ask your agent “how long has this Mac been up?” and it calls the tool. To test without an agent, run it under the MCP Inspector (debugging guide).

Before you give it real power

A tool that only reads is easy. A tool that changes something (a refund, an email, a file) needs a decision about who approves it, and where its credentials live. Keep secrets out of the server’s arguments and environment if you can.

The safe way to do this

MoltenRock Connect

One local MCP server for 30 business platforms

MoltenRock Connect is a Swift MCP server for 30 business platforms that never hands an agent a key.

Get MoltenRock Connect

Questions developers ask

Is there an official Swift SDK for MCP?

Yes: modelcontextprotocol/swift-sdk on GitHub, maintained under the MCP organisation.

Why does my stdio MCP server break when I print?

stdout is the JSON-RPC channel. Any extra output corrupts it; write logs to stderr.

Can a Swift MCP server use macOS frameworks?

Yes. It’s a normal Mac executable, so it can use Foundation, the Keychain, EventKit and more, within the permissions macOS grants it.