AI Hints
Home›MCP Tutorial›Your First Server
MCP · Lesson 9 of 120

Your First Client

Call it in-process.

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

To test a server, we don't need an AI app. The SDK's Client class can connect straight to a server object, inside the same Python program. It's perfect for learning, and for tests.

Your First Client
From the video: Your First Client
  • No AI app needed
  • Client class
  • Connect in-process

Calling the tool

We import the server object. Client opens a connection to it. Call tool sends the tool's name and arguments. The result's content holds the text. Run it. Sixty pounds gets free delivery. Twenty pounds costs four ninety nine.

client.pypython
import asyncio
from mcp import Client
from northwind import mcp

async def main():
    async with Client(mcp) as client:
        for total in [60, 20]:
            result = await client.call_tool("delivery_cost", {"order_total": total})
            print(total, "->", result.content[0].text)

asyncio.run(main())
Output
60 -> free
20 -> £4.99
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 sitecustomize.py
import logging, warnings
logging.disable(logging.INFO)
warnings.filterwarnings("ignore")

Async code

The client is async, because talking to a server means waiting. Async with opens and closes the connection. Await waits for each reply. And asyncio dot run starts it all.

keyvalue
async withopen and close
awaitwait for replies
asyncio.runstart
In-processgreat for tests

Summary

Client connects to a server, and call tool runs a tool with arguments. Next, what the client sees.

Key takeaways
  • Client(mcp)
  • call_tool
  • result.content
Next up: What the Client Sees — the tool schema