LangChain LangGraph
Module 1 Module 2 Module 3
Module 2 · Tools, State and Multi-Agent
1 2.1 MCP
2 2.1 Travel Agent
3 2.2 Runtime Context
4 2.2 State
5 2.3 Multi Agent
6 2.4 Wedding Planners
7 Bonus: RAG
8 Bonus: SQL
SUM Module Summary
HomeLangChainModule 2 · Tools, State and Multi-Agent2.1 MCP
📓 Notebook 📖 Explained
LANGCHAIN MODULE 2 · LESSON 2.1a

MCP: connecting external capabilities to an Agent
Tools · Resources · Prompts

This lesson uses MultiServerMCPClient to connect to a local server and a time server at the same time, then gives the tools, resources and prompt exposed by MCP to a LangChain Agent.

1 What problem does MCP solve?

The core value of MCP (Model Context Protocol) is to expose external system capabilities to model applications in a consistent way. For a LangChain Agent, an MCP server can provide three kinds of things:toolsresources and prompts

TypeMeaning in this lessonHow the Agent uses it
ToolsCallable actions, such as checking the time or querying an external systemAs create_agent(..., tools=tools) tool list
ResourcesReadable materials, such as text, files or configuration provided by the serverRead them first, then place them into the prompt or context
PromptsPrompt templates maintained by the serverUse after fetching as system_prompt
Key idea

MCP does not let the model access arbitrary systems directly. The server explicitly exposes the available capabilities, and the Agent only sees the registered tools, resources and prompts.

2 Initialize the environment and compatibility handling

The notebook first loads environment variables, then handles the event-loop and stderr issues that can appear when starting stdio subprocesses under Windows + Jupyter.

Python
from dotenv import load_dotenv
load_dotenv()

import os
import sys
import asyncio
from langchain.agents import create_agent
from langchain.messages import HumanMessage
from langchain_mcp_adapters.client import MultiServerMCPClient

if sys.platform == "win32":
    if not isinstance(asyncio.get_event_loop_policy(), asyncio.WindowsProactorEventLoopPolicy):
        asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

    if "ipykernel" in sys.modules:
        sys.stderr = sys.__stderr__
Why this compatibility code is needed

stdio MCP servers of this type rely on subprocess communication. Jupyter on Windows has unusual event-loop and stderr behavior, so the notebook handles compatibility first.

3 Configure MultiServerMCPClient

MultiServerMCPClient can manage multiple MCP servers at the same time. This lesson connects to two servers: a local Python server and another time server started through uvx .

Python
client = MultiServerMCPClient(
    {
        "local_server": {
            "transport": "stdio",
            "command": "python",
            "args": ["resources/2.1_mcp_server.py"],
        },
        "time": {
            "transport": "stdio",
            "command": "uvx",
            "args": ["mcp-server-time", "--local-timezone=Australia/Melbourne"],
        },
    }
)
local_server

The local script exposes course-specific tools, resources and prompt.

time

The third-party MCP server exposes time-query tools.

client

Fetch the capabilities together and give them to the Agent.

4 Get tools, resources and prompt

After connecting, the client can read the tools, resources and prompt exposed by the MCP server. The prompt here becomes the Agent system prompt.

Python
tools = await client.get_tools()
resources = await client.get_resources("local_server")
prompt = await client.get_prompt("local_server", "prompt")
prompt = prompt[0].content
Note

get_tools() By default it aggregates tools from all servers managed by the client; resources and prompts usually require a server name.

5 Create the Agent and call it asynchronously

After obtaining the MCP tools and prompt, creating the Agent is almost the same as using ordinary LangChain tools. The difference is that MCP tools often involve process or network I/O, so the notebook uses await agent.ainvoke(...)

Python
agent = create_agent(
    model=deepseek_model(),
    tools=tools,
    system_prompt=prompt,
)

question = HumanMessage(content="What time is it?")
response = await agent.ainvoke({"messages": [question]})

print(response["messages"][-1].content)
Call chain

After the user question enters the Agent, the model decides whether to call an MCP tool based on tool descriptions. After the MCP server returns a result, the model composes the final natural-language answer.

6 Minimal mental model for this lesson

1. Server exposes capabilities

The MCP server defines tools, resources and prompt. It can be a local script or a remote service.

2. Client fetches capabilities

MultiServerMCPClient Bring the capabilities from multiple servers into the current program.

3. Agent uses capabilities

create_agent It receives tools and a system prompt, and at runtime the model decides which tool to call.

When to use it

When tools come from external processes, third-party services, internal company systems, or need cross-language reuse, MCP is easier to standardize than writing Python tools directly.