AI Hints
Home›MCP Tutorial›Streamable HTTP
MCP · Lesson 68 of 120

Connecting by URL

The HTTP client.

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

A client connects to an HTTP server by passing its URL. Everything else, listing and calling tools, works exactly the same as before.

Connecting by URL
From the video: Connecting by URL
  • Pass a URL
  • Same methods
  • Same results

serve.py

Normally you'd start the server in its own terminal. For our demos, this small helper runs a web app in the background, using uvicorn.

serve.pypython
import threading, time, uvicorn

def start(app, port=8765):
    """Run a web app in the background, just for demos."""
    threading.Thread(target=uvicorn.run, args=(app,), daemon=True,
                     kwargs={"port": port, "log_level": "critical"}).start()
    time.sleep(1)

A client by URL

We start the Northwind HTTP app in the background. Then the client gets a URL instead of a server object. Run it. The same tools, and the same answer, now over HTTP.

by_url.pypython
import asyncio
from mcp import Client
from northwind import mcp
from serve import start

start(mcp.streamable_http_app())

async def main():
    async with Client("http://127.0.0.1:8765/mcp") as client:
        print("tools:", [t.name for t in (await client.list_tools()).tools])
        r = await client.call_tool("order_status", {"order_id": 1042})
        print("1042:", r.content[0].text)

asyncio.run(main())
Output
tools: ['order_status', 'delivery_cost']
1042: shipped
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 serve.py
import threading, time, uvicorn

def start(app, port=8765):
    """Run a web app in the background, just for demos."""
    threading.Thread(target=uvicorn.run, args=(app,), daemon=True,
                     kwargs={"port": port, "log_level": "critical"}).start()
    time.sleep(1)

stdio or HTTP

Use standard IO for local tools, started by the host, one user at a time. Use HTTP for shared servers, in the cloud, with many users and proper logins.

stdio

  • LocalStarted by the hostOne user

HTTP

  • Remote or sharedRuns on its ownMany users, with auth

Summary

Clients connect to HTTP servers by URL, with the same methods as before. Next, mounting MCP inside FastAPI.

Key takeaways
  • Client(url)
  • Same methods
  • stdio vs HTTP
Next up: Mounting in FastAPI — one app