{ "cells": [ { "cell_type": "markdown", "id": "12bbf854", "metadata": {}, "source": [] }, { "cell_type": "markdown", "id": "23555bd4", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 1, "id": "c645eaed", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dotenv import load_dotenv\n", "from agents import Agent, Runner, trace\n", "from agents.mcp import MCPServerStdio\n", "from IPython.display import display, Markdown\n", "\n", "load_dotenv(override=True)" ] }, { "cell_type": "code", "execution_count": 4, "id": "487a4825", "metadata": {}, "outputs": [], "source": [ "import functools\n", "import subprocess\n", "import agents.mcp.server\n", "\n", "agents.mcp.server.stdio_client = functools.partial(agents.mcp.server.stdio_client, errlog=subprocess.DEVNULL)" ] }, { "cell_type": "code", "execution_count": 6, "id": "46a09b17", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting massive\n", " Downloading massive-2.8.0-py3-none-any.whl.metadata (974 bytes)\n", "Requirement already satisfied: certifi<2027.0.0,>=2022.5.18 in c:\\Users\\Mohan Raj P\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages (from massive) (2026.5.20)\n", "Requirement already satisfied: urllib3>=1.26.9 in c:\\Users\\Mohan Raj P\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages (from massive) (2.6.3)\n", "Requirement already satisfied: websockets>=14.0 in c:\\Users\\Mohan Raj P\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages (from massive) (15.0.1)\n", "Downloading massive-2.8.0-py3-none-any.whl (68 kB)\n", "Installing collected packages: massive\n", "Successfully installed massive-2.8.0\n", "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n", "[notice] A new release of pip is available: 26.1 -> 26.1.2\n", "[notice] To update, run: python.exe -m pip install --upgrade pip\n" ] } ], "source": [ "%pip install massive\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "a80b242c", "metadata": {}, "outputs": [], "source": [ "from backend.accounts import Account" ] }, { "cell_type": "code", "execution_count": 8, "id": "c49b604e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Account(name='mohan', balance=10000.0, strategy='', holdings={}, transactions=[], portfolio_value_time_series=[])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "account = Account.get(\"Mohan\")\n", "account.reset()\n", "account" ] }, { "cell_type": "code", "execution_count": 9, "id": "25e877de", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Completed. Latest details:\\n{\"name\": \"mohan\", \"balance\": 9120.4444, \"strategy\": \"\", \"holdings\": {\"AMZN\": 3}, \"transactions\": [{\"symbol\": \"AMZN\", \"quantity\": 3, \"price\": 293.1852, \"timestamp\": \"2026-07-24 16:56:54\", \"rationale\": \"Because this bookstore website looks promising\"}], \"portfolio_value_time_series\": [[\"2026-07-24 16:56:54\", 9998.2444]], \"total_portfolio_value\": 9998.2444, \"total_profit_loss\": -1.7556000000004133}'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "account.buy_shares(\"AMZN\", 3, \"Because this bookstore website looks promising\")" ] }, { "cell_type": "code", "execution_count": 10, "id": "0cd24850", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'{\"name\": \"mohan\", \"balance\": 9120.4444, \"strategy\": \"\", \"holdings\": {\"AMZN\": 3}, \"transactions\": [{\"symbol\": \"AMZN\", \"quantity\": 3, \"price\": 293.1852, \"timestamp\": \"2026-07-24 16:56:54\", \"rationale\": \"Because this bookstore website looks promising\"}], \"portfolio_value_time_series\": [[\"2026-07-24 16:56:54\", 9998.2444], [\"2026-07-24 16:57:24\", 9998.3044]], \"total_portfolio_value\": 9998.3044, \"total_profit_loss\": -1.6955999999991036}'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "account.report()" ] }, { "cell_type": "code", "execution_count": 12, "id": "7ce77f58", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'symbol': 'AMZN',\n", " 'quantity': 3,\n", " 'price': 293.1852,\n", " 'timestamp': '2026-07-24 16:56:54',\n", " 'rationale': 'Because this bookstore website looks promising'}]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "account.list_transactions()" ] }, { "cell_type": "code", "execution_count": 15, "id": "163f1e8e", "metadata": {}, "outputs": [], "source": [ "import os\n", "project_dir = os.path.abspath(os.path.join(os.getcwd()))\n", "params = {\"command\": \"uv\", \"args\": [\"run\", \"-m\", \"backend.accounts_server\"], \"cwd\": project_dir}\n", "async with MCPServerStdio(params=params, client_session_timeout_seconds=30) as server:\n", " mcp_tools = await server.list_tools()\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "69471b9c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Tool(name='get_balance', title=None, description='Get the cash balance of the given account name.\\n\\nArgs:\\n name: The name of the account holder\\n', inputSchema={'properties': {'name': {'title': 'Name', 'type': 'string'}}, 'required': ['name'], 'title': 'get_balanceArguments', 'type': 'object'}, outputSchema={'properties': {'result': {'title': 'Result', 'type': 'number'}}, 'required': ['result'], 'title': 'get_balanceOutput', 'type': 'object'}, icons=None, annotations=None, meta=None, execution=None),\n", " Tool(name='get_holdings', title=None, description='Get the holdings of the given account name.\\n\\nArgs:\\n name: The name of the account holder\\n', inputSchema={'properties': {'name': {'title': 'Name', 'type': 'string'}}, 'required': ['name'], 'title': 'get_holdingsArguments', 'type': 'object'}, outputSchema={'additionalProperties': {'type': 'integer'}, 'title': 'get_holdingsDictOutput', 'type': 'object'}, icons=None, annotations=None, meta=None, execution=None),\n", " Tool(name='buy_shares', title=None, description=\"Buy shares of a stock.\\n\\nArgs:\\n name: The name of the account holder\\n symbol: The symbol of the stock\\n quantity: The quantity of shares to buy\\n rationale: The rationale for the purchase and fit with the account's strategy\\n\", inputSchema={'properties': {'name': {'title': 'Name', 'type': 'string'}, 'symbol': {'title': 'Symbol', 'type': 'string'}, 'quantity': {'title': 'Quantity', 'type': 'integer'}, 'rationale': {'title': 'Rationale', 'type': 'string'}}, 'required': ['name', 'symbol', 'quantity', 'rationale'], 'title': 'buy_sharesArguments', 'type': 'object'}, outputSchema={'properties': {'result': {'title': 'Result', 'type': 'number'}}, 'required': ['result'], 'title': 'buy_sharesOutput', 'type': 'object'}, icons=None, annotations=None, meta=None, execution=None),\n", " Tool(name='sell_shares', title=None, description=\"Sell shares of a stock.\\n\\nArgs:\\n name: The name of the account holder\\n symbol: The symbol of the stock\\n quantity: The quantity of shares to sell\\n rationale: The rationale for the sale and fit with the account's strategy\\n\", inputSchema={'properties': {'name': {'title': 'Name', 'type': 'string'}, 'symbol': {'title': 'Symbol', 'type': 'string'}, 'quantity': {'title': 'Quantity', 'type': 'integer'}, 'rationale': {'title': 'Rationale', 'type': 'string'}}, 'required': ['name', 'symbol', 'quantity', 'rationale'], 'title': 'sell_sharesArguments', 'type': 'object'}, outputSchema={'properties': {'result': {'title': 'Result', 'type': 'number'}}, 'required': ['result'], 'title': 'sell_sharesOutput', 'type': 'object'}, icons=None, annotations=None, meta=None, execution=None),\n", " Tool(name='change_strategy', title=None, description='At your discretion, if you choose to, call this to change your investment strategy for the future.\\n\\nArgs:\\n name: The name of the account holder\\n strategy: The new strategy for the account\\n', inputSchema={'properties': {'name': {'title': 'Name', 'type': 'string'}, 'strategy': {'title': 'Strategy', 'type': 'string'}}, 'required': ['name', 'strategy'], 'title': 'change_strategyArguments', 'type': 'object'}, outputSchema={'properties': {'result': {'title': 'Result', 'type': 'string'}}, 'required': ['result'], 'title': 'change_strategyOutput', 'type': 'object'}, icons=None, annotations=None, meta=None, execution=None)]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mcp_tools" ] }, { "cell_type": "code", "execution_count": 17, "id": "650f3b69", "metadata": {}, "outputs": [], "source": [ "instructions = \"You are able to manage an account for a client, and answer questions about the account.\"\n", "request = \"My name is Mohan and my account is under the name raj. What's my balance and my holdings?\"\n", "model = \"gpt-4o-mini\"" ] }, { "cell_type": "code", "execution_count": 18, "id": "ecb1e9a8", "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Your current balance is **$10,000**. \n", "\n", "You don't have any holdings in your account at the moment. If you need assistance with anything else, feel free to ask!" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\n", "async with MCPServerStdio(params=params, client_session_timeout_seconds=30) as mcp_server:\n", " agent = Agent(name=\"account_manager\", instructions=instructions, model=model, mcp_servers=[mcp_server])\n", " with trace(\"account_manager\"):\n", " result = await Runner.run(agent, request)\n", " display(Markdown(result.final_output))\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0bd3618a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "e1ad4b88", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b51a3e02", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "9fc4bc54", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "cfbba53f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "cf0f4e1f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "bc78871c", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }