Deploying an Agentic LLM with MCP and LangGraph
This guide walks through how to use LangGraph with LangChain MCP to deploy a tool-using agentic LLM. We’ll use a local model via Ollama, wrap tools using MCP, and connect everything into a reactive LangGraph agent.
🚀 Overview
- Model:
ChatOllama(e.g.llama3,granite3.3:2b) - Tools: Registered using
langchain_mcp - Agent: Built with
create_react_agent()fromlanggraph.prebuilt - Transport: Local
streamable-httpvia FastAPI
📦 Setup
Install the required packages:
pip install langgraph langchain langchain-mcp langchain-ollama fastapi uvicorn
🛠️ Step 1: Define a Tool with MCP
Create a file called tools/weather_tool.py:
from langchain_mcp import tool, mcp
import os
import requests
@tool
def get_weather(location: str) -> str:
"""Fetch current weather for a given location."""
api_key = os.getenv("WEATHER_API_KEY")
url = f"http://api.weatherapi.com/v1/current.json?key={api_key}&q={location}"
resp = requests.get(url)
data = resp.json()
return f"{location}: {data['current']['temp_c']}°C, {data['current']['condition']['text']}"
if __name__ == "__main__":
mcp.run(
transport={
"type": "streamable-http",
"bind": "127.0.0.1:8000",
"path": "/mcp"
}
)
Run it:
python tools/weather_tool.py
🧠 Step 2: Build the Agent with LangGraph
Create a new file main.py:
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain_ollama import ChatOllama
from langgraph.prebuilt import create_react_agent
import asyncio
import os
async def main():
client = MultiServerMCPClient({
"weather": {
"url": "http://localhost:8000/mcp",
"transport": "streamable_http"
}
})
tools = await client.get_tools()
model = ChatOllama(model="granite3.3:2b")
agent = create_react_agent(model, tools)
response = await agent.ainvoke({
"messages": [{"role": "user", "content": "What's the weather in London?"}]
})
for msg in response["messages"]:
print(msg)
if __name__ == "__main__":
asyncio.run(main())
✅ Result
The agent should:
- Parse the user query
- Select the
get_weathertool via MCP - Call the tool with
location='London' - Return the weather result in a natural reply
🧩 Notes
- MCP allows tools to run independently in different processes or machines
- LangGraph agents handle multi-step reasoning and looping if needed
- You can define multiple tools (e.g., weather, math, search) and MCP will orchestrate them
🔗 Useful Links
Happy building!