From d79776cba6cd73a2bae73a0fe0e44289eb1f540d Mon Sep 17 00:00:00 2001 From: RPBot Date: Wed, 16 Sep 2026 15:30:33 +0000 Subject: [PATCH] MCP vs API: rename folder to match slug and sync code with the article Consolidates the two open PRs on this folder into one. - Renames mcp-vs-api-calls/ to mcp-vs-api/ to match the tutorial slug (supersedes #819, minus its stray root-level uv.lock). - Syncs client.py and client_api.py with the tech-review changes in tutorial-drafts#1184: collect every tool_use block and append the assistant turn once, select the text block by type rather than position, and raise max_tokens to 2048 (supersedes #832). - Repairs the leftover fragments in #832: client_api.py did not parse, and client.py ran its __main__ block twice. - Points the README at the correct tutorial URL and the pinned install command. Code verified line-for-line against the article. ruff check and ruff format pass. Co-Authored-By: Claude Opus 5 (1M context) --- {mcp-vs-api-calls => mcp-vs-api}/README.md | 4 +-- {mcp-vs-api-calls => mcp-vs-api}/client.py | 31 +++++++++---------- .../client_api.py | 28 +++++++---------- {mcp-vs-api-calls => mcp-vs-api}/server.py | 0 {mcp-vs-api-calls => mcp-vs-api}/tool.py | 0 5 files changed, 28 insertions(+), 35 deletions(-) rename {mcp-vs-api-calls => mcp-vs-api}/README.md (93%) rename {mcp-vs-api-calls => mcp-vs-api}/client.py (73%) rename {mcp-vs-api-calls => mcp-vs-api}/client_api.py (74%) rename {mcp-vs-api-calls => mcp-vs-api}/server.py (100%) rename {mcp-vs-api-calls => mcp-vs-api}/tool.py (100%) diff --git a/mcp-vs-api-calls/README.md b/mcp-vs-api/README.md similarity index 93% rename from mcp-vs-api-calls/README.md rename to mcp-vs-api/README.md index 7fc000e502..d4f61a37a3 100644 --- a/mcp-vs-api-calls/README.md +++ b/mcp-vs-api/README.md @@ -1,6 +1,6 @@ # MCP vs API Calls: Which Should You Use for Python LLM Apps? -This folder contains the sample code for the Real Python tutorial [MCP vs API Calls: Which Should You Use for Python LLM Apps?](https://realpython.com/mcp-vs-api-calls/). +This folder contains the sample code for the Real Python tutorial [MCP vs API Calls: Which Should You Use for Python LLM Apps?](https://realpython.com/mcp-vs-api/). ## Files @@ -16,7 +16,7 @@ Create a virtual environment and install the dependencies: ```console $ python -m venv tools-venv $ source tools-venv/bin/activate -(tools-venv) $ python -m pip install anthropic "mcp>=2,<3" +(tools-venv) $ python -m pip install "anthropic>=1,<2" "mcp>=2,<3" ``` The examples use the Anthropic client, which requires an API key. Create a key in the [Anthropic Console](https://console.anthropic.com/), scope it to a single workspace, and export it: diff --git a/mcp-vs-api-calls/client.py b/mcp-vs-api/client.py similarity index 73% rename from mcp-vs-api-calls/client.py rename to mcp-vs-api/client.py index 0240e5e1d2..0d1fd61bb1 100644 --- a/mcp-vs-api-calls/client.py +++ b/mcp-vs-api/client.py @@ -32,7 +32,7 @@ def ask_model( ) -> Message: return client.messages.create( model=model, - max_tokens=1024, + max_tokens=2048, tools=tools, messages=messages, ) @@ -45,14 +45,9 @@ async def run_tool(client: Client, block: ToolUseBlock) -> str: def tool_result(block: ToolUseBlock, output: str) -> dict: return { - "role": "user", - "content": [ - { - "type": "tool_result", - "tool_use_id": block.id, - "content": output, - }, - ], + "type": "tool_result", + "tool_use_id": block.id, + "content": output, } @@ -63,15 +58,17 @@ async def main(server: StdioServerParameters) -> None: client = anthropic.Anthropic() messages = [{"role": "user", "content": QUESTION}] response = ask_model(client, messages, tools) - for block in response.content: - if block.type == "tool_use": - messages.append( - {"role": "assistant", "content": response.content} - ) - output = await run_tool(mcp, block) - messages.append(tool_result(block, output)) + + tool_uses = [b for b in response.content if b.type == "tool_use"] + if tool_uses: + messages.append({"role": "assistant", "content": response.content}) + results = [ + tool_result(b, await run_tool(mcp, b)) for b in tool_uses + ] + messages.append({"role": "user", "content": results}) + final = ask_model(client, messages, tools) - print(final.content[0].text) + print(next(b.text for b in final.content if b.type == "text")) if __name__ == "__main__": diff --git a/mcp-vs-api-calls/client_api.py b/mcp-vs-api/client_api.py similarity index 74% rename from mcp-vs-api-calls/client_api.py rename to mcp-vs-api/client_api.py index 069b84a472..ffbe333e28 100644 --- a/mcp-vs-api-calls/client_api.py +++ b/mcp-vs-api/client_api.py @@ -40,7 +40,7 @@ def ask_model( ) -> Message: return client.messages.create( model=model, - max_tokens=1024, + max_tokens=2048, tools=tools, messages=messages, ) @@ -53,14 +53,9 @@ def run_tool(block: ToolUseBlock) -> str: def tool_result(block: ToolUseBlock, output: str) -> dict: return { - "role": "user", - "content": [ - { - "type": "tool_result", - "tool_use_id": block.id, - "content": output, - }, - ], + "type": "tool_result", + "tool_use_id": block.id, + "content": output, } @@ -68,14 +63,15 @@ def main() -> None: client = anthropic.Anthropic() messages = [{"role": "user", "content": QUESTION}] response = ask_model(client, messages, TOOLS) - for block in response.content: - if block.type == "tool_use": - messages.append( - {"role": "assistant", "content": response.content}, - ) - messages.append(tool_result(block, run_tool(block))) + + tool_uses = [b for b in response.content if b.type == "tool_use"] + if tool_uses: + messages.append({"role": "assistant", "content": response.content}) + results = [tool_result(b, run_tool(b)) for b in tool_uses] + messages.append({"role": "user", "content": results}) + final = ask_model(client, messages, TOOLS) - print(final.content[0].text) + print(next(b.text for b in final.content if b.type == "text")) if __name__ == "__main__": diff --git a/mcp-vs-api-calls/server.py b/mcp-vs-api/server.py similarity index 100% rename from mcp-vs-api-calls/server.py rename to mcp-vs-api/server.py diff --git a/mcp-vs-api-calls/tool.py b/mcp-vs-api/tool.py similarity index 100% rename from mcp-vs-api-calls/tool.py rename to mcp-vs-api/tool.py