# leehack/mcp_dart architecture

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

- Interactive diagram: https://gitdiagram.com/leehack/mcp_dart
- Repository: https://github.com/leehack/mcp_dart

## Overview

##### Project type and purpose
- This is a Dart SDK library implementing the Model Context Protocol (MCP) for both clients and servers. It’s not a single full‐stack application, but rather a protocol SDK with pluggable transports (stdio, HTTP, SSE, streams) and example binaries showing usage.

##### File structure
- Top-level:
- lib/: the published SDK.
- example/: standalone demo clients and servers.
- test/: unit and integration tests.
- .github/, CI workflows, lint and build configs.
- Inside lib/src:
- shared/: core protocol definitions (messages, transports, UUID, URI template).
- client/: client-side API and transports (stdio, streamable_https).
- server/: server-side API, tool registration, transports (stdio, SSE, streamable_https).
- Examples show how to wire up a McpServer or McpClient with specific transports and host processes.

##### README insights
- This SDK aligns with a published spec.
- Supports multiple transports symmetrically (client + server) except SSE (server only, deprecated).
- Shows typical startup code: server.tool(...) + server.connect(transport).

a. Main components:
- MCP Client Library
- MCP Server Library
- Transport Layer (std I/O, Streamable HTTPS, SSE, In‐process stream)
- Protocol Core (shared message definitions, module loader)
- Tooling Framework (server.tool callbacks, resource & tool capabilities)
- Example Applications (CLI binaries for demo)
- External LLM Host or Consumer (e.g., Claude Desktop)
b. Relationships & Data Flow:
- Client Library → serializes MCP requests → Transport Layer → delivers JSON to Server Layer
- Server Library → deserializes MCP requests → Core Protocol Handler → dispatches to registered tools/resources → generates MCP responses → back through transport to client
- Example binaries instantiate Client or Server + select transport
- External LLM Host communicates with client executables via stdin/stdout or HTTP
c. Architectural patterns & principles:
- Layered modular design: transport, client/server API, shared protocol
- Dependency inversion: transports implement abstract interfaces in shared module
- Plugin pattern: server.tool(...) registers new tool handlers
- Single responsibility: each transport and module handles one concern
d. Key technologies & libraries:
- Dart SDK ≥3.0
- Built-in dart:io streams for stdio transport
- HTTP server/client for streamable HTTPS
- EventSource or SSE server manager
- JSON encoding/decoding

## Diagram (Mermaid)

