# loopwork-ai/imcp architecture

> Architecture diagram of the GitHub repository loopwork-ai/imcp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/loopwork-ai/imcp
- Repository: https://github.com/loopwork-ai/imcp

## Overview

##### Project type and purpose
- This is a full-stack macOS application (“iMCP.app”) bundled with a command-line MCP server executable (“imcp-server”).
- Purpose: expose on-device services (Calendar, Contacts, Location, Maps, Messages, Reminders, Weather, etc.) over the Model Context Protocol (MCP) to AI clients such as Claude Desktop.

##### File structure
- Top-level “App/” directory: SwiftUI/macOS UI, Controllers, Views, Models, Services, Integrations.
- “CLI/” directory: single Swift file (main.swift) implementing the MCP server, proxying MCP requests via stdio to the App.
- Xcode project with both targets: the .app and the bundled imcp-server CLI.
- Configuration & discovery via Bonjour (“_mcp._tcp.local”).

##### Key technical components from the README
- App sandbox + Apple frameworks (EventKit, Contacts, MapKit, CoreLocation, etc.) to fetch data.
- imcp-server uses standard input/output (stdio) to communicate with MCP clients.
- App and CLI discover each other and exchange requests/responses over a local TCP connection via Bonjour.
- Data serialization in JSON-LD (using Ontology package) and MCP message handling (via mcp-swift-sdk).
- Special handling for iMessage DB (SQLite + typedstream decoding via Madrid package).

##### Main components
A. MCP Client (e.g., Claude Desktop)
B. imcp-server CLI process
C. iMCP macOS App process
D. Bonjour/mDNS discovery layer
E. stdio transport (between client and CLI)
F. TCP transport (between CLI and App)
G. Service modules inside App (CalendarService, ContactsService, etc.)
H. System frameworks & external resources
- EventKit, Contacts framework, CoreLocation, MapKit, SQLite chat.db, Weather API
I. Swift SDKs
- mcp-swift-sdk (MCP protocol)
- Madrid (iMessage typedstream)
- Ontology (JSON-LD)

##### Relationships & data flow
1. Claude Desktop (MCP Client) launches imcp-server with stdio transport.
→ (1) MCP request via STDIN
2. imcp-server CLI reads requests from STDIN, advertises its presence via Bonjour.
→ (2) forwards request over TCP to iMCP App
3. iMCP App receives request, routes to the appropriate Service module (e.g., Calendar)
→ (3) invokes Apple framework or reads chat.db
4. Service module returns domain model value, serialized to JSON-LD via Ontology.
→ (4) App sends JSON-LD response back over TCP to CLI
5. imcp-server writes response JSON-LD to STDOUT.
→ (5) MCP Client reads and handles the tool result

##### Architectural patterns & design principles
- Separation of concerns: UI (SwiftUI Views/Controllers) vs. service/business logic vs. transport layer.
- Adapter modules for each system framework (Services directory) implementing a common “Tool” interface.
- Proxy pattern: imcp-server acts as a proxy between MCP clients and the App.
- Discovery & decoupling: Bonjour for locating the App at runtime.
- Use of standard transports (stdio, TCP) and standardized serialization (JSON-LD).

## Diagram (Mermaid)

