Amin-alzz96/Agent-with-MCP-connection
If you are the rightful owner of Agent-with-MCP-connection and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
This project demonstrates a proof-of-concept for connecting a LangChain agent to a custom Model Context Protocol (MCP) tool, which is a web scraper that extracts data from websites using CSS selectors.
LangChain Agent with MCP Web Scraper Tool
This project demonstrates a proof-of-concept (POC) for connecting a LangChain (or LangGraph) agent to a custom Model Context Protocol (MCP) tool. The MCP tool is a web scraper that extracts data from any website using a CSS selector. The agent can invoke this tool as part of its reasoning process.
Features
- MCP Server: Exposes a web scraping tool via MCP protocol.
- LangChain Agent: Connects to the MCP tool and an LLM (OpenAI GPT-4o-mini by default).
- Async Integration: Uses async agent and tool invocation for compatibility.
- Configurable: Server configuration via
conf.json
.
Quick Start Guide
1. Prerequisites
- Python 3.12+
- uv for dependency management (or use pip if preferred)
- OpenAI API key (for LLM)
2. Clone the Repository
git clone <your-repo-url>
cd 08_MCP_Agent
3. Install Dependencies
uv pip install -r requirements.txt
# OR, if using pyproject.toml:
uv sync
4. Set Up Environment Variables
Create a .env
file in the project root:
OPENAI_API_KEY=your-openai-api-key
5. Run the Agent
In another terminal:
python main.py
You should see the agent invoke the web scraper tool and print the results.
Project Structure
main.py
— Entry point; runs the async agent and connects to the MCP tool.mcp_server.py
— Defines and serves the web scraping MCP tool.llm_config.py
— LLM (OpenAI) configuration.conf.json
— MCP server configuration.pyproject.toml
— Project dependencies.README.md
— This file.
How It Works
- MCP Server:
mcp_server.py
registers ascrape_website
tool that takes a URL and CSS selector, returning the text and attributes of matching elements. this app is designed to run locally so please make necessary changes inconf.json
file. - Agent:
main.py
loads the MCP tool usinglangchain_mcp_adapters
, connects it to the LLM, and runs an async agent loop. - Example: The agent is prompted to scrape all
<h1>
elements from a sample website.
Customization
- To add more MCP tools, define them in
mcp_server.py
and updateconf.json
. - To use a different LLM, modify
llm_config.py
.
License
MIT