Skip to content

Tusharbecoding/langchain-mcp-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LangChain MCP Server Demo

This project demonstrates how to create and use Model Context Protocol (MCP) servers with LangChain. MCP is a protocol that allows AI models to interact with external tools and data sources through a standardized interface.

What is MCP?

Model Context Protocol (MCP) is a protocol that enables AI models to:

  • Connect to external tools and data sources
  • Execute functions on remote servers
  • Access real-time information
  • Interact with databases, APIs, and other services

Project Structure

langchain-mcp-server/
├── client.py          # Main client that connects to MCP servers
├── mathserver.py      # Math MCP server (stdio transport)
├── weather.py         # Weather MCP server (HTTP transport)
├── requirements.txt   # Python dependencies
└── README.md         # This file

Components

1. Math Server (mathserver.py)

A simple MCP server that provides mathematical operations:

  • Transport: stdio (standard input/output)
  • Tools:
    • add(a: int, b: int) -> int: Adds two numbers
    • multiply(a: int, b: int) -> int: Multiplies two numbers

2. Weather Server (weather.py)

A basic MCP server that provides weather information:

  • Transport: streamable-http (HTTP-based)
  • Tools:
    • get_weather(city: str) -> str: Returns weather for a given city

3. Client (client.py)

A LangChain client that connects to multiple MCP servers and uses them with an AI agent:

  • Connects to both math and weather servers
  • Uses Groq's Qwen model for reasoning
  • Creates a ReAct agent that can use the MCP tools

Setup and Installation

  1. Install dependencies:

    pip install -r requirements.txt
  2. Set up environment variables: Create a .env file with your Groq API key:

    GROQ_API_KEY=your_groq_api_key_here
    
  3. Start the weather server (in a separate terminal):

    python weather.py

    This will start the weather server on http://localhost:8000/mcp

Usage

Running the Demo

  1. Start the weather server (if not already running):

    python weather.py
  2. Run the main client:

    python client.py

The client will:

  • Connect to both the math server (via stdio) and weather server (via HTTP)
  • Ask the AI agent to solve a math problem: (19 + 17) * 45
  • Ask the AI agent to get weather information for Tokyo
  • Display the responses

Expected Output

Math Response: The result of (19 + 17) * 45 is 1620
Weather Response: The weather in Tokyo is sunny

How It Works

MCP Server Creation

Each MCP server is created using the FastMCP framework:

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("ServerName")

@mcp.tool()
def my_function(param: str) -> str:
    """Function description"""
    return "result"

if __name__ == "__main__":
    mcp.run(transport="stdio")  # or "streamable-http"

Client Configuration

The client connects to multiple servers using MultiServerMCPClient:

client = MultiServerMCPClient({
    "math": {
        "command": "python",
        "args": ["mathserver.py"],
        "transport": "stdio",
    },
    "weather": {
        "url": "http://localhost:8000/mcp",
        "transport": "streamable-http",
    }
})

Transport Types

  • stdio: Uses standard input/output for local communication
  • streamable-http: Uses HTTP for network communication

Key Concepts Demonstrated

  1. Multi-Server Architecture: Connecting to multiple MCP servers simultaneously
  2. Different Transport Protocols: stdio for local servers, HTTP for remote servers
  3. Tool Integration: MCP tools become available to LangChain agents
  4. AI Agent Reasoning: The agent can choose which tools to use based on the user's request

Extending the Project

Adding New MCP Servers

  1. Create a new server file (e.g., databaseserver.py)
  2. Define tools using the @mcp.tool() decorator
  3. Add the server configuration to client.py
  4. Restart the client

Adding New Tools

To add new tools to existing servers, simply add new functions with the @mcp.tool() decorator:

@mcp.tool()
def divide(a: float, b: float) -> float:
    """Divide two numbers"""
    return a / b

Dependencies

  • langchain-groq: Groq LLM integration
  • langchain-mcp-adapters: MCP client adapters for LangChain
  • mcp: Model Context Protocol framework
  • langgraph: For creating ReAct agents

This project serves as a simple introduction to MCP servers and demonstrates how they can be integrated with LangChain for AI-powered applications.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages