Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mcp-vs-api-calls/README.md → mcp-vs-api/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand Down
31 changes: 14 additions & 17 deletions mcp-vs-api-calls/client.py → mcp-vs-api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def ask_model(
) -> Message:
return client.messages.create(
model=model,
max_tokens=1024,
max_tokens=2048,
tools=tools,
messages=messages,
)
Expand All @@ -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,
}


Expand All @@ -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__":
Expand Down
28 changes: 12 additions & 16 deletions mcp-vs-api-calls/client_api.py → mcp-vs-api/client_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def ask_model(
) -> Message:
return client.messages.create(
model=model,
max_tokens=1024,
max_tokens=2048,
tools=tools,
messages=messages,
)
Expand All @@ -53,29 +53,25 @@ 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,
}


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__":
Expand Down
File renamed without changes.
File renamed without changes.
Loading