### Copy Environment Example File - Bash Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/README.md Copies the example environment file `.env.example` to `.env` to start configuring API keys and settings. ```bash cp .env.example .env ``` -------------------------------- ### Setup Virtual Environment and Install Dependencies - Bash Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/README.md Creates a Python virtual environment using uv, installs project dependencies from requirements.txt, and activates the virtual environment. ```bash uv venv uv pip install -r requirements.txt source .venv/bin/activate # For Windows: .venv\Scripts\activate ``` -------------------------------- ### Start Remote MCP Server (Bash) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Activates the Python virtual environment and runs the `mcp_server_remote.py` script to start the remote MCP server. This server should be running before connecting clients. ```bash source .venv/bin/activate python mcp_server_remote.py ``` -------------------------------- ### Install uv on Windows (PowerShell) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Installs the `uv` package manager on Windows systems using a PowerShell script. `uv` is required for efficient dependency management. ```bash # Windows (PowerShell) irm https://astral.sh/uv/install.ps1 | iex ``` -------------------------------- ### Start Streamlit Application - Bash Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/README.md Runs the main Streamlit application script `app.py` to start the web interface. ```bash streamlit run app.py ``` -------------------------------- ### Install Python Dependencies with uv (Bash) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Installs project dependencies listed in `requirements.txt` using the `uv pip` command. Requires `uv` to be installed. ```bash uv pip install -r requirements.txt ``` -------------------------------- ### Configure API Keys in .env - Bash Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/README.md Example configuration for the `.env` file, showing how to set API keys for Anthropic, OpenAI, and LangSmith, along with LangSmith tracing settings. ```bash ANTHROPIC_API_KEY=your_anthropic_api_key OPENAI_API_KEY=your_openai_api_key LANGSMITH_API_KEY=your_langsmith_api_key LANGSMITH_TRACING=true LANGSMITH_ENDPOINT=https://api.smith.langchain.com LANGSMITH_PROJECT=LangGraph-MCP-Agents ``` -------------------------------- ### Install uv on macOS/Linux (Bash) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Installs the `uv` package manager on macOS or Linux systems using a curl script. `uv` is required for efficient dependency management. ```bash # macOS/Linux curl -LsSf https://astral.sh/uv/install.sh | sh ``` -------------------------------- ### Executing Agent with Retriever and Sequential Thinking Tools (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb This snippet provides another example of running the LangGraph agent. The prompt instructs the agent to first use the 'retriever' tool to find information and then use the 'Sequential Thinking' tool to write a report based on the findings. It also uses the `config`. ```python await astream_graph( agent, { "messages": ( "Use the `retriever` tool to search for information about generative AI developed by Samsung Electronics, " "and then use the `Sequential Thinking` tool to write a report." ) }, config=config, ) ``` -------------------------------- ### Executing Agent with Desktop Commander Tool (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb This snippet shows an example of running the LangGraph agent using `astream_graph`. The prompt is designed to utilize the 'Desktop Commander' tool to list the folder structure, excluding the `.venv` directory. It uses the previously defined `config`. ```python await astream_graph( agent, { "messages": "Draw the folder structure including the current path as a tree. However, exclude the .venv folder from the output." }, config=config, ) ``` -------------------------------- ### Enable and Configure Login in .env - Bash Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/README.md Configuration for the `.env` file to enable the login feature and set a default user ID and password. ```bash USE_LOGIN=true USER_ID=admin USER_PASSWORD=admin123 ``` -------------------------------- ### Clone Project Repository - Bash Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/README.md Clones the project repository from GitHub and changes the current directory into the cloned repository. ```bash git clone https://github.com/teddynote-lab/langgraph-mcp-agents.git cd langgraph-mcp-agents ``` -------------------------------- ### Disable Login in .env - Bash Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/README.md Configuration for the `.env` file to disable the login feature. ```bash USE_LOGIN=false ``` -------------------------------- ### Initialize MultiServerMCPClient Explicitly (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Creates a `MultiServerMCPClient` instance and explicitly initializes the connection using `await client.__aenter__()`. This keeps the session open for subsequent operations until explicitly closed. It then prints the loaded tools. ```python # 1. Create client client = MultiServerMCPClient( { "weather": { "url": "http://localhost:8005/sse", "transport": "sse" } } ) # 2. Explicitly initialize connection (this part is necessary) # Initialize await client.__aenter__() # Now tools are loaded print(client.get_tools()) # Tools are displayed ``` -------------------------------- ### Run LangGraph Agent with StdIO Client Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Connects to a server using a stdio client, initializes a client session, loads tools via MCP, creates a React agent using `create_react_agent`, and streams the response from the agent graph for a specific query. Requires `stdio_client`, `ClientSession`, `load_mcp_tools`, `create_react_agent`, and `astream_graph`. ```python async with stdio_client(server_params) as (read, write): # Create client session async with ClientSession(read, write) as session: # Initialize connection await session.initialize() # Load MCP tools (in this case, the retriever tool) tools = await load_mcp_tools(session) # Create and run the agent agent = create_react_agent(model, tools) # Stream agent responses await astream_graph( agent, { "messages": "Search for the name of the generative AI developed by Samsung Electronics" }, ) ``` -------------------------------- ### Connect to Local MCP Server using Stdio (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Demonstrates connecting to a local MCP server (`mcp_server_local.py`) using the Stdio transport method. It sets up server parameters, uses `stdio_client` within an `async with` block, initializes a `ClientSession`, loads tools, creates a LangGraph agent, and streams a response. ```python from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client from langgraph.prebuilt import create_react_agent from langchain_mcp_adapters.tools import load_mcp_tools from langchain_anthropic import ChatAnthropic # Initialize Anthropic's Claude model model = ChatAnthropic( model_name="claude-3-7-sonnet-latest", temperature=0, max_tokens=20000 ) # Set up StdIO server parameters # - command: Path to Python interpreter # - args: MCP server script to execute server_params = StdioServerParameters( command="./.venv/bin/python", args=["mcp_server_local.py"], ) # Use StdIO client to communicate with the server async with stdio_client(server_params) as (read, write): # Create client session async with ClientSession(read, write) as session: # Initialize connection await session.initialize() # Load MCP tools tools = await load_mcp_tools(session) print(tools) # Create agent agent = create_react_agent(model, tools) # Stream agent responses await astream_graph(agent, {"messages": "What's the weather like in Seoul?"}) ``` -------------------------------- ### Initializing MultiServerMCPClient with Smithery and Custom Tools (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb This snippet initializes the `MultiServerMCPClient` to connect to multiple MCP servers, including Smithery's Sequential Thinking and Desktop Commander, and a custom document retriever. It configures each server with its command, arguments, and sets the transport to `stdio`. It also initializes the LLM and explicitly connects to the servers. ```python from langchain_mcp_adapters.client import MultiServerMCPClient from langgraph.prebuilt import create_react_agent from langchain_anthropic import ChatAnthropic # Initialize LLM model model = ChatAnthropic(model="claude-3-7-sonnet-latest", temperature=0, max_tokens=20000) # 1. Create client client = MultiServerMCPClient( { "server-sequential-thinking": { "command": "npx", "args": [ "-y", "@smithery/cli@latest", "run", "@smithery-ai/server-sequential-thinking", "--key", "your_smithery_api_key", ], "transport": "stdio", # Add communication using stdio method }, "desktop-commander": { "command": "npx", "args": [ "-y", "@smithery/cli@latest", "run", "@wonderwhy-er/desktop-commander", "--key", "your_smithery_api_key", ], "transport": "stdio", # Add communication using stdio method }, "document-retriever": { "command": "./.venv/bin/python", # Update with the absolute path to the mcp_server_rag.py file "args": ["./mcp_server_rag.py"], # Communication using stdio (standard input/output) "transport": "stdio", }, } ) # 2. Explicitly initialize connection await client.__aenter__() ``` -------------------------------- ### Creating LangGraph ReAct Agent with MCP Tools (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb This snippet demonstrates how to create a ReAct agent using `langgraph.prebuilt.create_react_agent`. It takes the initialized LLM and the tools provided by the `MultiServerMCPClient`. It also sets up a `MemorySaver` for checkpointing and defines a `RunnableConfig`. ```python from langgraph.checkpoint.memory import MemorySaver from langchain_core.runnables import RunnableConfig # Set up configuration config = RunnableConfig(recursion_limit=30, thread_id=3) # Create agent agent = create_react_agent(model, client.get_tools(), checkpointer=MemorySaver()) ``` -------------------------------- ### Combine LangChain and MCP Tools Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Initializes a `TavilySearchResults` tool from `langchain_community` to perform news searches. It then retrieves the existing tools from the `MultiServerMCPClient` and combines them with the newly created Tavily tool into a single list. ```python from langchain_community.tools.tavily_search import TavilySearchResults # Initialize the Tavily search tool (news type, news from the last 3 days) tavily = TavilySearchResults(max_results=3, topic="news", days=3) # Use it together with existing MCP tools tools = client.get_tools() + [tavily] ``` -------------------------------- ### Initialize Multi-Server MCP Client with StdIO and SSE Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Initializes a `MultiServerMCPClient` configured to connect to a 'document-retriever' server via stdio and a 'langchain-dev-docs' server via SSE. It also initializes a `ChatAnthropic` model. The client connection is explicitly initialized using an async context manager. ```python from langchain_mcp_adapters.client import MultiServerMCPClient from langgraph.prebuilt import create_react_agent from langchain_anthropic import ChatAnthropic # Initialize Anthropic's Claude model model = ChatAnthropic( model_name="claude-3-7-sonnet-latest", temperature=0, max_tokens=20000 ) # 1. Create multi-server MCP client client = MultiServerMCPClient( { "document-retriever": { "command": "./.venv/bin/python", # Update with the absolute path to mcp_server_rag.py file "args": ["./mcp_server_rag.py"], # Communicate via stdio (using standard input/output) "transport": "stdio", }, "langchain-dev-docs": { # Make sure the SSE server is running "url": "https://teddynote.io/mcp/langchain/sse", # Communicate via SSE (Server-Sent Events) "transport": "sse", }, } ) # 2. Initialize connection explicitly through async context manager await client.__aenter__() ``` -------------------------------- ### Connect to Remote MCP Server using async with (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Demonstrates connecting to a remote MCP server using `MultiServerMCPClient` with SSE transport within an `async with` block. It initializes the client, retrieves tools, creates a LangGraph agent, and streams a response. The session is automatically closed upon exiting the block. ```python from langchain_mcp_adapters.client import MultiServerMCPClient from langgraph.prebuilt import create_react_agent from utils import ainvoke_graph, astream_graph from langchain_anthropic import ChatAnthropic model = ChatAnthropic( model_name="claude-3-7-sonnet-latest", temperature=0, max_tokens=20000 ) async with MultiServerMCPClient( { "weather": { # Must match the server's port (port 8005) "url": "http://localhost:8005/sse", "transport": "sse" } } ) as client: print(client.get_tools()) agent = create_react_agent(model, client.get_tools()) answer = await astream_graph( agent, {"messages": "What's the weather like in Seoul?"} ) ``` -------------------------------- ### Create LangGraph React Agent with MCP Tools and Memory Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Creates a React agent using the `create_react_agent` function, incorporating tools retrieved from the initialized `MultiServerMCPClient`. It sets a specific prompt and configures the agent with a `MemorySaver` for managing conversation history and state. ```python from langgraph.checkpoint.memory import MemorySaver from langchain_core.runnables import RunnableConfig prompt = ( "You are a smart agent. " "Use `retriever` tool to search on AI related documents and answer questions." "Use `langchain-dev-docs` tool to search on langchain / langgraph related documents and answer questions." "Answer in English." ) agent = create_react_agent( model, client.get_tools(), prompt=prompt, checkpointer=MemorySaver() ) ``` -------------------------------- ### Create LangGraph Agent with Explicitly Initialized Client (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Creates a LangGraph ReAct agent using a pre-initialized `MultiServerMCPClient` instance. The agent is configured with a ChatAnthropic model and the tools obtained from the client. ```python # Create agent agent = create_react_agent(model, client.get_tools()) ``` -------------------------------- ### Stream Agent Graph with LangChain Dev Docs Tool Query Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Defines a `RunnableConfig` with a recursion limit and thread ID. It then executes the agent graph asynchronously using `astream_graph`, providing a query specifically targeting the `langchain-dev-docs` tool and applying the defined configuration. ```python config = RunnableConfig(recursion_limit=30, thread_id=1) await astream_graph( agent, { "messages": "Please tell me about the definition of self-rag by referring to the langchain-dev-docs" }, config=config, ) ``` -------------------------------- ### Configure Stdio Client for RAG Server (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Sets up the `StdioServerParameters` to connect to a local MCP server specifically designed for RAG (`mcp_server_rag.py`). This configuration specifies the Python interpreter and the script path for the server process. ```python from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client from langchain_mcp_adapters.tools import load_mcp_tools from langgraph.prebuilt import create_react_agent from langchain_anthropic import ChatAnthropic from utils import astream_graph # Initialize Anthropic's Claude model model = ChatAnthropic( model_name="claude-3-7-sonnet-latest", temperature=0, max_tokens=20000 ) # Set up StdIO server parameters for the RAG server server_params = StdioServerParameters( command="./.venv/bin/python", args=["./mcp_server_rag.py"], ) ``` -------------------------------- ### Stream Agent Graph with Retriever Tool Query (Combined Tools) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Executes the agent graph asynchronously using `astream_graph` with a query targeting the `retriever` tool. This confirms that the MCP-managed tools remain functional even after adding standard LangChain tools to the agent's toolset. ```python await astream_graph( agent, { "messages": "Use the `retriever` tool to search for the name of the generative AI developed by Samsung Electronics" }, config=config, ) ``` -------------------------------- ### Stream Agent Graph with Retriever Tool Query Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Defines a `RunnableConfig` with a recursion limit and thread ID. It then executes the agent graph asynchronously using `astream_graph`, providing a query specifically targeting the `retriever` tool and applying the defined configuration. ```python config = RunnableConfig(recursion_limit=30, thread_id=1) await astream_graph( agent, { "messages": "Use the `retriever` tool to search for the name of the generative AI developed by Samsung Electronics" }, config=config, ) ``` -------------------------------- ### Execute Graph with Explicitly Initialized Client (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Runs the LangGraph agent using the `astream_graph` utility function. This execution occurs while the `MultiServerMCPClient` session, initialized explicitly, is still active, allowing the agent to use the client's tools. ```python await astream_graph(agent, {"messages": "What's the weather like in Seoul?"}) ``` -------------------------------- ### Attempt Graph Execution After Session Close (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Shows an attempt to run the LangGraph agent after the `async with` block has exited, illustrating that the client session is closed and tools are no longer accessible. ```python await astream_graph(agent, {"messages": "What's the weather like in Seoul?"}) ``` -------------------------------- ### Stream Agent Graph with Tavily Search Tool Query Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Executes the agent graph asynchronously using `astream_graph` with a query designed to trigger the `TavilySearchResults` tool. This demonstrates the agent's ability to use the combined set of tools. ```python await astream_graph( agent, {"messages": "Tell me about today's news for me"}, config=config ) ``` -------------------------------- ### Create React Agent with Combined LangChain and MCP Tools Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Creates a new React agent using the `create_react_agent` function. This agent is configured with the combined list of tools, including both MCP-managed tools and a standard LangChain tool like `TavilySearchResults`. It also uses `MemorySaver` for state management. ```python from langgraph.checkpoint.memory import MemorySaver from langchain_core.runnables import RunnableConfig prompt = "You are a smart agent with various tools. Answer questions in English." agent = create_react_agent(model, tools, prompt=prompt, checkpointer=MemorySaver()) ``` -------------------------------- ### Stream Agent Graph for Multi-Turn Conversation Summary Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Executes the agent graph asynchronously using `astream_graph` with a query requesting a summary of the previous conversation. This demonstrates the use of the `MemorySaver` configured in the agent to maintain context across turns. ```python await astream_graph( agent, {"messages": "Summarize the previous content in bullet points"}, config=config, ) ``` -------------------------------- ### Load Environment Variables (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-ENG.ipynb Loads environment variables from a `.env` file into the process environment. Uses the `python-dotenv` library. The `override=True` argument ensures existing variables are overwritten. ```python from dotenv import load_dotenv load_dotenv(override=True) ``` -------------------------------- ### Creating MCP Client with Smithery Tools (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Creates a MultiServerMCPClient instance configured to connect to multiple MCP servers, including 'server-sequential-thinking' and 'desktop-commander' from Smithery.ai, as well as the local 'document-retriever'. All connections are configured to use the 'stdio' transport via npx. ```Python from langchain_mcp_adapters.client import MultiServerMCPClient from langgraph.prebuilt import create_react_agent from langchain_anthropic import ChatAnthropic # LLM 모델 초기화 model = ChatAnthropic(model="claude-3-7-sonnet-latest", temperature=0, max_tokens=20000) # 1. 클라이언트 생성 client = MultiServerMCPClient( { "server-sequential-thinking": { "command": "npx", "args": [ "-y", "@smithery/cli@latest", "run", "@smithery-ai/server-sequential-thinking", "--key", "89a4780a-53b7-4b7b-92e9-a29815f2669b" ], "transport": "stdio" # stdio 방식으로 통신을 추가합니다. }, "desktop-commander": { "command": "npx", "args": [ "-y", "@smithery/cli@latest", "run", "@wonderwhy-er/desktop-commander", "--key", "89a4780a-53b7-4b7b-92e9-a29815f2669b" ], "transport": "stdio" # stdio 방식으로 통신을 추가합니다. }, "document-retriever": { "command": "./.venv/bin/python", # mcp_server_rag.py 파일의 절대 경로로 업데이트해야 합니다 "args": ["./mcp_server_rag.py"], # stdio 방식으로 통신 (표준 입출력 사용) "transport": "stdio" } } ) ``` -------------------------------- ### Combining MCP and LangChain Tools (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Initializes a Tavily search tool from LangChain Community, configured for news search within the last 3 days. It then combines the tools obtained from the MCP client with this new Tavily tool into a single list. ```Python from langchain_community.tools.tavily_search import TavilySearchResults # Tavily 검색 도구를 초기화 합니다. (news 타입, 최근 3일 내 뉴스) tavily = TavilySearchResults(max_results=3, topic="news", days=3) # 기존의 MCP 도구와 함께 사용합니다. tools = client.get_tools() + [tavily] ``` -------------------------------- ### Running Agent with Retriever Tool (Combined Tools) (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Executes the LangGraph agent asynchronously with the combined toolset. It sends a message asking the agent to use the 'retriever' tool to search for the name of a generative AI developed by Samsung Electronics, confirming that the original MCP tools are still functional alongside the new tools. ```Python await astream_graph( agent, { "messages": "\`retriever\` 도구를 사용해서 삼성전자가 개발한 생성형 AI 이름을 검색해줘" }, config=config, ) ``` -------------------------------- ### Running Agent with Langchain-Dev-Docs Tool (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Executes the LangGraph agent asynchronously with the same configuration. It sends a message asking the agent to use the 'langchain-dev-docs' tool to explain the definition of self-rag, referencing the relevant documentation. ```Python config = RunnableConfig(recursion_limit=30, thread_id=1) await astream_graph( agent, {"messages": "langgraph-dev-docs 참고해서 self-rag 의 정의에 대해서 알려줘"}, config=config, ) ``` -------------------------------- ### Initializing MCP Client Connection (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Explicitly initializes the asynchronous connection for the MultiServerMCPClient. This step is necessary to establish communication with the configured MCP servers before using the client's tools. ```Python await client.__aenter__() ``` -------------------------------- ### Creating Agent with Combined Tools (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Creates a new ReAct agent using LangGraph, similar to the previous agent creation. This agent uses the updated list of tools, which now includes both the MCP tools and the newly added LangChain Tavily search tool. ```Python from langgraph.checkpoint.memory import MemorySaver from langchain_core.runnables import RunnableConfig # 재귀 제한 및 스레드 아이디 설정 config = RunnableConfig(recursion_limit=30, thread_id=2) # 프롬프트 설정 prompt = "You are a smart agent with various tools. Answer questions in Korean." # 에이전트 생성 agent = create_react_agent(model, tools, prompt=prompt, checkpointer=MemorySaver()) ``` -------------------------------- ### Running Agent with Retriever Tool (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Executes the LangGraph agent asynchronously with a specific configuration. It sends a message prompting the agent to use the 'retriever' tool to search for the name of a generative AI developed by Samsung Electronics. ```Python config = RunnableConfig(recursion_limit=30, thread_id=1) await astream_graph( agent, { "messages": "\`retriever\` 도구를 사용해서 삼성전자가 개발한 생성형 AI 이름을 검색해줘" }, config=config, ) ``` -------------------------------- ### Running Agent with Tavily Tool (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Executes the LangGraph agent asynchronously with the updated configuration and agent instance. It sends a message prompting the agent to use the 'tavily' tool to search for today's news, demonstrating the use of the combined toolset. ```Python await astream_graph(agent, {"messages": "오늘 뉴스 찾아줘"}, config=config) ``` -------------------------------- ### Creating MultiServerMCPClient (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Creates a client for the Multi-Server Communication Protocol (MCP). It is configured with two servers: 'document-retriever' using stdio transport and 'langchain-dev-docs' using sse transport. ```Python client = MultiServerMCPClient( { "document-retriever": { "command": "./.venv/bin/python", # mcp_server_rag.py 파일의 절대 경로로 업데이트해야 합니다 "args": ["./mcp_server_rag.py"], # stdio 방식으로 통신 (표준 입출력 사용) "transport": "stdio" }, "langchain-dev-docs": { # SSE 서버가 실행 중인지 확인하세요 "url": "https://teddynote.io/mcp/langchain/sse", # SSE(Server-Sent Events) 방식으로 통신 "transport": "sse" } } ) ``` -------------------------------- ### Creating LangGraph ReAct Agent (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Creates a ReAct agent using LangGraph. It utilizes the initialized language model, tools obtained from the MCP client, a specific prompt, and a MemorySaver for managing conversation history and state. ```Python from langgraph.checkpoint.memory import MemorySaver from langchain_core.runnables import RunnableConfig prompt = ( "You are a smart agent. " "Use \`retriever\` tool to search on AI related documents and answer questions." "Use \`langchain-dev-docs\` tool to search on langchain / langgraph related documents and answer questions." "Answer in Korean." ) agent = create_react_agent( model, client.get_tools(), prompt=prompt, checkpointer=MemorySaver() ) ``` -------------------------------- ### Initializing Anthropic Claude Model (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Initializes the ChatAnthropic model with a specific model name, temperature, and max tokens. This model will be used as the language model for the agent. ```Python model = ChatAnthropic( model_name="claude-3-7-sonnet-latest", temperature=0, max_tokens=20000 ) ``` -------------------------------- ### Running Agent with Memory (Python) Source: https://github.com/teddynote-lab/langgraph-mcp-agents/blob/master/MCP-HandsOn-KOR.ipynb Executes the LangGraph agent asynchronously, leveraging the previously configured MemorySaver. It sends a message requesting the agent to summarize the previous conversation content in bullet points, demonstrating multi-turn capability. ```Python await astream_graph( agent, {"messages": "이전의 내용을 bullet point 로 요약해줘"}, config=config ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.