How jamesrochabrun/mcpswiftwrapper works
Project type and purpose
• This is a full-stack (library + example app) Swift project.
• The core is a Swift Package (“MCPSwiftWrapper”) that wraps the mcp-swift-sdk to provide Model Context Protocol (MCP) clients to macOS applications.
• There’s also an example macOS SwiftUI chat app (“MCPClientChat”) demonstrating how to call MCP tools for Anthropic and OpenAI models.
File structure
• Top-level:
• Package.swift, Sources/MCPSwiftWrapper (wrapper library)
• Example/MCPClientChat (SwiftUI macOS App)
• Tests/MCPSwiftWrapperTests
• In the wrapper:
• MCPTool+AnthropicTool.swift, MCPTool+OpenAITool.swift – adapters that convert MCP tools into Anthropic/OpenAI formats
• In the example app:
• Chat/Models – ChatManager, ChatMessage, and non-stream MCP client managers
• Chat/UI – SwiftUI views (ChatView, ChatMessageView, ChatInputView)
• MCP/Clients – concrete MCP clients (GithubMCPClient, ClaudeCodeMCPClient) that launch external server processes via stdio.
README for insights
• The wrapper initializes MCPClient with info, transport (stdioProcess), and capabilities.
• The example shows two usage flows:
a. Fetching tools (anthropicTools/openAITools)
b. Handling tool calls by forwarding them to MCPClient (anthropicCallTool/openAICallTool)
• Important: external tool providers run as separate processes (via npx @modelcontextprotocol/server-…), communicating over standard I/O.
Main components & how they interact
a. MCPSwiftWrapper Library
• Exposes MCPClient, adapters for Anthropic/OpenAI parameters, and high-level call methods.
• Internally uses mcp-swift-sdk protocols and MCPTool extensions.
b. External MCP Server Processes
• Launched via stdio transport (e.g. npx @modelcontextprotocol/server-github, server-anthropic).
• Exchange JSON messages with MCPClient.
c. Chat Application (Example/MCPClientChat)
• SwiftUI Frontend: ChatInputView → ChatManager → MCPClient
• ChatManager: a façade that sends user messages to an LLM (Anthropic/OpenAI), processes responses, detects tool uses, and calls MCPSwiftWrapper to invoke tools.
• Models: ChatMessage, ChatManager, tool-response handling streams.
d. External LLM Services (Anthropic, OpenAI)
• Integrated through SwiftAnthropic and SwiftOpenAI libraries.
• After obtaining tool definitions via MCP, passed into LLM parameters so the model can choose tool calls.
Relationships & data flow
1. User types a message in ChatInputView → ChatManager receives it
2. ChatManager calls LLM (Anthropic/OpenAI) via respective Swift SDK
3. LLM may respond with a “toolUse” or function call
4. ChatManager detects tool calls → invokes MCPSwiftWrapper methods (anthropicCallTool/openAICallTool)
5. MCPSwiftWrapper’s MCPClient sends JSON request via stdioProcess to external MCP Server
6. MCP Server executes the tool (e.g. GitHub API, file system) and returns JSON result
7. MCPClient unwraps response → ChatManager updates the conversation with the tool result
8. ChatManager continues dialogue with LLM, including the tool result in the next prompt
Architectural patterns & design principles
• Separation of Concerns:
• UI layer (SwiftUI views)
• Domain layer (ChatManager, models)
• Integration layer (MCPSwiftWrapper + external MCP processes)
• Adapter Pattern: MCPTool+AnthropicTool/OpenAITool adapt MCP tool definitions to LLM SDKs
• Asynchronous Streams: AsyncStream for client initialization and message flows
• Process-per-tool-provider: each MCP server runs as a separate child process over stdio
Final checklist
• Each component labeled clearly (e.g., “ChatInputView”, “ChatManager”, “MCPClient”)