Skip to main content
Glama

MCP Agent Platform

base_agent.cpython-310.pyc9.45 kB
o �$�g;7�@srdZddlZddlZddlZddlmZmZmZmZm Z m Z ddl Z ddl m Z ejejdd�Gdd�d�ZdS) u 基础智能体类 �N)�Dict�Any�Callable� Coroutine�Optional�List)� MCPMessagez4%(asctime)s - %(name)s - %(levelname)s - %(message)s)�level�formatc@s\eZdZdZdedededefdd�Zdedeeee ffd d �Z d d �Z d d�Z dededefdd�Z defdd�Zdd�Zdd�Zdedeee ffdd�Zdedeeee fgeffdd�Zdeee ffdd �Zdeee ffd!d"�Zdeee ffd#d$�Zdeee ffd%d&�Zdefd'd(�Zd)d*�Zdeefd+d,�Zdedefd-d.�Zd/S)0� BaseAgentu基础智能体类�agent_id� agent_type�host�portcCsL||_||_||_||_t�d|���|_i|_i|_d|_ d|_ i|_ dS)NzAgent:F) r r rr�logging� getLogger�logger� connections�message_handlers�server� is_running�agent_addresses)�selfr r rr�r�5D:\DeepSeek\MCPAgent\mcpTest\src\agents\base_agent.py�__init__s zBaseAgent.__init__�returncCsv||jvr ||j|d�S||jvr|j|Sddd�ddd�ddd�ddd�d�}||vr9|||j|<||Sd S) u获取智能体的地址信息)r � connection� localhostiJ)rriKiLiM)�brain�eye�ear�mouthN)rr)rr Zdefault_addressesrrr�get_agent_addresss   �zBaseAgent.get_agent_addressc�sv�d|_t�|j|j|j�IdH|_|j�d|j �d|j�d|j���|� d|j �|� d|j �|� d|j �dS) u启动智能体服务器TN�Agent z started on �:�text�command�status)r� websockets�serve�_handle_connectionrrrr�infor �register_handler�_handle_text_message�_handle_command_message�_handle_status_message�rrrr�start7s�  �$zBaseAgent.startc�sB�|jr|j��|j��IdHd|_|j�d|j�d��dS)u停止智能体服务器NFr$z stopped)r�close� wait_closedrrr,r r1rrr�stopDs � zBaseAgent.stopc �s�d}d}t|�D]�}zJd|�d|��}tj|ddd�IdH}||j|<|j�d |�d |���d |j|d d |jid�d�} |�t � | ��IdHt � |� ||��WdSty�} z(|j�d|d�d|�d|�d| ���||dkrt �|�IdHWYd} ~ q d} ~ wwdS)u连接到其他智能体��zws://r%�� )Z ping_intervalZ ping_timeoutNzConnected to agent z at r(� connectedr �r(�details�� message_type� sender_id� receiver_id�contentTzAttempt ��/z failed to connect to agent �: F)�ranger)�connectrrr,r r �send�json�dumps�asyncio� create_task�_receive_messages� Exception�error�sleep) rr rrZ max_retriesZ retry_delayZattempt�urir� status_msg�errr�connect_to_agentLs>�  � ��� ( ��zBaseAgent.connect_to_agentc �s�� z|��IdH}|�|�IdHWnYtjjyG|j�d|�d��||jvr/|j|=|�||j |j �IdHrD|j�d|���YdSt yl}z|j� d|�d|���t �d�IdHWYd}~nd}~wwq) u!接收来自特定连接的消息TNzConnection to agent z closedzReconnected to agent zError receiving messages from rDrB)�recv�_process_messager)� exceptions�ConnectionClosedrr,rrSrrrMrNrJrO)rr r�messagerRrrrrLts$� ���zBaseAgent._receive_messagesc �st�zo|��IdH}t�|�}|�d�dkr^|�di��d�dkr^|�d�}||j|<|j�d|�d��d|j|d d |jid �d �}|� t� |��IdH |��IdH}|� |�IdHqN|j� d|���|� �IdHWdStjjy�t|j���D]\}}||kr�|j|=|j�d|�d��YdSq�YdSty�} z|j�d| ���WYd} ~ dSd} ~ ww)u处理新的WebSocket连接Nr>r(rAr:r?r$z connected�acceptedr r;r=Tz%Received invalid connection message: � disconnectedzError handling connection: )rTrH�loads�getrrr,r r rGrIrU�warningr3r)rVrW�list�itemsrMrN) r� websocket�pathrX�datar?�responser �connrRrrrr+�sF� $  ��� ��� ��zBaseAgent._handle_connectionc �s��z0t|t�r t�|�}n|}|�d�}||jvr&|j||�IdHWdS|j�d|���WdStyM}z|j� d|���WYd}~dSd}~ww)u处理接收到的消息r>NzNo handler for message type: zError processing message: ) � isinstance�strrHr[r\rrr]rMrN)r� message_datarbr>rRrrrrU�s�     ��zBaseAgent._process_messager@rXc �s��z�|�|�}|s|j�d|���WdSt|t�r|��}nt|t�r;d|vr-|j|d<d|vr5||d<t� |�}n|}||j vrrz|j |� |�IdHWWdSt yq}z|j�d|�d|���WYd}~WdSd}~wwd |vr�d |vr�|j� d |�d |d �d |d �d��|�||d |d �IdHr�|�||�IdHWS|j�d|���WdS|j�d|�d��WdSt y�}z|j�d|���WYd}~dSd}~ww)u发送消息到指定智能体u未找到智能体: Fr?r@NTzError sending message to rDrru尝试连接到智能体 z (r%�)u无法连接到智能体 zNo connection to agent z and no address informationu发送消息失败: )r#rrNrerZto_json�dictr rHrIrrGrMr,rS� send_messager])rr@rX�receiverrgrRrrrrj�sJ�       ��(��zBaseAgent.send_messager>�handlercCs ||j|<|j�d|���dS)u注册消息处理器z%Registered handler for message type: N)rrr,)rr>rlrrrr-�s zBaseAgent.register_handlerc �s.�|j�d|d�d|d�dd����dS)u!处理文本消息的默认方法zReceived text message from r?rDrAr&�N)rr,r\)rrXrrrr.�s�,zBaseAgent._handle_text_messagec�s2�|d�dd�}|j�d|d�d|���dS)u!处理命令消息的默认方法rAr'rmzReceived command from r?rDN)r\rr,)rrXr'rrrr/�s� z!BaseAgent._handle_command_messagec �s��|d�dd�}|j�d|d�d|���|dkr4|j�d|d�d |d�d i��d ����dS|d krE|j�d |d���dS|dkrd|d|jvrV|j|d=|j�d|d�d��dSdS)u!处理状态消息的默认方法rAr(rmzReceived status update from r?rDr:r$z connected with type: r<r rYzConnection accepted by � disconnectedrZN)r\rr,r)rrXr(rrrr0s� � �z BaseAgent._handle_status_messagec�s6�g}|jD]}|�||�IdH}|�||f�q|S)u'广播消息到所有连接的智能体N)rrj�append)rrX�resultsr �resultrrr�broadcast_messages � zBaseAgent.broadcast_messagec �s��||jvrVz0d|j|dddid�d�}|�||�IdH|j|��IdH|j|=|j�d|���Wd StyU}z|j�d |�d |���WYd}~d Sd}~wwd S) u!断开与指定智能体的连接r(rn�reasonZclient_disconnectr;r=NzDisconnected from agent TzError disconnecting from agent rDF)rr rjr3rr,rMrN)rr rQrRrrr�disconnect_from_agents,� ��� ��zBaseAgent.disconnect_from_agentc�s@�t|j���}g}|D]}|�|�IdH}|�||f�q |S)u!断开与所有智能体的连接N)r^r�keysrtro)rZ agent_idsrpr rqrrr�disconnect_all5s�zBaseAgent.disconnect_allcCst|j���S)u)获取所有已连接的智能体ID列表)r^rrur1rrr�get_connected_agents>szBaseAgent.get_connected_agentscCs ||jvS)u*检查是否与指定智能体保持连接)r)rr rrr�is_connected_toBs zBaseAgent.is_connected_toN)�__name__� __module__� __qualname__�__doc__rf�intrrrrr#r2r5rSrLr+rUrjrrr-r.r/r0rrrtrvrrw�boolrxrrrrr s(  (+$0 r )r|rJrHr�typingrrrrrrr)�src.utils.mcp_protocolr� basicConfig�INFOr rrrr�<module>s  �

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/rolenet/McpAgentRobot'

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