Start learning free

LESSON 55 OF 129 · AI ENGINEERING: MCP

Serve It over HTTP in Python

Practice

Until now the client started the MCP server as a program on the same computer and talked to it directly. This is called stdio, and it only works on one machine. To let an assistant on another computer use the shop's tools, the server must answer over HTTP, like your Flask apps do.

The tools don't change. The server's streamable_http_app() method gives you a web app that speaks MCP, and a web server program named uvicorn listens on a host and a port and hands each request to that app.

Running in the background: a web server's run() never returns, because it keeps listening. In this lesson the same file also plays the client, so the server runs in a thread. A thread gets a function as its target, without parentheses:

def ring():
    print("ring")

threading.Thread(target=ring, daemon=True).start()
print("the main program goes on")

Over HTTP the client gets a URL instead of a command: the host, the port and the path /mcp. A server needs a moment before it listens, so the program waits until server.started is true before the client connects.

Your program lists the tools and calls one. Then the check looks at the server's side, to see that the requests really arrived over HTTP:

Tool: lookup_order
Order 4: {
  "id": 4,
  "ordered": "2026-04-18",
  "status": "shipped"
}
Server listened on: 127.0.0.1:8010
The client's requests arrived over HTTP (POST /mcp): yes
Server stopped at the end: yes

Your task: the server and its lookup_order tool are already written. Get the web app from the server, serve it with uvicorn on 127.0.0.1, port 8010, in a background thread, and wait until it has started. Then connect the client to http://127.0.0.1:8010/mcp and call lookup_order for order 4.


Assignment
Follow the Coding Tutorial to serve the shop's MCP server over HTTP and call its tool through a URL.


Hint
Give the thread server.run without parentheses: with them the call would start right there and never return. The client's URL ends in /mcp.


STEP 1 OF 9
    Ctrl + Enter to check

    Ai Mcp Http in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free