Close Menu
geekfence.comgeekfence.com
    What's Hot

    ClickFix attackers using new tactic to evade detection, says Microsoft – Computerworld

    March 7, 2026

    M&A Monthly: February/March 2026

    March 7, 2026

    Posit AI Blog: luz 0.4.0

    March 7, 2026
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    Facebook Instagram
    geekfence.comgeekfence.com
    • Home
    • UK Tech News
    • AI
    • Big Data
    • Cyber Security
      • Cloud Computing
      • iOS Development
    • IoT
    • Mobile
    • Software
      • Software Development
      • Software Engineering
    • Technology
      • Green Technology
      • Nanotechnology
    • Telecom
    geekfence.comgeekfence.com
    Home»iOS Development»Introducing SwiftMCP | Cocoanetics
    iOS Development

    Introducing SwiftMCP | Cocoanetics

    AdminBy AdminNovember 13, 2025No Comments5 Mins Read2 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    Introducing SwiftMCP | Cocoanetics
    Share
    Facebook Twitter LinkedIn Pinterest Email


    I’m thrilled to announce SwiftMCP, a Swift macro-based framework that elegantly exposes your Swift functions as powerful Model Context Protocol (MCP) tools for AI assistants. After months of careful refinement, SwiftMCP now delivers the experience I’ve always dreamed of: turning standard Swift documentation directly into AI-integrable tools—effortlessly.

    Behind the Scenes

    For a long time, I watched with envy as developers in languages like Python effortlessly created tools for AI agents by merely adding decorators to their functions, along with documentation enclosed in triple quotes. Something similar felt painfully out of reach for Swift developers—until I realized the incredible potential of Swift Macros.

    Macros in Swift have full access to the syntax tree of your source code, including every documentation comment, parameter type, and more. This opens up astonishing possibilities:

    • Extracting detailed metadata directly from your existing Swift documentation comments.
    • Automatically generating additional source code that captures and stores this metadata.
    • Dynamically creating function wrappers that simplify invocation with flexible arguments.

    To illustrate, consider this simple Swift function:

    /// Adds two integers and returns their sum
    /// - Parameter a: First number to add
    /// - Parameter b: Second number to add
    /// - Returns: The sum of a and b
    @MCPTool
    func add(a: Int, b: Int) -> Int {
        return a + b
    }

    Using SwiftMCP’s @MCPTool macro, the above function automatically generates metadata like this:

    /// autogenerated
    let __mcpMetadata_add = MCPToolMetadata(
    	name: "add",
    	description: "Adds two integers and returns their sum",
    	parameters: [MCPToolParameterInfo(name: "a", label: "a", type: "Int", description: "First number to add", defaultValue: nil), MCPToolParameterInfo(name: "b", label: "b", type: "Int", description: "Second number to add", defaultValue: nil)],
    	returnType: "Int",
    	returnTypeDescription: "The sum of a and b",
    	isAsync: false,
    	isThrowing: false
    )
    

    Additionally, SwiftMCP generates a flexible invocation wrapper, much like Objective-C’s NSInvocation, enabling your functions to accept parameters as dictionaries:

    /// Autogenerated wrapper for add that takes a dictionary of parameters
    func __mcpCall_add(_ params: [String: Sendable]) async throws -> (Codable & Sendable) {
        let a = try params.extractInt(named: "a")
        let b = try params.extractInt(named: "b")
        return add(a: a, b: b)
    }
    

    This wrapper intelligently validates and parses incoming parameters, automatically handling conversions and providing informative error messages if anything goes wrong.

    The final magic happens at the server level with @MCPServer, which includes a universal tool-invocation method:

    public func callTool(_ name: String, arguments: [String: Sendable]) async throws -> (Codable & Sendable) {
       guard let tool = mcpTools.first(where: { $0.name == name }) else {
          throw MCPToolError.unknownTool(name: name)
       }
    
       let enrichedArguments = try tool.enrichArguments(arguments, forObject: self)
    
       switch name {
          case "add":
             return try await __mcpCall_add(enrichedArguments)
          default:
             throw MCPToolError.unknownTool(name: name)
       }
    }

    With this plumbing in place, SwiftMCP effortlessly supports functions that are async, throwing, returning void, or returning any Codable type. You can serve your MCP tools either via standard IO or as an HTTP+SSE server, allowing seamless integration into various workflows.

    The simpler method of serving – via standard IO – is involves the client actually launching your app and then communicating it be sending single-line JSONRPC requests to stdin and receiving JSONRPC responses via stdout. If you want to send an error, then you should send that to stderr.

    let transport = StdioTransport(server: calculator)
    try await transport.run()

    The other – more sophisticated way of serving – is via a HTTP-SSE server. Here the client makes a GET request to /sse and keeps the connection open. It is informed of an endpoint to POST JSONRPC requests to. When a message is sent to the endpoint, the answer for it (with matching id) will be sent via the SSE channel.

    let transport = HTTPSSETransport(server: calculator, port: 8080)
    try await transport.run()

    SwiftMCP supports both methods. On the HTTP+SSE transport it has a few extra bells and whistles, like for example you can limit access to clients sending a specific bearer token.

    If you have your local server running you can expose it via an OpenAPI scheme to custom GPTs, so that even those can interact with your local tools.

    What’s Next?

    My immediate goal is to integrate SwiftMCP with my existing libraries such as SwiftMail, enabling my agents to interact with email via IMAP and SMTP, handle notifications, and manipulate calendar events via EventKit. There are many additional functions that you can thus make available to agentic coders like Cursor.

    I’ve started a project to do just that. For now it just lets me start an MCP Server and authorize location notifications.

    This exposes the function to send local notifications to my computer:

    /// Sends a notification with the specified title and body
    /// - Parameters:
    ///   - title: The title of the notification
    ///   - body: The body text of the notification
    ///   - subtitle: Optional subtitle for the notification
    @MCPTool
    func sendNotification(title: String,
                          body: String,
                          subtitle: String? = nil
                         ) async throws

    Another idea is to have some basic workflow things that Cursor is missing but Xcode provides, like launching an app in Simulator, or a wrapper for xcode-build and swift for building and testing.

    Conclusion

    Shortly after starting SwiftMCP, I noticed the NSHipster article about iMCP appearing in my feed, highlighting the broader community’s growing interest in MCP. Shortly thereafter, I also found another Swift MCP implementation by Compiler-Inc. It’s exciting to see others exploring similar solutions!

    SwiftMCP is open-source, actively maintained, and eager for your feedback and contributions. I’m happy to hear from you how you plan to use it or to help adding missing features.

    Check it out on GitHub: SwiftMCP, Swift Package Index is hosting the documentation.

    Like this:

    Like Loading…

    Related


    Categories: Administrative



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    I wish I could be an OpenClaw Maintainer

    March 7, 2026

    Swift factory method design pattern

    March 6, 2026

    react native – How to solve “”xcodebuild” exited with error code 65.“ – error from npx expo run:ios?

    March 5, 2026

    Post | Cocoanetics

    March 1, 2026

    Swift abstract factory design pattern

    February 28, 2026

    Flutter: Force portrait UI on tablets (iOS + Android) even when device is landscape, but avoid letterboxing / black bars

    February 27, 2026
    Top Posts

    Hard-braking events as indicators of road segment crash risk

    January 14, 202619 Views

    Understanding U-Net Architecture in Deep Learning

    November 25, 202518 Views

    How to integrate a graph database into your RAG pipeline

    February 8, 202610 Views
    Don't Miss

    ClickFix attackers using new tactic to evade detection, says Microsoft – Computerworld

    March 7, 2026

    “And all Windows computers should already be restricted so that random, unsigned (not signed by…

    M&A Monthly: February/March 2026

    March 7, 2026

    Posit AI Blog: luz 0.4.0

    March 7, 2026

    Top Reasons to Choose Precisely for SAP and Salesforce Process Automation

    March 7, 2026
    Stay In Touch
    • Facebook
    • Instagram
    About Us

    At GeekFence, we are a team of tech-enthusiasts, industry watchers and content creators who believe that technology isn’t just about gadgets—it’s about how innovation transforms our lives, work and society. We’ve come together to build a place where readers, thinkers and industry insiders can converge to explore what’s next in tech.

    Our Picks

    ClickFix attackers using new tactic to evade detection, says Microsoft – Computerworld

    March 7, 2026

    M&A Monthly: February/March 2026

    March 7, 2026

    Subscribe to Updates

    Please enable JavaScript in your browser to complete this form.
    Loading
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    © 2026 Geekfence.All Rigt Reserved.

    Type above and press Enter to search. Press Esc to cancel.