{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e6b78340-5c44-428c-82db-0aecd6ca7e96", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from mcp101.business_tools import (\n", " calculate_commission,\n", " calculate_total_premium,\n", " filter_policies_by_state,\n", " load_insurance_sales,\n", ")\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "75352684-14b1-4fba-9751-fa449f7346e9", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python 3.10.0\n" ] } ], "source": [ "!python --version" ] }, { "cell_type": "markdown", "id": "e9d3671f-6255-4b7f-a353-4f10c4aa6686", "metadata": {}, "source": [ "## Step 1: Create the Model Context Protocol Server" ] }, { "cell_type": "code", "execution_count": 3, "id": "34a3ed24-8df7-4afd-8bb7-8d1e00fc83ce", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MCP Server ready! Tools: calculate_profit.\n" ] } ], "source": [ "from fastmcp import FastMCP\n", "\n", "# Initialize MCP Server\n", "mcp = FastMCP(\"BusinessTools\")\n", "\n", "@mcp.tool()\n", "def calculate_profit(revenue: float, expenses: float) -> float:\n", " \"\"\"Calculate profit.\"\"\"\n", " return revenue - expenses\n", "\n", "print(\"MCP Server ready! Tools: calculate_profit.\")" ] }, { "cell_type": "markdown", "id": "c061d2bb-f4fa-4284-9085-5ae2e9ddee55", "metadata": {}, "source": [ "## Step 2: Test the MCP Tool Locally" ] }, { "cell_type": "code", "execution_count": 4, "id": "31892845-8c8d-417a-a441-9893f86183d0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Profit: 400\n" ] } ], "source": [ "profit = calculate_profit(1000, 600)\n", "print(\"Profit:\", profit)" ] }, { "cell_type": "markdown", "id": "10f55680-8f86-4b0f-a575-abf2be40aae4", "metadata": {}, "source": [ "## Step 3: Expand MCP by Adding More Functions" ] }, { "cell_type": "code", "execution_count": 5, "id": "6bfbbc4d-ecee-4492-a472-b85fca86c44c", "metadata": {}, "outputs": [], "source": [ "\n", "@mcp.tool()\n", "def get_sales_from_csv(file_path: str) -> float:\n", " \"\"\"Read total sales from a CSV file.\"\"\"\n", " df = pd.read_csv(file_path)\n", " return df[\"sales\"].sum()" ] }, { "cell_type": "markdown", "id": "a1b749f7-f8b3-4ae0-b07f-599040b5ea43", "metadata": {}, "source": [ "## Step 4: Create the CSV File" ] }, { "cell_type": "code", "execution_count": 7, "id": "da41522c-f512-4772-864e-d084010cbbda", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CSV file created successfully!\n" ] } ], "source": [ "csv_data = \"\"\"date,sales\n", "2024-02-26,200\n", "2024-02-27,450\n", "2024-02-28,300\n", "\"\"\"\n", "\n", "# Save to a file in the data folder\n", "with open(\"data/sales_data.csv\", \"w\") as file:\n", " file.write(csv_data)\n", "\n", "print(\"CSV file created successfully!\")" ] }, { "cell_type": "markdown", "id": "cff6480d-7f34-42aa-8cbe-b68ba3cedc77", "metadata": {}, "source": [ "## Step 5: Run get_sales_from_csv" ] }, { "cell_type": "code", "execution_count": 8, "id": "ef4b7533-cd81-4d68-9a38-73d778f81c24", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total Sales: 950\n" ] } ], "source": [ "sales_total = get_sales_from_csv(\"data/sales_data.csv\")\n", "print(\"Total Sales:\", sales_total)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 6: Calculate Commission" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "commission = calculate_commission([300, 700, 200], rate=0.1)\n", "print('Commission:', commission)" ] }, { "cell_type": "markdown", "metadata": {}, "id": "step-7-load", "source": [ "## Step 7: Load `insurance_sales.csv`" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "id": "step-7-code", "source": [ "records = load_insurance_sales('data/insurance_sales.csv')\n", "print(f'Total records: {len(records)}')" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "id": "step-8-code", "source": [ "total_premium = calculate_total_premium(records)\n", "print('Total Premium:', total_premium)" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "id": "step-9-code", "source": [ "ca_policies = filter_policies_by_state(records, 'CA')\n", "print('CA Policies:', len(ca_policies))" ] }, { "cell_type": "markdown", "metadata": {}, "id": "step-9-explain", "source": [ "These steps show how quickly you can tally premiums and isolate policies by state\u2014handy for reviewing territory performance and carrier payouts." ] }, { "cell_type": "markdown", "id": "3aee3fb6-e2ea-4761-b4e1-41d13ef8c507", "metadata": {}, "source": [ "# \ud83d\ude80 Super Simple Summary: MCP Project Overview\n", "\n", "## **What We Did** \ud83d\udee0\ufe0f\n", "1. **Set up our Python environment** \n", " - Installed Python 3.10 \n", " - Created a virtual environment (venv) \n", " - Installed necessary packages (like pandas) \n", " - Ensured Jupyter Notebook was using the correct environment \n", "\n", "2. **Created a simple MCP (Model Context Protocol) server** \n", " - Used `FastMCP` to set up a tool that can process functions \n", " - Created a `calculate_profit` function to subtract expenses from revenue \n", " - Registered that function with MCP \n", "\n", "3. **Expanded the MCP tool with more functionality** \n", " - Added a new function `get_sales_from_csv` to read a CSV file and sum the sales column \n", " - Used `pandas` to load and process the CSV data \n", "\n", "4. **Tested everything** \u2705 \n", " - Created a CSV file inside the notebook \n", " - Ran our function to sum the sales \n", " - Verified that the result was correct (`950` total sales) \n", "\n", "---\n", "\n", "## **Why Is This Important?** \ud83d\udd25\n", "- **MCP helps structure tools for AI models** \ud83e\udde0 \n", " \u2192 Instead of just writing random functions, MCP lets us build structured \"tools\" that can be reused. \n", "\n", "- **We practiced file handling, data processing, and function registration** \ud83d\udcc2 \n", " \u2192 This is a **real-world skill** that applies to **machine learning, AI automation, and software development**. \n", "\n", "---\n", "\n", "## **Next Steps:**\n", "Would you like to: \n", "1\ufe0f\u20e3 **Expand this project** (e.g., more data analysis, visualization)? \n", "2\ufe0f\u20e3 **Start a new MCP-based project**? \n", "3\ufe0f\u20e3 **Dive deeper into MCP internals** (how it works under the hood)? \n", "\n", "\ud83d\ude80\ud83d\udd25 Let\u2019s build something amazing!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.0" } }, "nbformat": 4, "nbformat_minor": 5 }