AI Hints
Home›MCP Tutorial›Agents over MCP
MCP · Lesson 58 of 120

MCP with LangChain

StructuredTool.

⏱ 2 min read▶ Video at 51:45🎓 Free
Watch this lesson in the MCP courseWatch on YouTube ↗Full MCP course

LangChain agents expect LangChain tools. We can wrap each MCP tool as a structured tool. Its schema comes straight from MCP, and calling it calls the server.

MCP with LangChain
From the video: MCP with LangChain
  • LangChain tools
  • Wrap each MCP tool
  • Calls go to the server

mcp_tools.py

To langchain takes a client and an MCP tool. The inner function calls the tool through the client, and returns its text. Structured tool uses the MCP name, description, and input schema. Load tools wraps every tool on a server.

mcp_tools.pypython
from langchain_core.tools import StructuredTool

def to_langchain(client, t):
    async def call(**args):
        result = await client.call_tool(t.name, args)
        return result.content[0].text
    return StructuredTool(name=t.name, description=t.description,
                          args_schema=t.input_schema, coroutine=call)

async def load_tools(client):
    listed = await client.list_tools()
    return [to_langchain(client, t) for t in listed.tools]

Trying the wrapper

We wrap the Northwind tools, and call one directly, as LangChain would. Run it. The LangChain tool returns the answer from the MCP server.

try_wrapper.pypython
import asyncio
from mcp import Client
from mcp_tools import load_tools
from northwind import mcp

async def main():
    async with Client(mcp) as client:
        tools = await load_tools(client)
        print([t.name for t in tools])
        print(await tools[0].ainvoke({"order_id": 1044}))

asyncio.run(main())
Output
['order_status', 'delivery_cost']
delivered
Also uses northwind.py
from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.exceptions import ToolError

mcp = MCPServer("northwind")
ORDERS = {1042: "shipped", 1043: "packing", 1044: "delivered"}

@mcp.tool()
def order_status(order_id: int) -> str:
    """Look up the status of a Northwind order by its number."""
    if order_id not in ORDERS:
        raise ToolError(f"Order {order_id} not found")
    return ORDERS[order_id]

@mcp.tool()
def delivery_cost(order_total: float) -> str:
    """UK delivery cost for an order total in pounds."""
    return "free" if order_total >= 50 else "£4.99"

@mcp.resource("policy://returns")
def returns_policy() -> str:
    """Northwind's returns policy."""
    return "You can return any unused item within 30 days of delivery."

@mcp.prompt()
def support_reply(question: str) -> str:
    """Draft a polite reply to a customer."""
    return f"Write a short, polite reply to this Northwind customer: {question}"

if __name__ == "__main__":
    mcp.run()
Also uses mcp_tools.py
from langchain_core.tools import StructuredTool

def to_langchain(client, t):
    async def call(**args):
        result = await client.call_tool(t.name, args)
        return result.content[0].text
    return StructuredTool(name=t.name, description=t.description,
                          args_schema=t.input_schema, coroutine=call)

async def load_tools(client):
    listed = await client.list_tools()
    return [to_langchain(client, t) for t in listed.tools]

Summary

Wrap MCP tools as LangChain structured tools, with the schema taken from MCP. Next, a full agent.

Key takeaways
  • StructuredTool
  • input_schema
  • coroutine
Next up: A Gemini Agent — over MCP