镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Merged
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
14 changes: 7 additions & 7 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 @@ -14,30 +14,30 @@ This folder contains the sample code for the Real Python tutorial [MCP vs API Ca
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"
$ python -m venv .venv
$ source .venv/bin/activate
(.venv) $ python -m pip install anthropic==1.4.0 mcp==2.1.1
```

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:

```console
(tools-venv) $ export ANTHROPIC_API_KEY="your-api-key-here"
(.venv) $ export ANTHROPIC_API_KEY="your-api-key-here"
```

## Running the examples

Run the direct API calls version:

```console
(tools-venv) $ python client_api.py
(.venv) $ python client_api.py
The latest version of the Jinja2 package is 3.1.6.
```

Run the MCP version:

```console
(tools-venv) $ python client.py
(.venv) $ python client.py
The latest version of the Jinja2 package is 3.1.6.
```

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