{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Weather Agent with MCP Support (SSE 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 SSE: http://localhost:8000/sse\n", "INFO:mcp.client.sse:Connecting to SSE endpoint: http://localhost:8000/sse\n", "INFO:httpx:HTTP Request: GET http://localhost:8000/sse \"HTTP/1.1 200 OK\"\n", "INFO:mcp.client.sse:Received endpoint URL: http://localhost:8000/messages/?session_id=916bc6e1fb514b3e814e6a980ce20bbc\n", "INFO:mcp.client.sse:Starting post writer with endpoint URL: http://localhost:8000/messages/?session_id=916bc6e1fb514b3e814e6a980ce20bbc\n", "INFO:httpx:HTTP Request: POST http://localhost:8000/messages/?session_id=916bc6e1fb514b3e814e6a980ce20bbc \"HTTP/1.1 202 Accepted\"\n", "INFO:httpx:HTTP Request: POST http://localhost:8000/messages/?session_id=916bc6e1fb514b3e814e6a980ce20bbc \"HTTP/1.1 202 Accepted\"\n", "INFO:httpx:HTTP Request: POST http://localhost:8000/messages/?session_id=916bc6e1fb514b3e814e6a980ce20bbc \"HTTP/1.1 202 Accepted\"\n", "INFO:dapr_agents.tool.mcp.client:Loaded 2 tools from server 'local'\n", "INFO:httpx:HTTP Request: POST http://localhost:8000/messages/?session_id=916bc6e1fb514b3e814e6a980ce20bbc \"HTTP/1.1 202 Accepted\"\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", "await client.connect_sse(\n", " server_name=\"local\", # Unique name you assign to this server\n", " url=\"http://localhost:8000/sse\", # MCP SSE endpoint\n", " headers=None # Optional HTTP headers if needed\n", ")\n", "\n", "# See what tools were loaded\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": 4, "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": 5, "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", "INFO:mcp.client.sse:Connecting to SSE endpoint: http://localhost:8000/sse\n", "INFO:httpx:HTTP Request: GET http://localhost:8000/sse \"HTTP/1.1 200 OK\"\n", "INFO:mcp.client.sse:Received endpoint URL: http://localhost:8000/messages/?session_id=b47ef10b57dd471aac4c5d7aaeadbf5b\n", "INFO:mcp.client.sse:Starting post writer with endpoint URL: http://localhost:8000/messages/?session_id=b47ef10b57dd471aac4c5d7aaeadbf5b\n", "INFO:httpx:HTTP Request: POST http://localhost:8000/messages/?session_id=b47ef10b57dd471aac4c5d7aaeadbf5b \"HTTP/1.1 202 Accepted\"\n", "INFO:httpx:HTTP Request: POST http://localhost:8000/messages/?session_id=b47ef10b57dd471aac4c5d7aaeadbf5b \"HTTP/1.1 202 Accepted\"\n", "INFO:httpx:HTTP Request: POST http://localhost:8000/messages/?session_id=b47ef10b57dd471aac4c5d7aaeadbf5b \"HTTP/1.1 202 Accepted\"\n", "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;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_lBVZIV7seOsWttLnfZaLSwS3)\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", "\u001b[38;2;191;69;126mLocalGetWeather(tool) (Id: call_lBVZIV7seOsWttLnfZaLSwS3):\u001b[0m\n", "\u001b[38;2;191;69;126m\u001b[0m\u001b[38;2;191;69;126mNew York: 65F.\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 weather in New York is 65°F. If you need more information, feel free to ask!\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 weather in New York is 65°F. If you need more information, feel free to ask!'" ] }, "execution_count": 5, "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 }