```mermaid
flowchart TB
    External["External LLM Host"]:::example
    External -->|"stdin/stdout or HTTP"| ANTHRO_MAIN
    External -->|"stdin/stdout or HTTP"| GEMINI_MAIN

    subgraph "Example Applications"
        ANTHRO_MAIN["anthropic-client/bin/main.dart"]:::example
        ANTHROPIC_LIB["anthropic-client/lib/anthropic_client.dart"]:::example
        GEMINI_MAIN["gemini-client/bin/main.dart"]:::example
        GEMINI_LIB["gemini-client/lib/gemini_client.dart"]:::example
        CLIENT_STDIO_EX["example/client_stdio.dart"]:::example
        SERVER_STDIO_EX["example/server_stdio.dart"]:::example
        SERVER_SSE_EX["example/server_sse.dart"]:::example
        CLIENT_HTTPS_EX["example/streamable_https/client_streamable_https.dart"]:::example
        SERVER_HTTPS_EX["example/streamable_https/server_streamable_https.dart"]:::example
        SERVER_IOSTREAM_EX["example/iostream-client-server/server_iostream.dart"]:::example
    end

    ANTHRO_MAIN -->|"uses"| CLIENT_LIB
    ANTHROPIC_LIB -->|"uses"| CLIENT_LIB
    GEMINI_MAIN -->|"uses"| CLIENT_LIB
    GEMINI_LIB -->|"uses"| CLIENT_LIB
    CLIENT_STDIO_EX -->|"uses"| CLIENT_LIB
    CLIENT_HTTPS_EX -->|"uses"| CLIENT_LIB

    SERVER_STDIO_EX -->|"uses"| SERVER_LIB
    SERVER_SSE_EX -->|"uses"| SERVER_LIB
    SERVER_HTTPS_EX -->|"uses"| SERVER_LIB
    SERVER_IOSTREAM_EX -->|"uses"| SERVER_LIB

    subgraph "MCP Client Library" 
        CLIENT_LIB["client.dart\nmodule.dart"]:::client
    end

    subgraph "MCP Server Library"
        SERVER_LIB["mcp.dart\nserver.dart\nmodule.dart"]:::server
    end

    subgraph "Shared Protocol Core"
        PROTOCOL["protocol.dart"]:::shared
        TRANSPORT_IF["transport.dart"]:::shared
        MODULE["module.dart"]:::shared
        IOSTREAM["iostream.dart"]:::shared
        STDIO_CORE["stdio.dart"]:::shared
        URI["uri_template.dart"]:::shared
        UUID["uuid.dart"]:::shared
    end

    CLIENT_LIB -->|calls| PROTOCOL
    CLIENT_LIB -->|calls| TRANSPORT_IF
    CLIENT_LIB -->|calls| MODULE

    SERVER_LIB -->|calls| PROTOCOL
    SERVER_LIB -->|calls| TRANSPORT_IF
    SERVER_LIB -->|calls| MODULE

    subgraph "Transport Adaptors" 
        subgraph "StdIO Adaptors" 
            CLIENT_STDIO["lib/src/client/stdio.dart"]:::transport
            SERVER_STDIO["lib/src/server/stdio.dart"]:::transport
        end
        subgraph "Streamable HTTPS Adaptors"
            CLIENT_HTTPS["lib/src/client/streamable_https.dart"]:::transport
            SERVER_HTTPS["lib/src/server/streamable_https.dart"]:::transport
        end
        subgraph "SSE Adaptors (deprecated)"
            SERVER_SSE["lib/src/server/sse.dart"]:::transport
            SERVER_SSE_MGR["lib/src/server/sse_server_manager.dart"]:::transport
        end
    end

    CLIENT_STDIO -->|implements| TRANSPORT_IF
    CLIENT_HTTPS -->|implements| TRANSPORT_IF

    SERVER_STDIO -->|implements| TRANSPORT_IF
    SERVER_HTTPS -->|implements| TRANSPORT_IF
    SERVER_SSE -->|implements| TRANSPORT_IF
    SERVER_SSE_MGR -->|manages| SERVER_SSE

    classDef shared fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef client fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef server fill:#ffe5cc,stroke:#333,stroke-width:1px
    classDef transport fill:#e0e0e0,stroke:#333,stroke-width:1px
    classDef example fill:#e5ccff,stroke:#333,stroke-width:1px

    click CLIENT_LIB "https://github.com/leehack/mcp_dart/blob/main/lib/src/client/client.dart"
    click CLIENT_LIB "https://github.com/leehack/mcp_dart/blob/main/lib/src/client/module.dart"
    click SERVER_LIB "https://github.com/leehack/mcp_dart/blob/main/lib/src/server/mcp.dart"
    click SERVER_LIB "https://github.com/leehack/mcp_dart/blob/main/lib/src/server/server.dart"
    click SERVER_LIB "https://github.com/leehack/mcp_dart/blob/main/lib/src/server/module.dart"
    click PROTOCOL "https://github.com/leehack/mcp_dart/blob/main/lib/src/shared/protocol.dart"
    click TRANSPORT_IF "https://github.com/leehack/mcp_dart/blob/main/lib/src/shared/transport.dart"
    click MODULE "https://github.com/leehack/mcp_dart/blob/main/lib/src/shared/module.dart"
    click IOSTREAM "https://github.com/leehack/mcp_dart/blob/main/lib/src/shared/iostream.dart"
    click STDIO_CORE "https://github.com/leehack/mcp_dart/blob/main/lib/src/shared/stdio.dart"
    click URI "https://github.com/leehack/mcp_dart/blob/main/lib/src/shared/uri_template.dart"
    click UUID "https://github.com/leehack/mcp_dart/blob/main/lib/src/shared/uuid.dart"
    click CLIENT_STDIO "https://github.com/leehack/mcp_dart/blob/main/lib/src/client/stdio.dart"
    click SERVER_STDIO "https://github.com/leehack/mcp_dart/blob/main/lib/src/server/stdio.dart"
    click CLIENT_HTTPS "https://github.com/leehack/mcp_dart/blob/main/lib/src/client/streamable_https.dart"
    click SERVER_HTTPS "https://github.com/leehack/mcp_dart/blob/main/lib/src/server/streamable_https.dart"
    click SERVER_SSE "https://github.com/leehack/mcp_dart/blob/main/lib/src/server/sse.dart"
    click SERVER_SSE_MGR "https://github.com/leehack/mcp_dart/blob/main/lib/src/server/sse_server_manager.dart"
    click ANTHRO_MAIN "https://github.com/leehack/mcp_dart/blob/main/example/anthropic-client/bin/main.dart"
    click ANTHROPIC_LIB "https://github.com/leehack/mcp_dart/blob/main/example/anthropic-client/lib/anthropic_client.dart"
    click GEMINI_MAIN "https://github.com/leehack/mcp_dart/blob/main/example/gemini-client/bin/main.dart"
    click GEMINI_LIB "https://github.com/leehack/mcp_dart/blob/main/example/gemini-client/lib/gemini_client.dart"
    click CLIENT_STDIO_EX "https://github.com/leehack/mcp_dart/blob/main/example/client_stdio.dart"
    click SERVER_STDIO_EX "https://github.com/leehack/mcp_dart/blob/main/example/server_stdio.dart"
    click SERVER_SSE_EX "https://github.com/leehack/mcp_dart/blob/main/example/server_sse.dart"
    click CLIENT_HTTPS_EX "https://github.com/leehack/mcp_dart/blob/main/example/streamable_https/client_streamable_https.dart"
    click SERVER_HTTPS_EX "https://github.com/leehack/mcp_dart/blob/main/example/streamable_https/server_streamable_https.dart"
    click SERVER_IOSTREAM_EX "https://github.com/leehack/mcp_dart/blob/main/example/iostream-client-server/server_iostream.dart"
```
