AI Hints
Home›MCP Tutorial›Client Callbacks and Practice
MCP · Lesson 63 of 120

Practice: A Chat Client

Ask the shop.

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

Let's put the client side together. A tiny chat client that keeps the conversation, and answers several customer messages with an agent over the Northwind server.

Practice: A Chat Client
From the video: Practice: A Chat Client
  • Keep the conversation
  • Agent over MCP
  • Several messages

A tiny chat client

History holds the whole conversation. For each customer message, we add it, run the agent, and keep its messages. Run it. The second question is a follow up, and the agent understands it from the history.

chat_client.pypython
import asyncio
from mcp import Client
from langchain.agents import create_agent
from mcp_tools import load_tools
from northwind import mcp

async def main():
    async with Client(mcp) as client:
        agent = create_agent("google_genai:gemini-flash-lite-latest", tools=await load_tools(client),
                             system_prompt="You are Northwind's support assistant. One short sentence.")
        history = []
        for text in ["What's the status of order 1044?", "And order 1043?"]:
            history.append({"role": "user", "content": text})
            history = (await agent.ainvoke({"messages": history}))["messages"]
            print("customer:", text, "\nassistant:", history[-1].text)

asyncio.run(main())
Output
customer: What's the status of order 1044?
assistant: Order 1044 has been delivered.
customer: And order 1043?
assistant: Order 1043 is currently packing.
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

A chat client keeps the history, and runs the agent for each message. Next, the clients quiz.

Key takeaways
  • History
  • Agent per message
  • Follow-ups work
Next up: Clients Quiz — test yourself