```mermaid
graph TB
  %% Processes
  MCPClient["MCP Client (Claude Desktop)"]:::external

  subgraph "imcp-server CLI" 
    CLIProcess["imcp-server\n(main.swift)"]:::cli
  end

  subgraph "iMCP.app"
    %% UI Layer
    AppMain["App/App.swift\n(Main Entry)"]:::frontend
    ContentView["App/Views/ContentView.swift"]:::frontend
    ServiceToggle["App/Views/ServiceToggleView.swift"]:::frontend
    AboutWindow["App/Views/AboutWindow.swift"]:::frontend

    Controllers["ServerController\n(Bonjour & TCP)"]:::controller

    subgraph "Service Modules"
      Cal["CalendarService"]:::service
      Contacts["ContactsService"]:::service
      Location["LocationService"]:::service
      Maps["MapsService"]:::service
      Messages["MessagesService"]:::service
      Reminders["RemindersService"]:::service
      Weather["WeatherService"]:::service
      Utils["Utilities"]:::service
    end

    subgraph "Models & Tools"
      ToolProto["Tool Protocol"]:::model
      ServiceDef["Service Definition"]:::model
      ValueModel["Value Models"]:::model
    end

    subgraph "Framework Adapters"
      EventKitExt["EventKit+Extensions"]:::framework
      ContactsExt["Contacts+Extensions"]:::framework
      MapKitExt["MapKit+Extensions"]:::framework
      FoundationExt["Foundation+Extensions"]:::framework
      LoggerExt["Logger+Extensions"]:::framework
    end

    Integration["ClaudeDesktop Integration"]:::integration
  end

  %% External frameworks & data
  Frameworks["EventKit, Contacts, CoreLocation, MapKit"]:::framework
  chatDB["chat.db (SQLite)"]:::datastore
  WeatherAPI["Weather API"]:::external

  %% Data Flows
  MCPClient -.->|"1.MCP request via STDIN"| CLIProcess
  CLIProcess -->|"2.TCP request via Bonjour/mDNS"| Controllers
  Controllers -->|"3.Route to Service Modules"| Cal
  Controllers --> Cal
  Controllers --> Contacts
  Controllers --> Location
  Controllers --> Maps
  Controllers --> Messages
  Controllers --> Reminders
  Controllers --> Weather
  Controllers --> Utils
  Cal -->|"calls frameworks & db"| Frameworks & chatDB
  Contacts --> Frameworks & chatDB
  Location --> Frameworks & chatDB
  Maps --> Frameworks & chatDB
  Messages --> Frameworks & chatDB
  Reminders --> Frameworks & chatDB
  Weather --> WeatherAPI
  Utils --> Frameworks & chatDB
  Frameworks & chatDB --> Services
  WeatherAPI --> Weather
  Services -->|"4.Response JSON-LD"| Controllers
  Controllers -->|"4.JSON-LD via TCP"| CLIProcess
  CLIProcess -.->|"5.Response via STDOUT"| MCPClient

  %% Click Events
  click CLIProcess "https://github.com/loopwork-ai/imcp/blob/main/CLI/main.swift"
  click Controllers "https://github.com/loopwork-ai/imcp/blob/main/App/Controllers/ServerController.swift"
  click Cal "https://github.com/loopwork-ai/imcp/blob/main/App/Services/Calendar.swift"
  click Contacts "https://github.com/loopwork-ai/imcp/blob/main/App/Services/Contacts.swift"
  click Location "https://github.com/loopwork-ai/imcp/blob/main/App/Services/Location.swift"
  click Maps "https://github.com/loopwork-ai/imcp/blob/main/App/Services/Maps.swift"
  click Messages "https://github.com/loopwork-ai/imcp/blob/main/App/Services/Messages.swift"
  click Reminders "https://github.com/loopwork-ai/imcp/blob/main/App/Services/Reminders.swift"
  click Weather "https://github.com/loopwork-ai/imcp/blob/main/App/Services/Weather.swift"
  click Utils "https://github.com/loopwork-ai/imcp/blob/main/App/Services/Utilities.swift"
  click ToolProto "https://github.com/loopwork-ai/imcp/blob/main/App/Models/Tool.swift"
  click ServiceDef "https://github.com/loopwork-ai/imcp/blob/main/App/Models/Service.swift"
  click ValueModel "https://github.com/loopwork-ai/imcp/blob/main/App/Models/Value.swift"
  click AppMain "https://github.com/loopwork-ai/imcp/blob/main/App/App.swift"
  click ContentView "https://github.com/loopwork-ai/imcp/blob/main/App/Views/ContentView.swift"
  click ServiceToggle "https://github.com/loopwork-ai/imcp/blob/main/App/Views/ServiceToggleView.swift"
  click AboutWindow "https://github.com/loopwork-ai/imcp/blob/main/App/Views/AboutWindow.swift"
  click EventKitExt "https://github.com/loopwork-ai/imcp/blob/main/App/Extensions/EventKit+Extensions.swift"
  click ContactsExt "https://github.com/loopwork-ai/imcp/blob/main/App/Extensions/Contacts+Extensions.swift"
  click MapKitExt "https://github.com/loopwork-ai/imcp/blob/main/App/Extensions/MapKit+Extensions.swift"
  click FoundationExt "https://github.com/loopwork-ai/imcp/blob/main/App/Extensions/Foundation+Extensions.swift"
  click LoggerExt "https://github.com/loopwork-ai/imcp/blob/main/App/Extensions/Logger+Extensions.swift"
  click Integration "https://github.com/loopwork-ai/imcp/blob/main/App/Integrations/ClaudeDesktop.swift"

  %% Styles
  classDef external fill:#f9f,stroke:#333,stroke-width:1px
  classDef cli fill:#cfc,stroke:#333,stroke-width:1px
  classDef app fill:#ccf,stroke:#333,stroke-width:1px
  classDef frontend fill:#bbf,stroke:#333,stroke-width:1px
  classDef controller fill:#ddf,stroke:#333,stroke-width:1px
  classDef service fill:#aff,stroke:#333,stroke-width:1px
  classDef model fill:#faf,stroke:#333,stroke-width:1px
  classDef framework fill:#ffd,stroke:#333,stroke-width:1px
  classDef integration fill:#fcf,stroke:#333,stroke-width:1px
  classDef datastore fill:#fcc,stroke:#333,stroke-width:1px
```
