Fire in da houseTop Tip:Most people pay up to $340 per month for Perplexity, MidJourney, Runway, ChatGPT, and more - but you can get them all your AI tools for $15 with Galaxy. It's free to test!Fire in da houseCheck it out

SwiftMCP

MCP.Pizza Chef: Cocoanetics

SwiftMCP is a cross-platform Model Context Protocol server implemented in Swift, supporting JSON-RPC 2.0 over multiple transports including standard I/O and HTTP+SSE. It features asynchronous response handling, built-in authorization, and is designed for easy integration in command-line and web applications.

Use This MCP server To

Host MCP server for command-line LLM integrations Serve MCP context over HTTP with Server-Sent Events Enable secure MCP communication with token-based authorization Integrate MCP server in Swift-based cross-platform apps Support asynchronous LLM responses via SSE transport Provide JSON-RPC 2.0 compliant MCP interface for clients

README

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
  • JSON-RPC 2.0 compliant
  • Asynchronous response handling via SSE
  • Built-in authorization support
  • Cross-platform compatibility

Installation

Add SwiftMCP as a dependency in your Package.swift:

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

Usage

Command Line Demo

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

# Using stdio transport
SwiftMCPDemo stdio

# Using HTTP+SSE transport
SwiftMCPDemo httpsse --port 8080

# Using HTTP+SSE with authorization
SwiftMCPDemo httpsse --port 8080 --token your-secret-token

# Using HTTP+SSE with OpenAPI support
SwiftMCPDemo httpsse --port 8080 --openapi

# Using HTTP+SSE with authorization and OpenAPI support
SwiftMCPDemo httpsse --port 8080 --token your-secret-token --openapi

When using HTTP+SSE transport with the --token option, clients must include an Authorization header with their requests:

Authorization: Bearer your-secret-token

OpenAPI support

The --openapi option enables OpenAPI endpoints for AI plugin integration. When this option is used, the server will provide an OpenAPI specification at /openapi.json and an AI plugin manifest at /.well-known/ai-plugin.json. This allows for easy integration with AI models and other tools that support OpenAPI.

Custom Server Implementation

To implement your own MCP server:

  1. Attach the @MCPServer macro to a reference type like class or actor
  2. Define your tools using @MCPTool attribute
  3. Choose and configure a transport

Example:

@MCPServer
class MyServer {
    @MCPTool
    func add(a: Int, b: Int) -> Int {
        return a + b
    }
}

// Using HTTP+SSE transport with authorization
let server = MyServer()
let transport = HTTPSSETransport(server: server, port: 8080)

// Optional: Add authorization
transport.authorizationHandler = { token in
    guard let token = token, token == "your-secret-token" else {
        return .unauthorized("Invalid token")
    }
    return .authorized
}

try transport.run()

Documentation Extraction

The @MCPServer and @MCPTool macros extract documentation comments to describe class, parameters and return value.

Macros Functionality

The macros in this repository provide functionality for defining and exposing tools and servers in the SwiftMCP framework. Here are the main functionalities of the macros:

  • @MCPServer: This macro is used to define a class or actor as an MCP server. It extracts documentation comments to describe the class, parameters, and return values. An example of its usage can be seen in the Demos/SwiftMCPDemo/Calculator.swift file, where the Calculator actor is annotated with @MCPServer(name: "SwiftMCP Demo").
  • @MCPTool: This macro is used to define functions within an MCP server that can be called as tools. It also extracts documentation comments to describe the function, parameters, and return values. Examples of its usage can be seen in the Demos/SwiftMCPDemo/Calculator.swift file, where various functions such as add, subtract, testArray, multiply, divide, greet, ping, and noop are annotated with @MCPTool.

These macros help in automatically generating the necessary metadata and documentation for the MCP server and its tools, making it easier to expose them for JSON-RPC communication and integration with AI models.

License

This project is licensed under the BSD 2-clause License - see the LICENSE file for details.

SwiftMCP FAQ

How do I add SwiftMCP to my Swift project?
Add SwiftMCP as a dependency in your Package.swift file using the GitHub URL and branch 'main'.
What transport protocols does SwiftMCP support?
SwiftMCP supports standard I/O (stdio) for CLI and HTTP with Server-Sent Events (SSE) for web applications.
Does SwiftMCP support authorization?
Yes, it includes built-in token-based authorization support for secure communication.
Can SwiftMCP handle asynchronous responses?
Yes, it supports asynchronous response handling via Server-Sent Events (SSE).
Is SwiftMCP cross-platform?
Yes, SwiftMCP is designed to be cross-platform and works on any system supporting Swift.
How do I run a demo of SwiftMCP?
Use the included demo application with commands like 'SwiftMCPDemo stdio' or 'SwiftMCPDemo httpsse --port 8080'.
Does SwiftMCP comply with any protocol standards?
Yes, it is fully compliant with JSON-RPC 2.0 specification.
Can SwiftMCP be used in web applications?
Yes, it supports HTTP+SSE transport ideal for web app integrations.