
mcp sse langchain example
Example of using MCP servers, both over sdio + sse. Also using langchain-mcp
Repository Info
About This Server
Example of using MCP servers, both over sdio + sse. Also using langchain-mcp
Model Context Protocol (MCP) - This server can be integrated with AI applications to provide additional context and capabilities, enabling enhanced AI interactions and functionality.
Documentation
MCP Multi-Server Demo with SSE Transport
This project demonstrates how to use the Model Context Protocol (MCP) with multiple servers using different transport methods (stdio and Server-Sent Events).
It is based on examples from: https://github.com/langchain-ai/langchain-mcp-adapters
Overview
The project consists of:
- A math server that provides basic arithmetic operations (add, multiply)
- A weather server that provides simulated weather information for different locations
- A main application that connects to both servers using the MultiServerMCPClient
- Integration with LangChain and OpenAI to create an agent that can use tools from both servers
Requirements
- Python 3.8+
- OpenAI API key
Installation
-
Clone the repository:
git clone https://github.com/yourusername/mcp-sse.git cd mcp-sse -
Install the required dependencies:
pip install -r requirements.txt -
Set up your OpenAI API key in a
.envfile:OPENAI_API_KEY=your-api-key-here
Project Structure
main.py: The main application that connects to both servers and runs the agentmath_server.py: A simple MCP server that provides math operations using stdio transportweather_server.py: A simple MCP server that provides weather information using SSE transportrequirements.txt: List of Python dependencies.env: Environment variables (contains your OpenAI API key)
How It Works
The application demonstrates how to use the MultiServerMCPClient to connect to multiple MCP servers with different transport methods:
- The math server uses stdio transport, which is a simple pipe-based communication method
- The weather server uses Server-Sent Events (SSE) transport, which is an HTTP-based protocol for server-to-client push notifications
The main application:
- Starts the weather server as a separate process
- Connects to both servers using the MultiServerMCPClient
- Creates a LangChain agent that can use tools from both servers
- Demonstrates using the agent to perform math calculations and get weather information
Usage
Run the main application:
python main.py
This will:
- Start the weather server on port 8000
- Connect to both the math and weather servers
- Run the agent with example queries for both math and weather
Example Queries
The demo includes two example queries:
- Math query: "what's (3 + 5) x 12?"
- Weather query: "what is the weather in nyc?"
Extending the Project
You can extend this project by:
- Adding more tools to the math or weather servers
- Creating additional MCP servers with different functionality
- Modifying the agent to handle more complex queries
- Connecting to real weather APIs instead of using simulated data
License
MIT
Acknowledgments
- This project uses the MCP (Model Context Protocol) developed by Anthropic
- Integration with LangChain for agent functionality
Quick Start
Clone the repository
git clone https://github.com/fredrikp999/mcp-sse-langchain-exampleInstall dependencies
cd mcp-sse-langchain-example
npm installFollow the documentation
Check the repository's README.md file for specific installation and usage instructions.
Repository Details
Recommended MCP Servers
Discord MCP
Enable AI assistants to seamlessly interact with Discord servers, channels, and messages.
Knit MCP
Connect AI agents to 200+ SaaS applications and automate workflows.
Apify MCP Server
Deploy and interact with Apify actors for web scraping and data extraction.
BrowserStack MCP
BrowserStack MCP Server for automated testing across multiple browsers.
Zapier MCP
A Zapier server that provides automation capabilities for various apps.