mirror of https://github.com/dapr/dapr-agents.git
297 lines
8.0 KiB
Plaintext
297 lines
8.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Basic Weather Agent with MCP Support (Stdio Transport)\n",
|
|
"\n",
|
|
"* Collaborator: Roberto Rodriguez @Cyb3rWard0g"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Install Required Libraries\n",
|
|
"Before starting, ensure the required libraries are installed:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install dapr-agents python-dotenv mcp starlette"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Import Environment Variables"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"True"
|
|
]
|
|
},
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from dotenv import load_dotenv\n",
|
|
"load_dotenv() # take environment variables from .env."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Enable Logging"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import logging\n",
|
|
"\n",
|
|
"logging.basicConfig(level=logging.INFO)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Connect to MCP Server and Get Tools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:dapr_agents.tool.mcp.client:Connecting to MCP server 'local' via stdio: python ['server.py', '--server_type', 'stdio']\n",
|
|
"INFO:dapr_agents.tool.mcp.client:Loaded 2 tools from server 'local'\n",
|
|
"INFO:dapr_agents.tool.mcp.client:Loaded 0 prompts from server 'local': \n",
|
|
"INFO:dapr_agents.tool.mcp.client:Successfully connected to MCP server 'local'\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"🔧 Tools: ['LocalGetWeather', 'LocalJump']\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from dapr_agents.tool.mcp.client import MCPClient\n",
|
|
"\n",
|
|
"client = MCPClient()\n",
|
|
"\n",
|
|
"# Connect to your test server\n",
|
|
"await client.connect_stdio(\n",
|
|
" server_name=\"local\",\n",
|
|
" command=\"python\",\n",
|
|
" args=[\"server.py\", \"--server_type\", \"stdio\"]\n",
|
|
")\n",
|
|
"\n",
|
|
"# Test tools\n",
|
|
"tools = client.get_all_tools()\n",
|
|
"print(\"🔧 Tools:\", [t.name for t in tools])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Initialize Agent"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:dapr_agents.llm.openai.client.base:Initializing OpenAI client...\n",
|
|
"INFO:dapr_agents.tool.executor:Tool registered: LocalGetWeather\n",
|
|
"INFO:dapr_agents.tool.executor:Tool registered: LocalJump\n",
|
|
"INFO:dapr_agents.tool.executor:Tool Executor initialized with 2 tool(s).\n",
|
|
"INFO:dapr_agents.agent.base:Constructing system_prompt from agent attributes.\n",
|
|
"INFO:dapr_agents.agent.base:Using system_prompt to create the prompt template.\n",
|
|
"INFO:dapr_agents.agent.base:Pre-filled prompt template with attributes: ['name', 'role', 'goal']\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from dapr_agents import Agent\n",
|
|
"\n",
|
|
"agent = Agent(\n",
|
|
" name=\"Rob\",\n",
|
|
" role= \"Weather Assistant\",\n",
|
|
" tools=tools\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Run Agent"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:dapr_agents.agent.patterns.toolcall.base:Iteration 1/10 started.\n",
|
|
"INFO:dapr_agents.llm.utils.request:Tools are available in the request.\n",
|
|
"INFO:dapr_agents.llm.openai.chat:Invoking ChatCompletion API.\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\u001b[38;2;242;182;128muser:\u001b[0m\n",
|
|
"\u001b[38;2;242;182;128m\u001b[0m\u001b[38;2;242;182;128mWhat is the weather in New York?\u001b[0m\u001b[0m\n",
|
|
"\u001b[0m\u001b[0m\n",
|
|
"\u001b[0m--------------------------------------------------------------------------------\u001b[0m\n",
|
|
"\u001b[0m\u001b[0m\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
|
|
"INFO:dapr_agents.llm.openai.chat:Chat completion retrieved successfully.\n",
|
|
"INFO:dapr_agents.agent.patterns.toolcall.base:Executing LocalGetWeather with arguments {\"location\":\"New York\"}\n",
|
|
"INFO:dapr_agents.tool.executor:Running tool (auto): LocalGetWeather\n",
|
|
"INFO:dapr_agents.tool.mcp.client:[MCP] Executing tool 'get_weather' with args: {'location': 'New York'}\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\u001b[38;2;217;95;118massistant:\u001b[0m\n",
|
|
"\u001b[38;2;217;95;118m\u001b[0m\u001b[38;2;217;95;118mFunction name: LocalGetWeather (Call Id: call_l8KuS39PvriksogjGN71rzCm)\u001b[0m\n",
|
|
"\u001b[38;2;217;95;118m\u001b[0m\u001b[38;2;217;95;118mArguments: {\"location\":\"New York\"}\u001b[0m\u001b[0m\n",
|
|
"\u001b[0m\u001b[0m\n",
|
|
"\u001b[0m--------------------------------------------------------------------------------\u001b[0m\n",
|
|
"\u001b[0m\u001b[0m\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:dapr_agents.agent.patterns.toolcall.base:Iteration 2/10 started.\n",
|
|
"INFO:dapr_agents.llm.utils.request:Tools are available in the request.\n",
|
|
"INFO:dapr_agents.llm.openai.chat:Invoking ChatCompletion API.\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\u001b[38;2;191;69;126mLocalGetWeather(tool) (Id: call_l8KuS39PvriksogjGN71rzCm):\u001b[0m\n",
|
|
"\u001b[38;2;191;69;126m\u001b[0m\u001b[38;2;191;69;126mNew York: 60F.\u001b[0m\u001b[0m\n",
|
|
"\u001b[0m\u001b[0m\n",
|
|
"\u001b[0m--------------------------------------------------------------------------------\u001b[0m\n",
|
|
"\u001b[0m\u001b[0m\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
|
|
"INFO:dapr_agents.llm.openai.chat:Chat completion retrieved successfully.\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\u001b[38;2;147;191;183massistant:\u001b[0m\n",
|
|
"\u001b[38;2;147;191;183m\u001b[0m\u001b[38;2;147;191;183mThe current temperature in New York is 60°F.\u001b[0m\u001b[0m\n",
|
|
"\u001b[0m\u001b[0m\n",
|
|
"\u001b[0m--------------------------------------------------------------------------------\u001b[0m\n",
|
|
"\u001b[0m\u001b[0m\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'The current temperature in New York is 60°F.'"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"await agent.run(\"What is the weather in New York?\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": ".venv",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.13.1"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|