Build a Runnable MCP Loop in Python (stdio streamable-http LLM tool choice)
Build a Runnable MCP Loop in Python (stdio → streamable-http → LLM tool choice) Attributed Chinese → English compile Source: [MCP][02]快速入门MCP开发 Original author: 花酒锄作田 (Cnblogs) · Posted: 2025-09-15 This is an English rewrite of the original tutorial’s ideas and code patterns. It is not original work by the compiler. Always link the Chinese source; do not present this compile as the original. Many…
Build a Runnable MCP Loop in Python (stdio → streamable-http → LLM tool choice)
The tutorial demonstrates building a Runnable MCP (Microservice Coordination Protocol) loop in Python, using stdio as the transport mechanism. The guide emphasizes the importance of establishing a reliable discover → bind → call → feedback loop before deciding on the LLM (Large Language Model) that will be used.
The environment for this tutorial requires Python 3.13.5 or higher, with a preference for uv or pip installation. The author uses the mcp package, which provides FastMCP v1, with the community FastMCP having moved to v2. It recommends trying both versions while learning.
The tutorial begins with a minimal FastMCP server example that supports prompts, resources, and tools using stdio transport. The code includes type hints, return types, and docstrings, which serve as the model-facing tool descriptions later.
Step 1 focuses on creating a minimal FastMCP server with stdio transport. It defines a greet_user function that greets a user with a specified style, a greeting_resource function that returns a simple greeting, a get_config function that returns static configuration data, an async get_date function that returns the current date, and an async get_weather function that returns weather information for a given city. The server is run with stdio transport using the mcp.run() function.
Step 2 introduces the StdioClient with ClientSession. The client launches the server as a subprocess via StdioServerParameters, specifying the command, args, and cwd. The run() function creates an async connection using stdio_client and ClientSession, initializes the session, lists available prompts and tools, reads a resource, and calls a tool (add) with arguments.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
