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