Skip to main content
Glama

FastMCP Supply Chain Optimizer

by ANSH-RIYAL
fastmcp_server.cpython-310.pyc7.96 kB
o ��\h�/�@sddlZddlZddlZddlmZmZmZddlmZ ddl m Z ddl m Z ddlmZmZmZmZddlmZdZe jed�e �d �ZGd d �d �Zdad d �Zdd�Zdeeefdefdd�Zdeefdd�Ze dkr~dd�Z!e�"e!��dSdS)�N)�Dict�Any�List)�Server)� stdio_server)�Tool� TextContent�CallToolResult�ListToolsResult)�SupplyChainToolsz'AIzaSyB-d7vpvd2W8kXyVmfjn7XJNiZmDNP6hHM)Zapi_keyzgemini-2.0-flash-expc@s~eZdZdd�Zdd�Zdd�Zdeeefdefd d �Z d edeeefdefd d �Z dd�Z dd�Z de efdd�ZdS)� FastMCPServercCs,td�|_t�|_|��|��d|_dS)Nzsupply-chain-optimizerF)r�serverr �tools� setup_tools� setup_prompts� is_running��self�r�;/Users/anshriyal/Downloads/github/FastMCP/fastmcp_server.py�__init__s  zFastMCPServer.__init__cs6�j��dd��}�j��dtdtf�fdd� �}dS)z/Register all supply chain tools with MCP serverc �s��ttddddddd�id�d �td d ddd d�dd d�ddd�d�gd�d�d �tddddd d�ddd�ddd�ddd�d�gd�d�d �tddddd d�ddd�d�ddgd�d �tddddd d�ddd�d �dd!gd�d �gd"�S)#N�get_inventory_statusz:Get current inventory status for a product or all products�object� product_id�stringzProduct ID to check (optional))�type� description)r� properties)�namerZ inputSchema�update_inventoryz5Update inventory for a specific product and warehousez Product IDz1Warehouse (warehouse_A, warehouse_B, warehouse_C)�integerzQuantity to add/subtract)r� warehouse�quantity)rr�required�calculate_transferz;Calculate and execute inventory transfer between warehouseszSource warehousezDestination warehousezQuantity to transfer)r�from_warehouse� to_warehouser"�predict_stockoutz-Predict when a product might run out of stockzWarehouse to check)rr!r!�recommend_reorderz&Recommend reorder action for a productzQuantity to reorder)rr"r")r)r rrrrr�handle_list_toolssn� ��� ��� ������ �����z4FastMCPServer.setup_tools.<locals>.handle_list_toolsr� argumentsc �s"�zn|dkr�j�|�d��}nP|dkr#�j�|d|d|d�}n=|dkr9�j�|d|d|d|d�}n'|d krI�j�|d|d�}n|d krY�j�|d|d�}nd d |��i}ttd t j |dd�d�gd�WSt y�}zttd dt |���d�gd�WYd}~Sd}~ww)Nrrrr!r"r$r%r&r'r(�errorzUnknown tool: �text���indent)rr,)�contentzError: ) rr�getrr$r'r(r r�json�dumps� Exception�str)rr*�result�errr�handle_call_tool_sJ���������z3FastMCPServer.setup_tools.<locals>.handle_call_toolN)r Z list_toolsZ call_toolr5�dict)rr)r8rrrrs CzFastMCPServer.setup_toolscCsd|_d|_dS)z,Setup system prompts for different scenariosaWYou are a Supply Chain Optimization Agent. Your job is to: 1. Analyze supply chain events (demand spikes, delays, cost increases) 2. Check current inventory status 3. Make intelligent recommendations for: - Inventory transfers between warehouses - Reorder quantities - Stockout predictions - Cost optimization Always think step by step: 1. First, get current inventory status 2. Analyze the event impact 3. Check for potential stockouts 4. Recommend specific actions 5. Execute the actions if needed Be concise but thorough in your analysis. Use the available tools to make decisions.z�New Supply Chain Event: {event_type} for {product_id} - {value} Current Inventory Status: {inventory_status} Please analyze this event and recommend appropriate actions using the available tools.N)� system_prompt�event_prompt_templaterrrrr�s zFastMCPServer.setup_prompts�event�returnc �s��z5|j�|�d��}|jj|�d�|�d�|�d�tj|dd�d�}|j�d|��}|�||�IdH}|WSt yO}z d t |���WYd}~Sd}~ww) z7Process a supply chain event with parallel tool callingr� event_type�valuer-r.)r>rr?�inventory_status� NzError processing event: ) rrr1r;�formatr2r3r:�_process_with_gemini_and_toolsr4r5)rr<r@Z event_promptZ full_prompt�responser7rrr�process_event_parallel�s � ���z$FastMCPServer.process_event_parallel�promptc �s��zbt�|�}|j}g}d|��vr.|�d�dkr.|j�dddd�}|�d|�dd ����d |��vs:d |��vrP|j�|�d�d �}|�d |�dd����d|�d�}|ra|dd� |�7}|WSt y|} z dt | ���WYd} ~ Sd} ~ ww)zFProcess prompt with Gemini and execute tools in parallel when possibleZtransferr�P001Z warehouse_BZ warehouse_A�z TRANSFER: �actionzTransfer executedZreorder�order�2z REORDER: zReorder recommendedz AI Analysis: rAzExecuted Actions: � zError in AI processing: N) �modelZgenerate_contentr,�lowerr1rr$�appendr(�joinr4r5) rrFr<rDZ response_textZactionsZtransfer_resultZreorder_resultZfinal_responser7rrrrC�s0�  � � ��z,FastMCPServer._process_with_gemini_and_toolsc�sB�td�td�|j��}tdt|��d��d|_td�dS)zStart the MCP serveru,🚀 Starting FastMCP Supply Chain Server...u📊 Loading inventory data...u ✅ Loaded z productsTu✅ FastMCP Server is running!)�printrr�lenr)rZinitial_inventoryrrr� start_server�s� zFastMCPServer.start_serverc�s.�td�|j��td�d|_td�dS)z"Stop the server and save inventoryu 💾 Saving inventory changes...u✅ Inventory saved!Fu🛑 FastMCP Server stopped!N)rQrZsave_inventoryrrrrr� stop_server�s �  zFastMCPServer.stop_servercCs |j��S)zGet all recorded actions)r�get_actions_logrrrrrU�s zFastMCPServer.get_actions_logN)�__name__� __module__� __qualname__rrrrr5rrErCrSrTrrUrrrrr sn%r c�s�t�at��IdHS�N)r �server_instancerSrrrrrSs�rSc�s�tr t��IdHdSdSrY)rZrTrrrrrTs��rTr<r=c�s�tr t�|�IdHSdS)NzServer not running)rZrE)r<rrr� process_event s�r[cCstrt��SgSrY)rZrUrrrrrUsrU�__main__c�s@�t�IdHdddd�}t|�IdH}t|�t�IdHdS)NZ DEMAND_SPIKErGz40 units)r>rr?)rSr[rQrT)Z test_eventr6rrr�tests� �r])#�asyncior2�os�typingrrrZgoogle.generativeaiZ generativeaiZgenaiZ mcp.serverrZmcp.server.stdiorZ mcp.typesrrr r Zsupply_chain_toolsr ZGEMINI_API_KEYZ configureZGenerativeModelrMr rZrSrTr5r[rUrVr]�runrrrr�<module>s.      p �

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ANSH-RIYAL/FastMCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server