AI Hints
Home›MCP Tutorial›Discovery and stdio
MCP · Lesson 11 of 120

Running over stdio

A real subprocess.

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

In real use, the host starts your server as a separate program, and talks to it through standard input and output. The Client class can do that too, with stdio server parameters.

Running over stdio
From the video: Running over stdio
  • Host starts the server
  • A separate program
  • stdin and stdout

Starting the server

Stdio server parameters says which command to run. Here, Python with our server file. Client starts the program, connects, and calls a tool. Run it. The answer comes back from a separate process.

stdio_client.pypython
import asyncio, sys
from mcp import Client, StdioServerParameters

server = StdioServerParameters(command=sys.executable, args=["northwind.py"])

async def main():
    async with Client(server) as client:
        result = await client.call_tool("order_status", {"order_id": 1042})
        print("order 1042:", result.content[0].text)

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

What happens

The client starts the server program. They exchange a first message, agreeing on the protocol version. The client sends requests on the server's input. The server replies on its output. And when the client closes, the server stops.

  1. Start
  2. Agree version
  3. Requests in
  4. Replies out
  5. Stop
What happens
From the video: What happens

Summary

With standard IO, the client starts the server as a subprocess, and talks through its input and output. Next, the foundations quiz.

Key takeaways
  • StdioServerParameters
  • Subprocess
  • stdin/stdout
Next up: Foundations Quiz — test yourself