# langchain-ai/langchain-mcp-adapters architecture

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

- Interactive diagram: https://gitdiagram.com/langchain-ai/langchain-mcp-adapters
- Repository: https://github.com/langchain-ai/langchain-mcp-adapters

## Overview

##### Main Components
- Library Core (Package): Focus on the “langchain_mcp_adapters” directory which contains modules like client.py, prompts.py, and tools.py. These encapsulate the core functionality such as creating clients, converting MCP tools, and handling prompts.
- MCP Client/Session: Highlight the classes and functions that manage the connection to one or multiple MCP servers (e.g., MultiServerMCPClient in client.py). This component abstracts the different transport mechanisms (stdio, SSE).
- Tool Loader/Converter: Include the component (often found in tools.py) that wraps raw MCP tools and converts them into a format that LangChain agents can consume.
- Integration with External Libraries: Note the integration points with LangChain, LangGraph, and even Chat models (as shown in the examples in the README with ChatOpenAI and ChatAnthropic).
- CI/CD and Testing: Represent the supporting infrastructure such as the GitHub workflows (.github directory), the Makefile, and tests (unit tests in the tests directory). This shows operational structure and quality assurance.

##### Relationships and Data Flow
- Flow from the initial connection (ClientSession setup) → tool retrieval (via the adapter functions) → integration with creating the LangChain/LangGraph agent.
- Use of asynchronous context managers for client connections, emphasizing the flow control in asynchronous operations.

##### Architectural Patterns and Design Principles
- Adapter Pattern: Emphasize that the library acts as an adapter by converting MCP tools into a format consumable by other components.
- Modular & Extensible Design: Each component (client connection, tool conversion, and prompts handling) is modularized, as depicted by separate modules—making it easy to extend (for example, handling additional transports or tool types).
- Separation of Concerns: Divide the system into clear layers: communication (MCP client/session), conversion logic (tool loading/adaptation), and integration with the external agent ecosystem.

##### Detail Relevant Technologies and Frameworks
- Python Packaging: The project is structured as a Python library (using pyproject.toml for dependency management).
- CI/CD: GitHub Actions workflows (in .github/workflows) ensure linting, testing, and release operations are automated.
- Transports: Include details on supported transports (stdio and sse) as distinct “communication channels” with external MCP servers.

##### Specific Elements
- Nodes/Components:
- MCP Server(s) (illustrate at least two types shown in examples: Math and Weather servers)
- MultiServerMCPClient (client wrapper handling multiple connections)
- ClientSession and transport channels (stdio and sse)
- Tool Converter/Adapter (converting MCP tool signatures to LangChain tools)
- LangChain/LangGraph Agent (final consumer of the tools)
- Configuration and orchestration elements (graph entrypoint in graph.py and langgraph.json)
- Connections:
- A feedback loop to show asynchronous initialization and data flow.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    subgraph "External Systems"
        A["MCP Server: Math"]:::external
        B["MCP Server: Weather"]:::external
        C["LangChain/LangGraph Agent"]:::external
    end

    %% Internal Library Core Modules
    subgraph "Library Core"
        D["MultiServerMCPClient"]:::internal
        E["Tool Loader/Converter"]:::internal
        F["Prompt Handling"]:::internal
    end

    %% Integration & Configuration
    subgraph "Integration & Configuration"
        G["Graph Entrypoint (graph.py)"]:::internal
        H["Config File (langgraph.json)"]:::internal
    end

    %% CI/CD & Testing Infrastructure
    subgraph "CI/CD & Testing"
        I["GitHub Workflows (.github)"]:::cicd
        J["Makefile"]:::cicd
        K["Tests Directory (tests)"]:::cicd
        L["Documentation (README.md)"]:::cicd
    end

    %% Data Flow Connections (Vertical Layout)
    A -->|"stdio"| D
    B -->|"SSE"| D
    D -->|"loads_tools"| E
    D -->|"init_prompts"| F
    E -->|"converts_for_agent"| C
    F -->|"provides_prompts"| C
    G -->|"orchestrates"| C
    H -->|"configures"| C

    %% Support connection from CI/CD to Library (dotted line to indicate supporting infra)
    I ---|"supports"| D
    J ---|"builds/tests"| D
    K ---|"verifies"| D
    L ---|"documents"| D

    %% Click Events for component mapping
    click D "https://github.com/langchain-ai/langchain-mcp-adapters/blob/main/langchain_mcp_adapters/client.py"
    click E "https://github.com/langchain-ai/langchain-mcp-adapters/blob/main/langchain_mcp_adapters/tools.py"
    click F "https://github.com/langchain-ai/langchain-mcp-adapters/blob/main/langchain_mcp_adapters/prompts.py"
    click I "https://github.com/langchain-ai/langchain-mcp-adapters/tree/main/.github/"
    click J "https://github.com/langchain-ai/langchain-mcp-adapters/tree/main/Makefile"
    click K "https://github.com/langchain-ai/langchain-mcp-adapters/tree/main/tests/"
    click L "https://github.com/langchain-ai/langchain-mcp-adapters/blob/main/README.md"
    
    %% Styles
    classDef external fill:#CDEB8B,stroke:#4CAF50,stroke-width:2px;
    classDef internal fill:#BBDEFB,stroke:#1976D2,stroke-width:2px;
    classDef cicd fill:#FFE082,stroke:#F57C00,stroke-width:2px;
```
