# conardli/mcp-client-nodejs architecture

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

- Interactive diagram: https://gitdiagram.com/conardli/mcp-client-nodejs
- Repository: https://github.com/conardli/mcp-client-nodejs

## Overview

##### Project type and purpose
- This is a Node.js command‑line client (“MCP Client”) implementing the Model Context Protocol (MCP).
- It’s not a web app but a modular CLI tool that connects to external MCP servers and LLM providers (e.g., OpenAI).
- Purpose: let applications interact with LLMs via a standardized protocol, discover and invoke server‑provided tools, and log all interactions.

##### File structure
- Top‑level
- src/index.ts           ← CLI entrypoint
- src/mcpClient.ts       ← Core MCPClient class
- src/services/LLMService.ts, ToolService.ts ← Abstractions for model calls & tool calls
- src/utils               ← config loader, logger, JSON schema validation
- src/types               ← shared TypeScript types and configs
- .env.example, config JSON, tsconfig, package.json, build artifacts
- No frontend directory; this is purely a backend/CLI package.

##### Extract architectural insights from the README
- Three layers:
a. Transport layer (StdioClientTransport)
b. Protocol layer (MCP protocol handling request/response and tool calls)
c. Model layer (LLMService wrapping OpenAI SDK)
- Logging system writes JSON logs to logs/
- Interactive, event‑driven loop: user input → protocol → model → optional tool → model → CLI output

##### Main components and their relationships
a. CLI Layer (index.ts)
- Reads env/config, parses CLI args (script path or server identifier + JSON config).
- Instantiates MCPClient.
b. Config Loader (utils/config.ts + types/config.ts)
- Loads .env via dotenv, merges CLI flags, JSON server list.
c. MCPClient (src/mcpClient.ts)
- Manages:
- Transport (spawn child process for MCP server, stdio)
- Protocol logic (handshake, tool discovery, request/response framing)
- Orchestrates user queries (interactive loop).
- Delegates calls to LLMService and ToolService.
d. LLMService (services/LLMService.ts)
- Wraps OpenAI or other LLM API calls (model layer).
- Sends user/system messages, receives structured responses.
e. ToolService (services/ToolService.ts)
- Given a tool call request from LLM, invokes the server via MCPClient’s transport.
- Parses and returns the tool result.
f. Transport Layer (part of MCPClient or separate class)
- Uses stdio to communicate with the external MCP server process.
g. Logging (utils/log.ts)
- Intercepts all LLM requests/responses, tool calls/results, errors.
- Writes JSON files under logs/.
h. External Systems
- MCP Server (JS or Python script).
- LLM Provider (e.g., OpenAI REST API).

##### Interactions & data flow
1. User types query in CLI → forwarded to MCPClient.
2. MCPClient packages into MCP protocol request → sent over stdio to server.
3. Server returns tool metadata → ToolService caches available tools.
4. CLI query triggers LLMService to send chat completion via OpenAI.
5. LLM decides on tool call or direct answer:
- If tool call: LLMService returns function call → MCPClient → ToolService → stdio → server → returns output → LLMService sends follow‑up to LLM.
- If direct answer: LLMService returns text.
6. MCPClient prints final answer, loop continues.
7. Logging wraps each request/response and writes to disk.

##### Architectural patterns and design principles
- Modular layering: CLI, transport, protocol, model, tool, logging.
- Single Responsibility: each service handles one aspect (LLM, tools, config, logs).
- Event‑driven interactive loop.
- Dependency injection in MCPClient for services and transport.

##### Final advice
- Be exhaustive: include all major TS modules and external endpoints.
- Both control flow (user → server via client) and data flow (messages, tool payloads).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% CLI & Config Layer
    subgraph "CLI & Config Layer"
        direction TB
        User["User\n(Console)"]:::external
        CLI["src/index.ts\n(CLI Entrypoint)"]:::internal
        ConfigLoader["config.ts"]:::util
        TypesConfig["src/types/config.ts"]:::util
        TypesIndex["src/types/index.ts"]:::util
        GlobalTypes["src/types/global.d.ts"]:::util
    end

    %% Core Client Layer
    subgraph "Core Client Layer"
        direction TB
        MCPClient["MCPClient\n(src/mcpClient.ts)"]:::internal
        Transport["StdioTransport\n(part of src/mcpClient.ts)"]:::internal
    end

    %% Services Layer
    subgraph "Services Layer"
        direction TB
        LLM["LLMService\n(src/services/LLMService.ts)"]:::internal
        Tool["ToolService\n(src/services/ToolService.ts)"]:::internal
    end

    %% Utilities
    subgraph "Utilities"
        direction TB
        Logger["log.ts"]:::util
        Schema["schema.ts"]:::util
        StaticDiagrams["public/"]:::util
    end

    %% External Systems
    subgraph "External Systems"
        direction TB
        MCPServer["MCP Server"]:::external
        OpenAI["OpenAI API"]:::external
        LogsStorage["logs/"]:::storage
    end

    %% Connections
    User -->|starts CLI| CLI
    CLI -->|loads config| ConfigLoader
    CLI -->|reads types| TypesConfig
    CLI --> TypesIndex
    CLI --> GlobalTypes
    CLI -->|instantiates| MCPClient

    MCPClient -->|delegates chat| LLM
    MCPClient -->|delegates tool| Tool
    MCPClient --> Transport
    Transport -->|stdio| MCPServer
    Tool --> Transport
    LLM -->|API call| OpenAI

    %% Logging
    MCPClient -->|log events| Logger
    LLM --> Logger
    Tool --> Logger
    Logger --> LogsStorage

    %% Click Events
    click CLI "https://github.com/conardli/mcp-client-nodejs/blob/main/src/index.ts"
    click ConfigLoader "https://github.com/conardli/mcp-client-nodejs/blob/main/src/utils/config.ts"
    click TypesConfig "https://github.com/conardli/mcp-client-nodejs/blob/main/src/types/config.ts"
    click TypesIndex "https://github.com/conardli/mcp-client-nodejs/blob/main/src/types/index.ts"
    click GlobalTypes "https://github.com/conardli/mcp-client-nodejs/blob/main/src/types/global.d.ts"
    click MCPClient "https://github.com/conardli/mcp-client-nodejs/blob/main/src/mcpClient.ts"
    click Transport "https://github.com/conardli/mcp-client-nodejs/blob/main/src/mcpClient.ts"
    click LLM "https://github.com/conardli/mcp-client-nodejs/blob/main/src/services/LLMService.ts"
    click Tool "https://github.com/conardli/mcp-client-nodejs/blob/main/src/services/ToolService.ts"
    click Logger "https://github.com/conardli/mcp-client-nodejs/blob/main/src/utils/log.ts"
    click Schema "https://github.com/conardli/mcp-client-nodejs/blob/main/src/utils/schema.ts"
    click StaticDiagrams "https://github.com/conardli/mcp-client-nodejs/tree/main/public/"

    %% Styles
    classDef internal fill:#bbdefb,stroke:#1e88e5,color:#0d47a1
    classDef util fill:#f5f5f5,stroke:#9e9e9e,color:#424242
    classDef external fill:#c8e6c9,stroke:#388e3c,color:#1b5e20
    classDef storage fill:#ffe082,stroke:#f57f17,color:#e65100
```
