How conardli/mcp-client-nodejs works
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).